Skip to content

Instantly share code, notes, and snippets.

@Ocramius
Forked from ebernhardson/benchmark.php
Created January 11, 2013 01:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ocramius/4507173 to your computer and use it in GitHub Desktop.
Save Ocramius/4507173 to your computer and use it in GitHub Desktop.
<?php
//
// Measure the time to execute two thousand jobs ten times.
//
require_once __DIR__.'/../vendor/autoload.php';
function benchmark($resque, $count) {
$start = microtime(true);
for($i=$count;$i > 0;--$i) {
$jid = $resque->enqueue('default', 'DanceForMe', null, $i <= 1);
}
echo "waiting\n";
$status = new Resque_Job_Status($jid);
while($status->get() < $status::STATUS_FAILED) {
usleep(10000);
}
return microtime(true) - $start;
}
// Add 2000 jobs and wait for the last one to finish processing
$resque = new Resque;
$count = 2000;
foreach(range(1,10) as $_) {
printf("Completed %d jobs in %.3fs\n", $count, benchmark($resque, $count));
}
<?php
//
// Called by php-fpm to kick off a job
//
require_once __DIR__.'/../vendor/autoload.php';
class DanceForMe
{
public $job;
public $args;
public $queue;
public function perform()
{
echo "Dancing!\n";
}
}
if (!isset($_GET['job'])) {
die(json_encode(['result' => false, 'message' => 'No Job Provided']));
}
list($queue, $payload) = json_decode($_GET['job'], true);
$job = new Resque_Job($queue, $payload);
$job->perform();
<?php
//
// Intended to be used with resque.php as APP_INCLUDE.
// FCGIClient from https://github.com/PreetamJinka/BitTP/blob/master/src/FCGIClient.php
//
require_once __DIR__.'/../vendor/autoload.php';
class FcgiWorker
{
// static to re-use the connection
// TODO: detect error and re-connect
static $fcgi;
public function perform()
{
if (!self::$fcgi) {
self::$fcgi = new BitTP\FCGIClient('127.0.0.1', 9000);
self::$fcgi->setKeepAlive(true);
}
$this->args['class'] = $this->args['__fcgi_class'];
unset($this->args['__fcgi_class']);
self::$fcgi->request([
'GATEWAY_INTERFACE' => 'FastCGI/1.0',
'REQUEST_METHOD' => 'GET',
'SCRIPT_FILENAME' => __DIR__.'/fcgi_worker.php',
'SERVER_SOFTWARE' =>'PHP-Resque-fcgi/0.1',
'REMOTE_ADDR' => '127.0.0.1',
'REMOTE_PORT' => '8888',
'SERVER_ADDR' => '127.0.0.1',
'SERVER_PORT' => '8888',
'SERVER_NAME' => php_uname('n'),
'SERVER_PROTOCOL' => 'HTTP/1.1',
'QUERY_STRING' => 'job='.urlencode(json_encode([$this->queue, $this->args])),
], '');
$response = self::$fcgi->response();
if (!isset($response['body'])) {
throw new \Exception(
'Job exited with no response: ' . print_r($response, true)
);
}
$result = json_decode($response['body'], true);
if (!$result) {
throw new \Exception(
'Job exited with unreadable response body: ' . print_r($response, true)
);
}
if ($result['result'] !== true) {
throw new \Exception(
'Job failed: ' . print_r($response, true)
);
}
// success
}
}
Resque_Event::listen('afterFork', function(Resque_Job $job) {
// Swap out the job with our fcgi worker
$job->payload['__fcgi_class'] = $job->payload['class'];
$job->payload['class'] = 'FcgiWorker';
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment