|
| 1 | +<?php |
| 2 | + |
| 3 | +use Swoole\Coroutine; |
| 4 | + |
| 5 | +//关闭错误输出 |
| 6 | +//error_reporting(0); |
| 7 | +$shortopts = "c:"; |
| 8 | +$shortopts .= "n:"; |
| 9 | +$shortopts .= "s:"; |
| 10 | +$shortopts .= "f:"; |
| 11 | +$shortopts .= "p::"; |
| 12 | +$shortopts .= "l:"; |
| 13 | + |
| 14 | +$opt = getopt($shortopts); |
| 15 | +//并发数量 |
| 16 | +if (!isset($opt['c'])) { |
| 17 | + exit("require -c [process_num]. ep: -c 100\n"); |
| 18 | +} |
| 19 | +if (!isset($opt['n'])) { |
| 20 | + exit("require -n [request_num]. ep: -n 10000\n"); |
| 21 | +} |
| 22 | +if (!isset($opt['s'])) { |
| 23 | + exit("require -s [server_url]. ep: -s tcp://127.0.0.1:9999\n"); |
| 24 | +} |
| 25 | +if (!isset($opt['f'])) { |
| 26 | + exit("require -f [test_function]. ep: -f websocket|http|tcp|udp|length\n"); |
| 27 | +} |
| 28 | + |
| 29 | +class CoBenchMarkTest |
| 30 | +{ |
| 31 | + protected $nConcurrency; |
| 32 | + protected $nRequest; |
| 33 | + protected $host; |
| 34 | + protected $port; |
| 35 | + |
| 36 | + protected $nRecvBytes = 0; |
| 37 | + protected $nSendBytes = 0; |
| 38 | + |
| 39 | + protected $requestCount = 0; |
| 40 | + protected $connectErrorCount = 0; |
| 41 | + |
| 42 | + protected $connectTime = 0; |
| 43 | + |
| 44 | + protected $startTime; |
| 45 | + protected $beginSendTime; |
| 46 | + protected $testMethod; |
| 47 | + |
| 48 | + protected $sentData = "hello world\n"; |
| 49 | + |
| 50 | + function __construct($opt) |
| 51 | + { |
| 52 | + $this->nConcurrency = $opt['c']; |
| 53 | + $this->nRequest = $opt['n']; |
| 54 | + $serv = parse_url($opt['s']); |
| 55 | + $this->host = $serv['host']; |
| 56 | + $this->port = $serv['port']; |
| 57 | + $this->testMethod = $opt['f']; |
| 58 | + |
| 59 | + //data length |
| 60 | + if (isset($opt['l']) and intval($opt['l']) > 0) { |
| 61 | + $this->setSentData(str_repeat('A', intval($opt['l']))); |
| 62 | + } |
| 63 | + |
| 64 | + if (!method_exists($this, $this->testMethod)) { |
| 65 | + throw new RuntimeException("method [{$this->testMethod}] is not exists."); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + function setSentData($data) |
| 70 | + { |
| 71 | + $this->sentData = $data; |
| 72 | + } |
| 73 | + |
| 74 | + protected function finish() |
| 75 | + { |
| 76 | + echo "============================================================\n"; |
| 77 | + echo " Swoole Version " . SWOOLE_VERSION . "\n"; |
| 78 | + echo "============================================================\n"; |
| 79 | + echo "{$this->requestCount}\tbenchmark tests is finished.\n"; |
| 80 | + echo "SendBytes:\t" . number_format($this->nSendBytes) . "\n"; |
| 81 | + echo "nReceBytes:\t" . number_format($this->nRecvBytes) . "\n"; |
| 82 | + echo "concurrency:\t" . $this->nConcurrency, "\n"; |
| 83 | + echo "connect failed:\t" . $this->connectErrorCount, "\n"; |
| 84 | + echo "request num:\t" . $this->nRequest, "\n"; |
| 85 | + $costTime = $this->format(microtime(true) - $this->startTime); |
| 86 | + echo "total time:\t" . ($costTime) . "\n"; |
| 87 | + if ($this->requestCount > 0) { |
| 88 | + echo "request per second:\t" . intval($this->requestCount / $costTime), "\n"; |
| 89 | + } else { |
| 90 | + echo "request per second:\t0\n"; |
| 91 | + } |
| 92 | + echo "connection time:\t" . $this->format($this->connectTime) . "\n"; |
| 93 | + } |
| 94 | + |
| 95 | + function format($time) |
| 96 | + { |
| 97 | + return round($time, 4); |
| 98 | + } |
| 99 | + |
| 100 | + function websocket() |
| 101 | + { |
| 102 | + $cli = new Swoole\Coroutine\http\client($this->host, $this->port); |
| 103 | + $cli->set(array('websocket_mask' => true)); |
| 104 | + $cli->upgrade('/'); |
| 105 | + $n = $this->nRequest / $this->nConcurrency; |
| 106 | + while ($n--) { |
| 107 | + //requset |
| 108 | + $data = $this->sentData; |
| 109 | + $cli->push($data); |
| 110 | + $this->nSendBytes += strlen($data); |
| 111 | + $this->requestCount++; |
| 112 | + //response |
| 113 | + $frame = $cli->recv(); |
| 114 | + $this->nRecvBytes += strlen($frame->data); |
| 115 | + } |
| 116 | + $cli->close(); |
| 117 | + } |
| 118 | + |
| 119 | + function eof() |
| 120 | + { |
| 121 | + $eof = "\r\n\r\n"; |
| 122 | + $cli = new Coroutine\Client(SWOOLE_TCP); |
| 123 | + $cli->set(array('open_eof_check' => true, "package_eof" => $eof)); |
| 124 | + $cli->connect($this->host, $this->port); |
| 125 | + $n = $this->nRequest / $this->nConcurrency; |
| 126 | + while ($n--) { |
| 127 | + //requset |
| 128 | + $data = $this->sentData . $eof; |
| 129 | + $cli->send($data); |
| 130 | + $this->nSendBytes += strlen($data); |
| 131 | + $this->requestCount++; |
| 132 | + //response |
| 133 | + $rdata = $cli->recv(); |
| 134 | + $this->nRecvBytes += strlen($rdata); |
| 135 | + } |
| 136 | + $cli->close(); |
| 137 | + } |
| 138 | + |
| 139 | + function length() |
| 140 | + { |
| 141 | + $cli = new Coroutine\Client(SWOOLE_TCP); |
| 142 | + $cli->set(array( |
| 143 | + 'open_length_check' => true, |
| 144 | + "package_length_type" => 'N', |
| 145 | + 'package_body_offset' => 4, |
| 146 | + )); |
| 147 | + $cli->connect($this->host, $this->port); |
| 148 | + $n = $this->nRequest / $this->nConcurrency; |
| 149 | + while ($n--) { |
| 150 | + //requset |
| 151 | + $data = pack('N', strlen($this->sentData)) . $this->sentData; |
| 152 | + $cli->send($data); |
| 153 | + $this->nSendBytes += strlen($data); |
| 154 | + $this->requestCount++; |
| 155 | + //response |
| 156 | + $rdata = $cli->recv(); |
| 157 | + $this->nRecvBytes += strlen($rdata); |
| 158 | + } |
| 159 | + $cli->close(); |
| 160 | + } |
| 161 | + |
| 162 | + function run() |
| 163 | + { |
| 164 | + $this->startTime = microtime(true); |
| 165 | + for ($i = 0; $i < $this->nConcurrency; $i++) { |
| 166 | + go(function () { |
| 167 | + call_user_func([$this, $this->testMethod]); |
| 168 | + }); |
| 169 | + } |
| 170 | + $this->beginSendTime = microtime(true); |
| 171 | + $this->connectTime = $this->beginSendTime - $this->startTime; |
| 172 | + swoole_event::wait(); |
| 173 | + $this->finish(); |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +$bench = new CoBenchMarkTest($opt); |
| 178 | +$bench->setSentData(str_repeat('A', 1024)); |
| 179 | +$bench->run(); |
0 commit comments