X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-http;a=blobdiff_plain;f=scripts%2Fbench_select_vs_event.php;h=3bc9aed75c71adbc3ca75dc1759e35d99a8d07b9;hp=589f31e2c2f9413dadb81b5c0bde1d542d8de595;hb=HEAD;hpb=0b5d61db388a4177ee32092b9fe7a4e614520bb2 diff --git a/scripts/bench_select_vs_event.php b/scripts/bench_select_vs_event.php index 589f31e..3bc9aed 100644 --- a/scripts/bench_select_vs_event.php +++ b/scripts/bench_select_vs_event.php @@ -1,60 +1,26 @@ url = $url; - $pool->cnt = $n; - - $pool->enableEvents($e); - - for ($i = 0; $i < $c; ++$i) { - $pool->push(); - } - try { - $pool->send(); - } catch (Exception $ex) { - echo $ex, "\n"; - } - } - - function push() { - if ($this->cnt > 0) { - request::init($this, $this->url)->id = $this->cnt--; - } - } +function usage($e = null) { + global $argv; + if ($e) { + fprintf(STDERR, "ERROR: %s\n\n", $e); + } + fprintf(STDERR, "Usage: %s -u -n -c [-p (enable pipelining)] [-e (use libevent)]\n", $argv[0]); + fprintf(STDERR, "\nDefaults: -u http://localhost/ -n 1000 -c 10\n\n"); + exit(-1); } -class request extends HttpRequest { - static $counter = 0; - - public $id; - private $pool; - - static function init(pool $pool, $url) { - $r = new request($url); - $r->pool = $pool; - $pool->attach($r); - return $r; - } - - function onFinish() { - ++self::$counter; - $this->pool->detach($this); - $this->pool->push(); +function push($client, $url, &$n) { + if ($n-- > 0) { + $req = new http\Client\Request("GET", $url); + $client->enqueue($req, function($response) use ($client, $req, $url, &$n) { + global $count; ++$count; + push($client, $url, $n); + return true; // dequeue + }); } } -function usage() { - global $argv; - fprintf(STDERR, "Usage: %s -u -n -c -e (use libevent)\n", $argv[0]); - fprintf(STDERR, "\nDefaults: -u http://localhost/ -n 1000 -c 10\n\n"); - exit(-1); -} - isset($argv) or $argv = $_SERVER['argv']; defined('STDERR') or define('STDERR', fopen('php://stderr', 'w')); @@ -63,10 +29,25 @@ isset($opts["u"]) or $opts["u"] = "http://localhost/"; isset($opts["c"]) or $opts["c"] = 10; isset($opts["n"]) or $opts["n"] = 1000; -http_parse_message(http_head($opts["u"]))->responseCode == 200 or usage(); +$argc > 1 or usage(); $time = microtime(true); -pool::fetch($opts["u"], $opts["n"], $opts["c"], isset($opts["e"])); -printf("\n> %10.6fs\n", microtime(true)-$time); +$count = 0; +$client = new http\Client; + +$client->enablePipelining($opts["p"]===false); +$client->enableEvents($opts["e"]===false); + +for ($i = 0, $x = $opts["n"]; $i < $opts["c"]; ++$i) { + push($client, $opts["u"], $x); +} + +try { + $client->send(); +} catch (Exception $e) { + echo $e; +} + +printf("\n> %10.6fs (%3.2fM)\n", microtime(true)-$time, memory_get_peak_usage(true)/1024/1024); -request::$counter == $opts["n"] or printf("\nOnly %d finished\n", request::$counter); +$count == $opts["n"] or printf("\nOnly %d finished\n", $count);