Skip to content

Instantly share code, notes, and snippets.

@barryvdh
Last active December 3, 2018 19:35
Show Gist options
  • Save barryvdh/75667b91f4cd9820ae9c746d752166b7 to your computer and use it in GitHub Desktop.
Save barryvdh/75667b91f4cd9820ae9c746d752166b7 to your computer and use it in GitHub Desktop.
Example scenarios
{
"require": {
"hollodotme/fast-cgi-client": "^2.0",
"symfony/process": "^4.2"
}
}
<?php
// Bootstrap
require 'vendor/autoload.php';
use hollodotme\FastCGI\Client;
use hollodotme\FastCGI\Requests\GetRequest;
use hollodotme\FastCGI\SocketConnections\UnixDomainSocket;
class Scenarios {
protected $num = 10;
protected $callables = ['runExec', 'runProcess', 'runFastcgi'];
protected $client;
public function __construct()
{
$connection = new UnixDomainSocket('/Users/barry/.valet/valet.sock');
$this->client = new Client( $connection );
}
public function runAll() {
foreach ($this->callables as $callable) {
// Cold start
$start = microtime(true);
$output = call_user_func([$this, $callable]);
// echo $output;
echo("$callable COLD: " . round((microtime(true) - $start) * 1000, 2) . "ms\n");
$start = microtime(true);
// Hot start
for ($i=0; $i<$this->num; $i++) {
$output = call_user_func([$this, $callable]);
// echo $output;
}
echo("$callable HOT: " . round(((microtime(true) - $start) / $this->num) * 1000, 2) . "ms\n");
}
}
public function runExec()
{
return exec('php script.php');
}
public function runProcess()
{
$process = new \Symfony\Component\Process\Process(['/usr/local/bin/php', 'script.php']);
$process->setTimeout(null);
// This waits for the process to finish (i.e. waits until an event has been processed)
$process->run();
return $process->getOutput();
}
public function runFastcgi()
{
$request = new GetRequest(__DIR__ .'/script.php', '');
$response = $this->client->sendRequest($request);
return $response->getBody();
}
}
$scenarios = new Scenarios();
$scenarios->runAll();
<?php
echo microtime(true) . " - Hello world!\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment