add some more hacks
[m6w6/ext-http] / scripts / bench_select_vs_event.php
1 <?php
2
3 class pool extends HttpRequestPool {
4 private $url;
5 private $cnt;
6
7 static function fetch($url, $n, $c, $e) {
8 $pool = new pool;
9 $pool->url = $url;
10 $pool->cnt = $n;
11
12 $pool->enableEvents($e);
13
14 for ($i = 0; $i < $c; ++$i) {
15 $pool->push();
16 }
17 try {
18 $pool->send();
19 } catch (Exception $ex) {
20 echo $ex, "\n";
21 }
22 }
23
24 function push() {
25 if ($this->cnt > 0) {
26 request::init($this, $this->url)->id = $this->cnt--;
27 }
28 }
29 }
30
31 class request extends HttpRequest {
32 static $counter = 0;
33
34 public $id;
35 private $pool;
36
37 static function init(pool $pool, $url) {
38 $r = new request($url);
39 $r->pool = $pool;
40 $pool->attach($r);
41 return $r;
42 }
43
44 function onFinish() {
45 ++self::$counter;
46 $this->pool->detach($this);
47 $this->pool->push();
48 }
49 }
50
51 function usage() {
52 global $argv;
53 fprintf(STDERR, "Usage: %s -u <URL> -n <requests> -c <concurrency> -e (use libevent)\n", $argv[0]);
54 fprintf(STDERR, "\nDefaults: -u http://localhost/ -n 1000 -c 10\n\n");
55 exit(-1);
56 }
57
58 isset($argv) or $argv = $_SERVER['argv'];
59 defined('STDERR') or define('STDERR', fopen('php://stderr', 'w'));
60
61 $opts = getopt("u:c:n:e");
62 isset($opts["u"]) or $opts["u"] = "http://localhost/";
63 isset($opts["c"]) or $opts["c"] = 10;
64 isset($opts["n"]) or $opts["n"] = 1000;
65
66 http_parse_message(http_head($opts["u"]))->responseCode == 200 or usage();
67
68 $time = microtime(true);
69 pool::fetch($opts["u"], $opts["n"], $opts["c"], isset($opts["e"]));
70 printf("\n> %10.6fs\n", microtime(true)-$time);
71
72 request::$counter == $opts["n"] or printf("\nOnly %d finished\n", request::$counter);