-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathbenchmark-attach.php
67 lines (54 loc) · 2.09 KB
/
benchmark-attach.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
62
63
64
65
66
67
<?php
// This example executes a command within a new container and displays how fast
// it can receive its output.
//
// $ php examples/benchmark-attach.php
// $ php examples/benchmark-attach.php busybox echo -n hello
//
// Expect this to be noticeably faster than the (totally unfair) equivalent:
//
// $ docker run -i --rm --log-driver=none busybox dd if=/dev/zero bs=1M count=1000 status=none | dd of=/dev/null
use React\EventLoop\Loop;
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;
}
$image = 'busybox';
$cmd = array('dd', 'if=/dev/zero', 'bs=1M', 'count=1000', 'status=none');
if (isset($argv[1])) {
$image = $argv[1];
$cmd = array_slice($argv, 2);
}
$client = new Clue\React\Docker\Client();
$client->containerCreate(array(
'Image' => $image,
'Cmd' => $cmd,
'Tty' => false,
'HostConfig' => array(
'LogConfig' => array(
'Type' => 'none'
)
)
))->then(function ($container) use ($client) {
$stream = $client->containerAttachStream($container['Id'], false, true);
// we're creating the container without a log, so first wait for attach stream before starting
Loop::addTimer(0.1, function () use ($client, $container) {
$client->containerStart($container['Id'])->then(null, 'printf');
});
$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, $container) {
$time = microtime(true) - $start;
$client->containerRemove($container['Id'])->then(null, 'printf');
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;
});