Created
December 15, 2016 20:35
-
-
Save jmikola/4d1ed854cfc5ae8ac014debc161e64f6 to your computer and use it in GitHub Desktop.
Benchmarking buffering via php://memory and raw strings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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)], | |
]; | |
} | |
} |
Author
jmikola
commented
Dec 15, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment