diff --git a/examples/91-benchmark-compress.php b/examples/91-benchmark-compress.php new file mode 100644 index 0000000..7a0c31b --- /dev/null +++ b/examples/91-benchmark-compress.php @@ -0,0 +1,69 @@ + /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 + +require __DIR__ . '/../vendor/autoload.php'; + +if (DIRECTORY_SEPARATOR === '\\') { + fwrite(STDERR, 'Non-blocking console I/O not supported on Windows' . PHP_EOL); + exit(1); +} + +if (!defined('ZLIB_ENCODING_GZIP')) { + fwrite(STDERR, 'Requires PHP 5.4+ with ext-zlib enabled' . PHP_EOL); + exit(1); +} + + +if (extension_loaded('xdebug')) { + echo 'NOTICE: The "xdebug" extension is loaded, this has a major impact on performance.' . PHP_EOL; +} + +$loop = React\EventLoop\Factory::create(); + +// read 1 MiB * 1 Ki times +$count = 0; +$stream = new React\Stream\ReadableResourceStream(fopen('/dev/zero', 'r'), $loop, 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, $loop, $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; +}); + +$loop->run(); diff --git a/examples/92-benchmark-decompress.php b/examples/92-benchmark-decompress.php new file mode 100644 index 0000000..a8a06ba --- /dev/null +++ b/examples/92-benchmark-decompress.php @@ -0,0 +1,70 @@ + null.gz +// +// You can run the benchmark like this: +// +// $ php examples/92-benchmark-decompress.php null.gz +// +// Expect this to be slightly faster than the (totally unfair) equivalent: +// +// $ gunzip < null.gz | dd of=/dev/null status=progress +// +// Expect this to be somewhat faster than: +// +// $ php examples/gunzip.php < null.gz | dd of=/dev/zero status=progress + +require __DIR__ . '/../vendor/autoload.php'; + +if (DIRECTORY_SEPARATOR === '\\') { + fwrite(STDERR, 'Non-blocking console I/O not supported on Windows' . PHP_EOL); + exit(1); +} + +if (!defined('ZLIB_ENCODING_GZIP')) { + fwrite(STDERR, 'Requires PHP 5.4+ with ext-zlib enabled' . PHP_EOL); + exit(1); +} + +if ($argc !== 2) { + fwrite(STDERR, 'No archive given, requires single argument' . PHP_EOL); + exit(1); +} + +if (extension_loaded('xdebug')) { + echo 'NOTICE: The "xdebug" extension is loaded, this has a major impact on performance.' . PHP_EOL; +} + +$loop = React\EventLoop\Factory::create(); + +$in = new React\Stream\ReadableResourceStream(fopen($argv[1], 'r'), $loop); +$stream = new Clue\React\Zlib\Decompressor(ZLIB_ENCODING_GZIP); +$in->pipe($stream); + +$bytes = 0; +$stream->on('data', function ($chunk) use (&$bytes) { + $bytes += strlen($chunk); +}); + +$stream->on('error', 'printf'); + +//report progress periodically +$timer = $loop->addPeriodicTimer(0.2, function () use (&$bytes) { + echo "\rDecompressed $bytes bytes…"; +}); + +// show stats when stream ends +$start = microtime(true); +$stream->on('close', function () use (&$bytes, $start, $loop, $timer) { + $time = microtime(true) - $start; + $loop->cancelTimer($timer); + + echo "\rDecompressed $bytes bytes in " . round($time, 1) . 's => ' . round($bytes / $time / 1000000, 1) . ' MB/s' . PHP_EOL; +}); + +$loop->run();