-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 010f99b
Showing
56 changed files
with
3,033 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
tags | ||
vendor | ||
#composer.phar | ||
composer.lock | ||
*.log | ||
.pid |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
$lib = __DIR__ . '/dist/libserverbench.phar'; | ||
$bin = __DIR__ . '/dist/serverbench.phar'; | ||
@unlink($lib); | ||
@unlink($bin); | ||
|
||
$phar = new Phar($lib); | ||
$phar->startBuffering(); | ||
$phar->buildFromDirectory(__DIR__, '$(src|vendor)/.*|(logo|version)\.txt$'); | ||
// $phar->compressFiles(Phar::GZ); | ||
$phar->setStub($phar->createDefaultStub('./vendor/autoload.php')); | ||
$phar->stopBuffering(); | ||
|
||
$phar = new Phar($bin); | ||
$phar->startBuffering(); | ||
$phar->buildFromDirectory(__DIR__, '$(src|vendor)/.*|(logo|version)\.txt$'); | ||
// $phar->compressFiles(Phar::GZ); | ||
$phar->setStub($phar->createDefaultStub('./src/ServerBench/cli/cli.php')); | ||
$phar->stopBuffering(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"name": "serverbench/serverbench", | ||
"description": "a serverbench implemented using php", | ||
"keywords": ["ipc", "rpc", "net", "concurrent"], | ||
"require": { | ||
"php": ">=5.4.0", | ||
"ext-posix": "*", | ||
"ext-pcntl": "*", | ||
"ext-Phar": "*", | ||
"psr/log": "1.0.0" | ||
}, | ||
"autoload": { | ||
"psr-4": {"ServerBench\\": "src/ServerBench"}, | ||
"files": [ | ||
"src/ServerBench/helpers.php" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#!/bin/bash | ||
if [[ $# -lt 1 ]]; then | ||
echo 'usage: apputil {start|stop|ps}' | ||
fi | ||
|
||
app_dir="$(cd "$(dirname "$0")" && pwd -P)" | ||
cd $app_dir | ||
|
||
if [[ $1 = "start" ]] | ||
then | ||
php ./serverbench.phar --pidfile=$app_dir/pid --dir=$app_dir --app=$app_dir/app.php "${@:2}" | ||
elif [[ $1 = "stop" ]] | ||
then | ||
php ./dist/serverbench.phar --stop --pidfile=$app_dir/pid --dir=$app_dir | ||
elif [[ $1 = "ps" ]] | ||
php ../../dist/serverbench.phar --status --pidfile=$app_dir/pid --dir=$app_dir | ||
then | ||
if [ -f './pid' ] | ||
then | ||
echo -e "\e[0;32m" | ||
pid=`cat pid` | ||
ps -o pid,ppid,pgid,sess,tt,user,start_time,time,stat,%cpu,rss,vsz,size,%mem,cmd --pid $pid --ppid $pid | ||
echo -e "\e[0m" | ||
else | ||
echo -e "\e[0;31m" | ||
echo "no any running process found!" | ||
echo -e "\e[0m" | ||
fi | ||
fi |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
ini_set('error_reporting', E_ALL | E_STRICT); | ||
ini_set('display_errors', 1); | ||
assert_options(ASSERT_ACTIVE, 1); | ||
|
||
require dirname(dirname(__DIR__)) . '/dist/libserverbench.phar'; | ||
|
||
use ServerBench\App\Client\Client as ClientApp; | ||
|
||
function mt() | ||
{ | ||
return gettimeofday(true) * 1000; | ||
} | ||
|
||
$app = new ClientApp(); | ||
$app->connect('tcp://127.0.0.1:12345', new ServerBench\Codec\Json(), false); | ||
|
||
$start = mt(); | ||
$msg = <<<M | ||
1234567890 | ||
1234567890 | ||
1234567890 | ||
1234567890 | ||
1234567890 | ||
1234567890 | ||
1234567890 | ||
1234567890 | ||
1234567890 | ||
1234567890 | ||
M; | ||
|
||
for ($i = 0; $i < 1000; ++$i) { | ||
$rc = $app->send(['data' => $msg, 'seq' => $i]); | ||
|
||
if (false === $rc) { | ||
echo 'failed to send msg. ', $app->errno(), "\n"; | ||
die(); | ||
} | ||
|
||
$reply = $app->recv(); | ||
|
||
if (false === $reply) { | ||
echo 'failed to recv msg. ', $app->errno(), "\n"; | ||
die(); | ||
} | ||
|
||
assert($reply['seq'] == $i); | ||
} | ||
|
||
$end = mt(); | ||
$delta = $end - $start; | ||
$speed = 1000 / $delta * 1000; | ||
|
||
echo "use {$delta} ms, speed {$speed} pkgs/s \n"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<?php | ||
ini_set('error_reporting', E_ALL | E_STRICT); | ||
ini_set('display_errors', 1); | ||
assert_options(ASSERT_ACTIVE, 1); | ||
|
||
require dirname(dirname(__DIR__)) . '/dist/libserverbench.phar'; | ||
|
||
use ServerBench\App\Client\Client as ClientApp; | ||
use ServerBench\App\Client\Poller; | ||
use ServerBench\Base\CliArguments; | ||
|
||
$arguments = new CliArguments([ | ||
'c:' => 'concurrent:', | ||
'C:' => 'connect:', | ||
'T:' => 'time:', | ||
'L:' => 'length:' | ||
]); | ||
|
||
function mt() | ||
{ | ||
return gettimeofday(true) * 1000; | ||
} | ||
|
||
if (!isset($arguments['concurrent']) && | ||
!isset($arguments['connect']) && | ||
!isset($arguments['time']) | ||
) { | ||
die("usage: benchmark.php -c {num of clients} -C {address} -T {timed testing}"); | ||
} | ||
|
||
define('NUM_OF_CLIENTS', $arguments->get('concurrent')); | ||
define('CONNECT', $arguments->get('connect')); | ||
define('TIME_TO_TESTING', $arguments->get('time') * 1000); | ||
define('LENGTH', $arguments->get('length', 100)); | ||
|
||
printf( | ||
"clients %d, connect %s, time to testing %f sec, length %d\n", | ||
NUM_OF_CLIENTS, | ||
CONNECT, | ||
TIME_TO_TESTING / 1000, | ||
LENGTH | ||
); | ||
|
||
// ready | ||
$recv = []; | ||
$clients = []; | ||
$poller = new Poller(); | ||
|
||
$msg = str_repeat('-', LENGTH); | ||
|
||
for ($i = 0; $i < NUM_OF_CLIENTS; ++$i) { | ||
$client = new ClientApp(); | ||
$client->setSndHwm(1000000); | ||
$client->setRcvHwm(1000000); | ||
$client->connect(CONNECT, null, true); | ||
$client->setRecvTimeout(200); | ||
$client->setSendTimeout(200); | ||
$id = $poller->registerReadable($client); | ||
$clients[$id] = $client; | ||
$recv[$id] = 0; | ||
} | ||
|
||
$sending = 0; | ||
$recving = 0; | ||
|
||
$start = mt(); | ||
|
||
foreach ($clients as $id => $client) { | ||
$client->send($msg); | ||
} | ||
|
||
while (mt() - $start < TIME_TO_TESTING) { | ||
$rset = []; | ||
$wset = []; | ||
|
||
$events = $poller->poll($rset, $wset, 0); | ||
|
||
if ($events > 0) { | ||
foreach ($rset as $id) { | ||
$client = $clients[$id]; | ||
$msg = $client->recv(); | ||
|
||
if (isset($msg) && $client->errno()) { | ||
echo 'failed to recv msg. ', $client->errstr(), "\n"; | ||
die(); | ||
} | ||
|
||
++$recv[$id]; | ||
$client->send($msg); | ||
} | ||
} | ||
} | ||
|
||
$end = mt(); | ||
|
||
$delta = $end - $start; | ||
$total = array_sum($recv); | ||
$max = max($recv); | ||
$min = min($recv); | ||
|
||
$speed_total = $total / $delta * 1000; | ||
$speed_avg = $speed_total / NUM_OF_CLIENTS; | ||
$speed_high = $max / $delta * 1000; | ||
$speed_low = $min / $delta * 1000; | ||
|
||
echo "speed total {$speed_total} pkgs/sec\n"; | ||
echo "speed avg {$speed_avg} pkgs/sec\n"; | ||
echo "speed high {$speed_high} pkgs/sec\n"; | ||
echo "speed low {$speed_low} pkgs/sec\n"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
<?php | ||
ini_set('error_reporting', E_ALL | E_STRICT); | ||
ini_set('display_errors', 1); | ||
assert_options(ASSERT_ACTIVE, 1); | ||
|
||
require dirname(dirname(__DIR__)) . '/dist/libserverbench.phar'; | ||
|
||
use ServerBench\App\Client\Client as ClientApp; | ||
use ServerBench\App\Client\Multier; | ||
|
||
function mt() | ||
{ | ||
return gettimeofday(true) * 1000; | ||
} | ||
|
||
define('NUM_OF_CLIENTS', 200); | ||
define('NUM_OF_PACKAGES', 10000); | ||
|
||
$clients = []; | ||
|
||
for ($i = 0; $i < NUM_OF_CLIENTS; ++$i) { | ||
$client = new ClientApp(); | ||
$client->setSndHwm(1000000); | ||
$client->setRcvHwm(1000000); | ||
$client->connect('tcp://127.0.0.1:12345', new ServerBench\Codec\Json(), true); | ||
$client->setRecvTimeout(200); | ||
$client->setSendTimeout(200); | ||
$clients[] = $client; | ||
} | ||
|
||
$msg = <<<M | ||
1234567890 | ||
1234567890 | ||
1234567890 | ||
1234567890 | ||
1234567890 | ||
1234567890 | ||
1234567890 | ||
1234567890 | ||
1234567890 | ||
1234567890 | ||
M; | ||
|
||
$start = mt(); | ||
$sending = 0; | ||
$recving = 0; | ||
$m = new Multier(); | ||
$map = []; | ||
|
||
function doRecv($block = false) | ||
{ | ||
global $clients, $m, $cnt, $recving, $map; | ||
|
||
$reply = $m->fetch($clients, $block ? -1 : 0); | ||
|
||
if (false === $reply) { | ||
echo 'multier failed to recv msg. ', $m->errstr(), "\n"; | ||
die(); | ||
} | ||
|
||
if (empty($reply)) { | ||
return; | ||
} | ||
|
||
for ($i = 0; $i < NUM_OF_CLIENTS; ++$i) { | ||
if (!isset($reply[$i]) && $clients[$i]->errno()) { | ||
echo 'failed to recv msg. ', $clients[$i]->errstr(), "\n"; | ||
die(); | ||
} | ||
|
||
if (isset($reply[$i])) { | ||
unset($map[$reply[$i]['seq']]); | ||
++$recving; | ||
} | ||
} | ||
} | ||
|
||
while ($sending < NUM_OF_PACKAGES) { | ||
$j = $sending % NUM_OF_CLIENTS; | ||
$rc = $clients[$j]->send(['data' => $msg, 'seq' => $sending]); | ||
|
||
if (false === $rc) { | ||
printf( | ||
"failed to send msg. %d:%s\n", | ||
$clients[$j]->errno(), | ||
$clients[$j]->errstr() | ||
); | ||
die(); | ||
} else { | ||
$map[$sending] = 1; | ||
++$sending; | ||
} | ||
|
||
if ($sending % 10 == 0) { | ||
doRecv(); | ||
} | ||
} | ||
|
||
while ($sending > $recving) { | ||
printf("\rrecving(%d) < $sending(%d)", $recving, $sending); | ||
doRecv(true); | ||
} | ||
|
||
printf("\rrecving(%d) == $sending(%d)\n", $recving, $sending); | ||
|
||
$end = mt(); | ||
$delta = $end - $start; | ||
$speed = NUM_OF_PACKAGES / $delta * 1000; | ||
$mps = $speed * 100 / 1024 / 1024; | ||
|
||
echo "use {$delta} ms, speed {$speed} pkgs/sec, {$mps} mb/sec\n"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
require dirname(dirname(__DIR__)) . '/dist/libserverbench.phar'; | ||
|
||
$server = new \ServerBench\App\Server\Server('tcp://127.0.0.1:12345', function ($msg) { | ||
return $msg; | ||
}); | ||
|
||
$server->setProcessNum(10); | ||
$server->run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
daemonize=0 | ||
listen=tcp://127.0.0.1:12345 | ||
workers=8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
class App | ||
{ | ||
public function init() | ||
{ | ||
return true; | ||
} | ||
|
||
public function fini() | ||
{ | ||
return true; | ||
} | ||
|
||
public function process($msg) | ||
{ | ||
return $msg; | ||
} | ||
} | ||
|
||
return new App; |
Oops, something went wrong.