Photon

Photon Git Source Tree

Root/scripts/exerciser.php

1<?php
2/**
3 * Act like a Mongrel2 server and push a request to the handler.
4 * It prints the raw answer.
5 */
6$pub_addr = 'ipc://handler-res';
7$sub_addr = 'ipc://mongrel-req';
8$sub_addr = 'tcp://127.0.0.1:9997';
9$pub_addr = 'tcp://127.0.0.1:9996';
10
11$n = 10000;
12
13$payload = file_get_contents($argv[1]);
14$batch = (isset($argv[2]));
15$ctx = new ZMQContext(1);
16$serv = $ctx->getSocket(ZMQ::SOCKET_DOWNSTREAM);
17$serv->bind($sub_addr);
18$get = $ctx->getSocket(ZMQ::SOCKET_SUB);
19$get->bind($pub_addr);
20$get->setSockOpt(ZMQ::SOCKOPT_SUBSCRIBE, '');
21$start = microtime(true);
22if ($batch) {
23 // Batching all the requests and then getting the answers from the
24 // queue is way, way, way faster. Even for larger messages, the
25 // generation of the answers is most likely using the CPU a bit
26 // better and the answers may not come in the same order.
27 $i = 0;
28 while ($i < $n) {
29 $serv->send($payload);
30 $i++;
31 }
32 $i = 0;
33 while ($i < $n) {
34 $message = $get->recv();
35 $i++;
36 }
37} else {
38 $i = 0;
39 while ($i < $n) {
40 $serv->send($payload);
41 $message = $get->recv();
42 $i++;
43 usleep(1000);
44 }
45}
46$end = microtime(true);
47print "Done.\n";
48print "Last message:\n";
49print $message;
50print "\n\n";
51printf("Elapsed time: %ss\n", ($end-$start));
52printf("Req/s: %s\n", (float)$n/($end-$start));
53
54
55

Archive Download this file

Branches

Tags