Skip to content

Instantly share code, notes, and snippets.

@jmikola
Created December 15, 2016 20:35
Show Gist options
  • Save jmikola/4d1ed854cfc5ae8ac014debc161e64f6 to your computer and use it in GitHub Desktop.
Save jmikola/4d1ed854cfc5ae8ac014debc161e64f6 to your computer and use it in GitHub Desktop.
Benchmarking buffering via php://memory and raw strings
<?php
// composer require phpbench/phpbench @dev --dev
// vendor/bin/phpbench run MemoryStreamBench.php --report='generator: "table", compare: "revs", cols: ["subject", "mean"]'
/**
* @Revs(1000)
* @Iterations(5)
*/
class MemoryStreamBench
{
/**
* @ParamProviders({"provideData"})
*/
public function benchMemoryStream($params)
{
$buffer = fopen('php://memory', 'w+b');
$bufferLength = 0;
$chunkSize = $params['chunkSize'] ?? 261120;
$data = $params['data'];
$bytesRead = 0;
while ($bytesRead != strlen($data)) {
$bytesWritten = fwrite($buffer, substr($data, $bytesRead, $chunkSize - $bufferLength));
if ($bytesWritten === false) {
throw new \RuntimeException('fwrite() to buffer failed');
}
$bytesRead += $bytesWritten;
$bufferLength += $bytesWritten;
if ($bufferLength == $chunkSize) {
// insertChunkFromBuffer
stream_get_contents($buffer, -1, 0);
if (ftruncate($buffer, 0) === false) {
throw new \RuntimeException('ftruncate() on buffer failed');
}
$bufferLength = 0;
}
}
}
/**
* @ParamProviders({"provideData"})
*/
public function benchString($params)
{
$buffer = '';
$chunkSize = $params['chunkSize'] ?? 261120;
$data = $params['data'];
$bytesRead = 0;
while ($bytesRead != strlen($data)) {
$buffer = substr($data, $bytesRead, $chunkSize);
$bytesRead += strlen($buffer);
if (strlen($buffer) == $chunkSize) {
// insertChunkFromBuffer
$buffer = '';
}
}
}
public function provideData()
{
return [
['chunkSize' => 2048, 'data' => str_repeat('a', 1024)],
['chunkSize' => 2048, 'data' => str_repeat('a', 1024 * 1024)],
['chunkSize' => 2048, 'data' => str_repeat('a', 1024 * 1024 * 4)],
['chunkSize' => 2048, 'data' => str_repeat('a', 1024 * 1024 * 16)],
['chunkSize' => 261120, 'data' => str_repeat('a', 1024)],
['chunkSize' => 261120, 'data' => str_repeat('a', 1024 * 1024)],
['chunkSize' => 261120, 'data' => str_repeat('a', 1024 * 1024 * 4)],
['chunkSize' => 261120, 'data' => str_repeat('a', 1024 * 1024 * 16)],
['chunkSize' => 1024 * 1024, 'data' => str_repeat('a', 1024)],
['chunkSize' => 1024 * 1024, 'data' => str_repeat('a', 1024 * 1024)],
['chunkSize' => 1024 * 1024, 'data' => str_repeat('a', 1024 * 1024 * 4)],
['chunkSize' => 1024 * 1024, 'data' => str_repeat('a', 1024 * 1024 * 16)],
];
}
}
@jmikola
Copy link
Author

jmikola commented Dec 15, 2016

$ php -v
PHP 7.0.14 (cli) (built: Dec 13 2016 14:54:26) ( NTS DEBUG )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies

$ vendor/bin/phpbench run MemoryStreamBench.php  --report='generator: "table", compare: "revs", cols: ["subject", "mean"]'
PhpBench 0.14-dev (@git_version@). Running benchmarks.

\MemoryStreamBench

    benchMemoryStream             I4 P0 	[μ Mo]/r: 4.802 4.964 (μs) 	[μSD μRSD]/r: 0.280μs 5.84%
    benchMemoryStream             I4 P1 	[μ Mo]/r: 826.111 833.984 (μs) 	[μSD μRSD]/r: 14.909μs 1.80%
    benchMemoryStream             I4 P2 	[μ Mo]/r: 2,741.491 2,723.992 (μs) 	[μSD μRSD]/r: 40.748μs 1.49%
    benchMemoryStream             I4 P3 	[μ Mo]/r: 10,884.818 10,894.684 (μs) 	[μSD μRSD]/r: 180.171μs 1.66%
    benchMemoryStream             I4 P4 	[μ Mo]/r: 4.758 4.550 (μs) 	[μSD μRSD]/r: 0.442μs 9.29%
    benchMemoryStream             I4 P5 	[μ Mo]/r: 319.989 322.461 (μs) 	[μSD μRSD]/r: 6.376μs 1.99%
    benchMemoryStream             I4 P6 	[μ Mo]/r: 1,013.766 1,049.257 (μs) 	[μSD μRSD]/r: 60.385μs 5.96%
    benchMemoryStream             I4 P7 	[μ Mo]/r: 4,023.551 4,002.493 (μs) 	[μSD μRSD]/r: 58.926μs 1.46%
    benchMemoryStream             I4 P8 	[μ Mo]/r: 4.807 4.940 (μs) 	[μSD μRSD]/r: 0.246μs 5.12%
    benchMemoryStream             I4 P9 	[μ Mo]/r: 467.682 459.870 (μs) 	[μSD μRSD]/r: 16.124μs 3.45%
    benchMemoryStream             I4 P10 	[μ Mo]/r: 2,832.080 2,716.718 (μs) 	[μSD μRSD]/r: 235.774μs 8.33%
    benchMemoryStream             I4 P11 	[μ Mo]/r: 13,610.550 13,452.113 (μs) 	[μSD μRSD]/r: 404.690μs 2.97%
    benchString                   I4 P0 	[μ Mo]/r: 1.699 1.858 (μs) 	[μSD μRSD]/r: 0.347μs 20.45%
    benchString                   I4 P1 	[μ Mo]/r: 251.848 251.008 (μs) 	[μSD μRSD]/r: 2.149μs 0.85%
    benchString                   I4 P2 	[μ Mo]/r: 855.365 877.956 (μs) 	[μSD μRSD]/r: 45.836μs 5.36%
    benchString                   I4 P3 	[μ Mo]/r: 3,510.707 3,503.826 (μs) 	[μSD μRSD]/r: 90.134μs 2.57%
    benchString                   I4 P4 	[μ Mo]/r: 1.765 1.875 (μs) 	[μSD μRSD]/r: 0.228μs 12.90%
    benchString                   I4 P5 	[μ Mo]/r: 99.203 92.246 (μs) 	[μSD μRSD]/r: 9.580μs 9.66%
    benchString                   I4 P6 	[μ Mo]/r: 359.017 361.488 (μs) 	[μSD μRSD]/r: 6.438μs 1.79%
    benchString                   I4 P7 	[μ Mo]/r: 1,644.439 1,696.027 (μs) 	[μSD μRSD]/r: 72.995μs 4.44%
    benchString                   I4 P8 	[μ Mo]/r: 1.843 1.854 (μs) 	[μSD μRSD]/r: 0.129μs 7.02%
    benchString                   I4 P9 	[μ Mo]/r: 100.640 107.359 (μs) 	[μSD μRSD]/r: 10.243μs 10.18%
    benchString                   I4 P10 	[μ Mo]/r: 599.813 620.263 (μs) 	[μSD μRSD]/r: 39.458μs 6.58%
    benchString                   I4 P11 	[μ Mo]/r: 3,363.926 3,211.944 (μs) 	[μSD μRSD]/r: 312.150μs 9.28%

2 subjects, 120 iterations, 24,000 revs, 0 rejects
(best [mean mode] worst) = 1.060 [1,980.195 1,966.572] 2.037 (μs)
⅀T: 237,623.351μs μSD/r 67.032μs μRSD/r: 5.851%
suite: 133a2bf0f86024439c4390acb3f0df8f2ad7679f, date: 2016-12-15, stime: 15:21:54
+-------------------+----------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+-------------------+-------------------+
| subject           | revs:1000:mean | revs:1000:mean#1 | revs:1000:mean#2 | revs:1000:mean#3 | revs:1000:mean#4 | revs:1000:mean#5 | revs:1000:mean#6 | revs:1000:mean#7 | revs:1000:mean#8 | revs:1000:mean#9 | revs:1000:mean#10 | revs:1000:mean#11 |
+-------------------+----------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+-------------------+-------------------+
| benchMemoryStream | 4.802μs        | 826.111μs        | 2,741.491μs      | 10,884.818μs     | 4.758μs          | 319.989μs        | 1,013.766μs      | 4,023.551μs      | 4.807μs          | 467.682μs        | 2,832.080μs       | 13,610.550μs      |
| benchString       | 1.699μs        | 251.848μs        | 855.365μs        | 3,510.707μs      | 1.765μs          | 99.203μs         | 359.017μs        | 1,644.439μs      | 1.843μs          | 100.640μs        | 599.813μs         | 3,363.926μs       |
+-------------------+----------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+-------------------+-------------------+

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment