Skip to content

Add compression and decompression benchmarks #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions examples/91-benchmark-compress.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?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

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();
70 changes: 70 additions & 0 deletions examples/92-benchmark-decompress.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

// This benchmarking example reads a compressed file and displays how fast
// it can be decompressed.
//
// Before starting the benchmark, you have to create a (dummy) compressed file first, such as:
//
// $ dd if=/dev/zero bs=1M count=1k status=progress | gzip > 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();