3 use http\request\Pool
as HttpRequestPool
;
4 use http\Request
as HttpRequest
;
6 $factory = new http\request\
Factory("curl", array("requestPoolClass" => "pool", "requestClass" => "request"));
8 class pool
extends HttpRequestPool
{
14 static function fetch($factory, $url, $n, $c, $e, $p) {
15 $pool = $factory->createPool();
16 $pool->factory
= $factory;
20 $pool->enablePipelining($p);
21 $pool->enableEvents($e);
23 for ($i = 0; $i < $c; ++
$i) {
28 } catch (Exception
$ex) {
35 $this->factory
->createRequest()->init($this, $this->url
)->id
= $this->cnt
--;
40 class request
extends HttpRequest
implements SplObserver
{
46 function init(pool
$pool, $url) {
54 function update(SplSubject
$r) {
55 if ($r->getProgress()->finished
) {
57 $this->pool
->detach($this);
64 function usage($e = null) {
67 fprintf(STDERR
, "ERROR: %s\n\n", $e);
69 fprintf(STDERR
, "Usage: %s -u <URL> -n <requests> -c <concurrency> [-p (enable pipelining)] [-e (use libevent)]\n", $argv[0]);
70 fprintf(STDERR
, "\nDefaults: -u http://localhost/ -n 1000 -c 10\n\n");
74 isset($argv) or $argv = $_SERVER['argv'];
75 defined('STDERR') or define('STDERR', fopen('php://stderr', 'w'));
77 $opts = getopt("u:c:n:e");
78 isset($opts["u"]) or $opts["u"] = "http://localhost/";
79 isset($opts["c"]) or $opts["c"] = 10;
80 isset($opts["n"]) or $opts["n"] = 1000;
83 ($c=$factory->createRequest($opts["u"])->send()->getResponseCode()) == 200 or usage("Received response code $c");
84 } catch (Exception
$ex) {
85 usage($ex->getMessage());
90 $time = microtime(true);
91 pool
::fetch($factory, $opts["u"], $opts["n"], $opts["c"], isset($opts["e"]), isset($opts["p"]));
92 printf("\n> %10.6fs (%3.2fM)\n", microtime(true)-$time, memory_get_peak_usage(true)/1024/1024);
94 request
::$counter == $opts["n"] or printf("\nOnly %d finished\n", request
::$counter);