-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
benchmark-exec.php
53 lines (42 loc) · 1.64 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
46
47
48
49
50
51
52
53
<?php
// This example executes a command within the given running container and
// displays how fast it can receive its output.
//
// Before starting the benchmark, you have to start a container first, such as:
//
// $ docker run -it --rm --name=foo busybox sh
// $ php examples/benchmark-exec.php
// $ php examples/benchmark-exec.php foo echo -n hello
//
// Expect this to be significantly faster than the (totally unfair) equivalent:
//
// $ docker exec foo dd if=/dev/zero bs=1M count=1000 | dd of=/dev/null
require __DIR__ . '/../vendor/autoload.php';
if (extension_loaded('xdebug')) {
echo 'NOTICE: The "xdebug" extension is loaded, this has a major impact on performance.' . PHP_EOL;
}
$container = 'foo';
$cmd = array('dd', 'if=/dev/zero', 'bs=1M', 'count=1000');
if (isset($argv[1])) {
$container = $argv[1];
$cmd = array_slice($argv, 2);
}
$client = new Clue\React\Docker\Client();
$client->execCreate($container, $cmd)->then(function ($info) use ($client) {
$stream = $client->execStartStream($info['Id'], true);
$bytes = 0;
$stream->on('data', function ($chunk) use (&$bytes) {
$bytes += strlen($chunk);
});
$stream->on('error', function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});
// show stats when stream ends
$start = microtime(true);
$stream->on('close', function () use ($client, &$bytes, $start) {
$time = microtime(true) - $start;
echo 'Received ' . $bytes . ' bytes in ' . round($time, 1) . 's => ' . round($bytes / $time / 1000000, 1) . ' MB/s' . PHP_EOL;
});
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});