From 92a11623afb9bb69d248043bb2ccbcca6ff1a726 Mon Sep 17 00:00:00 2001 From: Michael Dowling Date: Sun, 23 Mar 2014 10:53:34 -0700 Subject: [PATCH] Adding perf test to makefile --- Makefile | 4 ++++ tests/Server.php | 20 +++++++++++++------- tests/perf.php | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 7 deletions(-) create mode 100644 tests/perf.php diff --git a/Makefile b/Makefile index d97557bd..27cdb8e4 100644 --- a/Makefile +++ b/Makefile @@ -34,4 +34,8 @@ docs: .FORCE view-docs: open docs/_build/html/index.html +perf: start-server + php tests/perf.php + $(MAKE) stop-server + .FORCE: diff --git a/tests/Server.php b/tests/Server.php index 89d52018..13dc5fbf 100644 --- a/tests/Server.php +++ b/tests/Server.php @@ -125,6 +125,18 @@ class Server self::$started = false; } + public static function wait($maxTries = 3) + { + $tries = 0; + while (!self::isListening() && ++$tries < $maxTries) { + usleep(100000); + } + + if (!self::isListening()) { + throw new \RuntimeException('Unable to contact node.js server'); + } + } + private static function start() { if (self::$started){ @@ -134,13 +146,7 @@ class Server if (!self::isListening()) { exec('node ' . __DIR__ . \DIRECTORY_SEPARATOR . 'server.js ' . self::$port . ' >> /tmp/server.log 2>&1 &'); - $tries = 0; - while (!self::isListening() && ++$tries < 3) { - usleep(100000); - } - if (!self::isListening()) { - throw new \RuntimeException('Unable to contact node.js server'); - } + self::wait(); } self::$started = true; diff --git a/tests/perf.php b/tests/perf.php new file mode 100644 index 00000000..1f8567aa --- /dev/null +++ b/tests/perf.php @@ -0,0 +1,47 @@ + Server::$url]); + +$t = microtime(true); +for ($i = 0; $i < $total; $i++) { + $client->get('/guzzle-server/perf'); +} +$totalTime = microtime(true) - $t; +$perRequest = ($totalTime / $total) * 1000; +printf("Serial: %f (%f ms / request) %d total\n", + $totalTime, $perRequest, $total); + +// Create a generator used to yield batches of requests to sendAll +$reqs = function () use ($client, $total) { + for ($i = 0; $i < $total; $i++) { + yield $client->createRequest('GET', '/guzzle-server/perf'); + } +}; + +$t = microtime(true); +$client->sendAll($reqs(), ['parallel' => $parallel]); +$totalTime = microtime(true) - $t; +$perRequest = ($totalTime / $total) * 1000; +printf("Parallel: %f (%f ms / request) %d total with %d in parallel\n", + $totalTime, $perRequest, $total, $parallel);