some verbosity adjustments
[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, $p) {
8 $pool = new pool;
9 $pool->url = $url;
10 $pool->cnt = $n;
11
12 $pool->enablePipelining($p);
13 $pool->enableEvents($e);
14
15 for ($i = 0; $i < $c; ++$i) {
16 $pool->push();
17 }
18 try {
19 $pool->send();
20 } catch (Exception $ex) {
21 echo $ex, "\n";
22 }
23 }
24
25 function push() {
26 if ($this->cnt > 0) {
27 request::init($this, $this->url)->id = $this->cnt--;
28 }
29 }
30
31 function cnt() {
32 return $this->cnt;
33 }
34 }
35
36 class request extends HttpRequest {
37 static $counter = 0;
38
39 public $id;
40 private $pool;
41
42 static function init(pool $pool, $url) {
43 #printf("init %d\n", $pool->cnt());
44 $r = new request($url);
45 $r->pool = $pool;
46 $pool->attach($r);
47 return $r;
48 }
49
50 function onFinish() {
51 #printf("left %d\n", count($this->pool));
52 #printf("done %d\n", $this->id);
53 ++self::$counter;
54 $this->pool->detach($this);
55 $this->pool->push();
56 }
57 }
58
59 function usage() {
60 global $argv;
61 fprintf(STDERR, "Usage: %s -u <URL> -n <requests> -c <concurrency> -e (use libevent) -p (use pipelining)\n", $argv[0]);
62 fprintf(STDERR, "\nDefaults: -u http://localhost/ -n 1000 -c 10\n\n");
63 exit(-1);
64 }
65
66 isset($argv) or $argv = $_SERVER['argv'];
67 defined('STDERR') or define('STDERR', fopen('php://stderr', 'w'));
68
69 $opts = getopt("u:c:n:ep"); #var_dump($opts);
70 isset($opts["u"]) or $opts["u"] = "http://localhost/";
71 isset($opts["c"]) or $opts["c"] = 10;
72 isset($opts["n"]) or $opts["n"] = 500;
73
74 http_parse_message(http_head($opts["u"]))->responseCode == 200 or usage();
75
76 $time = microtime(true);
77 pool::fetch($opts["u"], $opts["n"], $opts["c"], isset($opts["e"]), isset($opts["p"]));
78 printf("\n> %10.6fs\n", microtime(true)-$time);
79
80 printf("\nMemory usage: %s/%s\n", number_format(memory_get_usage()),number_format(memory_get_peak_usage()));
81
82 if (request::$counter != $opts["n"]) {
83 printf("\nOnly %d finished\n", request::$counter);
84 exit(1);
85 }