Skip to content

Commit 52e3367

Browse files
committed
add tests
1 parent a7d5524 commit 52e3367

File tree

2 files changed

+267
-0
lines changed

2 files changed

+267
-0
lines changed

benchmark/co_run.php

+179
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
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();

tests/swoole_server/send_3.phpt

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
--TEST--
2+
swoole_server: send big packet [3]
3+
--SKIPIF--
4+
<?php
5+
require __DIR__ . '/../include/skipif.inc';
6+
?>
7+
--FILE--
8+
<?php
9+
require __DIR__ . '/../include/bootstrap.php';
10+
11+
const REQ_N = 64;
12+
const CLIENT_N = 8;
13+
14+
$pm = new SwooleTest\ProcessManager;
15+
16+
$pm->parentFunc = function ($pid) use ($pm) {
17+
$total = 0;
18+
for ($i = 0; $i < CLIENT_N; $i++) {
19+
go(function () use ($pm, $i, &$total) {
20+
$cli = new Co\Client(SWOOLE_SOCK_TCP);
21+
$cli->set([
22+
'open_length_check' => true,
23+
'package_max_length' => 4 * 1024 * 1024,
24+
'package_length_type' => 'N',
25+
'package_length_offset' => 0,
26+
'package_body_offset' => 4,
27+
]);
28+
if ($cli->connect('127.0.0.1', $pm->getFreePort(), 100) == false) {
29+
echo "ERROR\n";
30+
return;
31+
}
32+
$n = REQ_N;
33+
while ($n--) {
34+
$data = $cli->recv();
35+
Assert::assert($data);
36+
$char = chr(ord('A') + $n % 10);
37+
$info = unpack('Nlen', substr($data, 0, 4));
38+
39+
// echo "c=$i, n=$n, len={$info['len']}\n---------------------------------------------------------------------\n";
40+
Assert::eq($info['len'], strlen($data) - 4);
41+
Assert::eq(str_repeat($char, 1024), substr($data, rand(4, $info['len'] - 1024 - 4), 1024));
42+
$total += strlen($data);
43+
}
44+
});
45+
}
46+
swoole_event::wait();
47+
echo $total . " bytes\n";
48+
$pm->kill();
49+
};
50+
51+
$pm->childFunc = function () use ($pm) {
52+
$serv = new Swoole\Server('127.0.0.1', $pm->getFreePort(), SWOOLE_PROCESS);
53+
$serv->set(array(
54+
'reactor_num' => 1,
55+
"worker_num" => 4,
56+
'log_level' => SWOOLE_LOG_ERROR,
57+
'open_length_check' => true,
58+
'package_max_length' => 4 * 1024 * 1024,
59+
'package_length_type' => 'N',
60+
'package_length_offset' => 0,
61+
'package_body_offset' => 4,
62+
'send_yield' => true,
63+
));
64+
$serv->on("WorkerStart", function (Swoole\Server $serv) use ($pm) {
65+
$pm->wakeup();
66+
});
67+
$serv->on('connect', function (Swoole\Server $serv, $fd, $rid) {
68+
$n = REQ_N;
69+
while ($n--) {
70+
$len = rand(65536, 1024 * 1024);
71+
$send_data = str_repeat(chr(ord('A') + $n % 10), $len);
72+
$retval = $serv->send($fd, pack('N', $len) . $send_data);
73+
if ($retval === false) {
74+
echo "send error, code=".swoole_last_error()."\n";
75+
}
76+
}
77+
});
78+
$serv->on('receive', function (Swoole\Server $serv, $fd, $rid, $data) {
79+
80+
});
81+
$serv->start();
82+
};
83+
84+
$pm->childFirst();
85+
$pm->run();
86+
?>
87+
--EXPECTF--
88+
%d bytes

0 commit comments

Comments
 (0)