etag test & fixes; set default etag mode for temp streams to crc32(b)
[m6w6/ext-http] / bench_select_vs_event.php
1 <?php
2
3 use http\request\Pool as HttpRequestPool;
4 use http\Request as HttpRequest;
5
6 $factory = new http\request\Factory("curl", array("requestPoolClass" => "pool", "requestClass" => "request"));
7
8 class pool extends HttpRequestPool {
9 private $url;
10 private $cnt;
11
12 private $factory;
13
14 static function fetch($factory, $url, $n, $c, $e, $p) {
15 $pool = $factory->createPool();
16 $pool->factory = $factory;
17 $pool->url = $url;
18 $pool->cnt = $n;
19
20 $pool->enablePipelining($p);
21 $pool->enableEvents($e);
22
23 for ($i = 0; $i < $c; ++$i) {
24 $pool->push();
25 }
26 try {
27 $pool->send();
28 } catch (Exception $ex) {
29 echo $ex, "\n";
30 }
31 }
32
33 function push() {
34 if ($this->cnt > 0) {
35 $this->factory->createRequest()->init($this, $this->url)->id = $this->cnt--;
36 }
37 }
38 }
39
40 class request extends HttpRequest implements SplObserver {
41 static $counter = 0;
42
43 public $id;
44 private $pool;
45
46 function init(pool $pool, $url) {
47 $this->setUrl($url);
48 $this->pool = $pool;
49 $this->attach($this);
50 $pool->attach($this);
51 return $this;
52 }
53
54 function update(SplSubject $r) {
55 if ($r->getProgress()->finished) {
56 ++self::$counter;
57 $this->pool->detach($this);
58 $this->detach($this);
59 $this->pool->push();
60 }
61 }
62 }
63
64 function usage($e = null) {
65 global $argv;
66 if ($e) {
67 fprintf(STDERR, "ERROR: %s\n\n", $e);
68 }
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");
71 exit(-1);
72 }
73
74 isset($argv) or $argv = $_SERVER['argv'];
75 defined('STDERR') or define('STDERR', fopen('php://stderr', 'w'));
76
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;
81
82 try {
83 ($c=$factory->createRequest($opts["u"])->send()->getResponseCode()) == 200 or usage("Received response code $c");
84 } catch (Exception $ex) {
85 usage($ex->getMessage());
86 }
87
88 $argc > 1 or usage();
89
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);
93
94 request::$counter == $opts["n"] or printf("\nOnly %d finished\n", request::$counter);