forked from clue/reactphp-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark-exec.php
45 lines (33 loc) · 1.39 KB
/
benchmark-exec.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
<?php
// this simple example executes a command within the given running container and
// displays how fast it can receive its output.
// expect this to be significantly faster than the (totally unfair) equivalent:
// $ docker exec asd dd if=/dev/zero bs=1M count=1000 | dd of=/dev/null
require __DIR__ . '/../vendor/autoload.php';
use React\EventLoop\Factory as LoopFactory;
use Clue\React\Docker\Factory;
use React\Stream\Stream;
$container = 'asd';
$cmd = array('dd', 'if=/dev/zero', 'bs=1M', 'count=1000');
if (isset($argv[1])) {
$container = $argv[1];
$cmd = array_slice($argv, 2);
}
$loop = LoopFactory::create();
$factory = new Factory($loop);
$client = $factory->createClient();
$client->execCreate($container, array('Cmd' => $cmd, 'AttachStdout' => true, 'AttachStderr' => true))->then(function ($info) use ($client) {
$stream = $client->execStartStream($info['Id'], array('Tty' => true));
$start = microtime(true);
$bytes = 0;
$stream->on('data', function ($chunk) use (&$bytes) {
$bytes += strlen($chunk);
});
$stream->on('error', 'printf');
// show stats when stream ends
$stream->on('close', function () use ($client, &$bytes, $start) {
$time = microtime(true) - $start;
echo 'Received ' . $bytes . ' bytes in ' . round($time, 1) . 's => ' . round($bytes / $time / 1024 / 1024, 1) . ' MiB/s' . PHP_EOL;
});
}, 'printf');
$loop->run();