-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
91-benchmark-compress.php
61 lines (50 loc) · 1.75 KB
/
91-benchmark-compress.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
// This benchmarking example reads a stream of dummy data and displays how fast
// it can be compressed.
//
// You can run the benchmark like this:
//
// $ php examples/91-benchmark-compress.php
//
// This runs the equivalent of:
//
// $ dd if=/dev/zero bs=1M count=1k status=progress | php examples/gzip.php > /dev/null
//
// Expect this to be only slightly slower than the equivalent:
//
// $ dd if=/dev/zero bs=1M count=1k status=progress | gzip > /dev/null
use React\EventLoop\Loop;
require __DIR__ . '/../vendor/autoload.php';
if (DIRECTORY_SEPARATOR === '\\') {
fwrite(STDERR, 'Non-blocking console I/O not supported on Windows' . PHP_EOL);
exit(1);
}
if (extension_loaded('xdebug')) {
echo 'NOTICE: The "xdebug" extension is loaded, this has a major impact on performance.' . PHP_EOL;
}
// read 1 MiB * 1 Ki times
$count = 0;
$stream = new React\Stream\ReadableResourceStream(fopen('/dev/zero', 'r'), null, 1024*1024);
$stream->on('data', function () use (&$count, $stream) {
if (++$count > 1024) {
$stream->close();
}
});
$compressor = new Clue\React\Zlib\Compressor(ZLIB_ENCODING_GZIP);
$stream->pipe($compressor);
// count number of input bytes before compression
$bytes = 0;
$stream->on('data', function ($chunk) use (&$bytes) {
$bytes += strlen($chunk);
});
// report progress periodically
$timer = Loop::addPeriodicTimer(0.05, function () use (&$bytes) {
echo "\rCompressed $bytes bytes…";
});
// report results once the stream closes
$start = microtime(true);
$stream->on('close', function () use (&$bytes, $start, $timer) {
$time = microtime(true) - $start;
Loop::cancelTimer($timer);
echo "\rCompressed $bytes bytes in " . round($time, 1) . 's => ' . round($bytes / $time / 1000000, 1) . ' MB/s' . PHP_EOL;
});