-
-
Notifications
You must be signed in to change notification settings - Fork 156
/
Copy path91-benchmark-server.php
61 lines (53 loc) · 2.33 KB
/
91-benchmark-server.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
// Just start the server and connect to it. It will count the number of bytes
// sent for each connection and will print the average throughput once the
// connection closes.
//
// $ php examples/91-benchmark-server.php 127.0.0.1:8000
// $ telnet localhost 8000
// $ echo hello world | nc -N localhost 8000
// $ dd if=/dev/zero bs=1M count=1000 | nc -N localhost 8000
//
// You can also run a secure TLS benchmarking server like this:
//
// $ php examples/91-benchmark-server.php tls://127.0.0.1:8000 examples/localhost.pem
// $ openssl s_client -connect localhost:8000
// $ echo hello world | openssl s_client -connect localhost:8000
// $ dd if=/dev/zero bs=1M count=1000 | openssl s_client -connect localhost:8000
//
// You can also run a Unix domain socket (UDS) server benchmark like this:
//
// $ php examples/91-benchmark-server.php unix:///tmp/server.sock
// $ nc -N -U /tmp/server.sock
// $ dd if=/dev/zero bs=1M count=1000 | nc -N -U /tmp/server.sock
//
// You can also use systemd socket activation and listen on an inherited file descriptor:
//
// $ systemd-socket-activate -l 8000 php examples/91-benchmark-server.php php://fd/3
// $ telnet localhost 8000
// $ echo hello world | nc -N localhost 8000
// $ dd if=/dev/zero bs=1M count=1000 | nc -N localhost 8000
require __DIR__ . '/../vendor/autoload.php';
$socket = new React\Socket\SocketServer($argv[1] ?? '127.0.0.1:0', [
'tls' => [
'local_cert' => $argv[2] ?? __DIR__ . '/localhost.pem'
]
]);
$socket->on('connection', function (React\Socket\ConnectionInterface $connection) {
echo '[' . $connection->getRemoteAddress() . ' connected]' . PHP_EOL;
// count the number of bytes received from this connection
$bytes = 0;
$connection->on('data', function ($chunk) use (&$bytes) {
$bytes += strlen($chunk);
});
// report average throughput once client disconnects
$t = microtime(true);
$connection->on('close', function () use ($connection, $t, &$bytes) {
$t = microtime(true) - $t;
echo '[' . $connection->getRemoteAddress() . ' disconnected after receiving ' . $bytes . ' bytes in ' . round($t, 3) . 's => ' . round($bytes / $t / 1024 / 1024, 1) . ' MiB/s]' . PHP_EOL;
});
});
$socket->on('error', function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});
echo 'Listening on ' . $socket->getAddress() . PHP_EOL;