rfc5988 fixes
[m6w6/ext-http] / bench_select_vs_event.php
1 <?php
2
3 function usage($e = null) {
4 global $argv;
5 if ($e) {
6 fprintf(STDERR, "ERROR: %s\n\n", $e);
7 }
8 fprintf(STDERR, "Usage: %s -u <URL> -n <requests> -c <concurrency> [-p (enable pipelining)] [-e (use libevent)]\n", $argv[0]);
9 fprintf(STDERR, "\nDefaults: -u http://localhost/ -n 1000 -c 10\n\n");
10 exit(-1);
11 }
12
13 function push($client, $url, &$n) {
14 if ($n-- > 0) {
15 $req = new http\Client\Request("GET", $url);
16 $client->enqueue($req, function($response) use ($client, $req, $url, &$n) {
17 global $count; ++$count;
18 push($client, $url, $n);
19 return true; // dequeue
20 });
21 }
22 }
23
24 isset($argv) or $argv = $_SERVER['argv'];
25 defined('STDERR') or define('STDERR', fopen('php://stderr', 'w'));
26
27 $opts = getopt("u:c:n:e");
28 isset($opts["u"]) or $opts["u"] = "http://localhost/";
29 isset($opts["c"]) or $opts["c"] = 10;
30 isset($opts["n"]) or $opts["n"] = 1000;
31
32 $argc > 1 or usage();
33
34 $time = microtime(true);
35 $count = 0;
36 $client = new http\Client;
37
38 $client->enablePipelining($opts["p"]===false);
39 $client->enableEvents($opts["e"]===false);
40
41 for ($i = 0, $x = $opts["n"]; $i < $opts["c"]; ++$i) {
42 push($client, $opts["u"], $x);
43 }
44
45 try {
46 $client->send();
47 } catch (Exception $e) {
48 echo $e;
49 }
50
51 printf("\n> %10.6fs (%3.2fM)\n", microtime(true)-$time, memory_get_peak_usage(true)/1024/1024);
52
53 $count == $opts["n"] or printf("\nOnly %d finished\n", $count);