Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Benchmark caches in file
<?php
/*
* (c) Antal Áron <antalaron@antalaron.hu>
*
* Under MIT license.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
namespace Antalaron\Benchmark;
/**
* BenchmarkCache.
*
* @author Antal Áron <antalaron@antalaron.hu>
*/
class BenchmarkCache
{
private static $methods = [
'encode',
'decode',
];
private static $functions = [
'json',
'serialize',
'export',
];
public function method($method, $function)
{
if (!in_array($method, static::$methods)) {
throw new \InvalidArgumentException(sprintf('No method "%s". Possible values: "%s".', $method, implode('", "', static::$methods)));
}
if (!in_array($function, static::$functions)) {
throw new \InvalidArgumentException(sprintf('No function "%s". Possible values: "%s".', $function, implode('", "', static::$functions)));
}
$this->{$method}($function);
}
public static function printHelp($argv)
{
echo "usage: {$argv[0]} <method> <function>\n";
exit;
}
private function encode($function)
{
$array = array_fill(0, 1000000, rand(1, 9999));
$start = microtime(true);
$export = $this->{'encode'.ucfirst($function)}($array);
file_put_contents($function.'.tmp', $export);
$end = microtime(true);
$duration = $end - $start;
$this->dump('encode', $function, $duration);
}
private function decode($function)
{
$method = 'decode'.ucfirst($function);
if (method_exists($this, 'decodeCustom'.ucfirst($function))) {
$method = 'decodeCustom'.ucfirst($function);
$start = microtime(true);
$result = $this->{$method}();
$end = microtime(true);
} else {
$start = microtime(true);
$import = file_get_contents($function.'.tmp');
$result = $this->{$method}($import);
$end = microtime(true);
}
$duration = $end - $start;
$this->dump('decode', $function, $duration);
}
private function encodeJson($array)
{
return json_encode($array);
}
private function decodeJson($json)
{
return json_decode($json);
}
private function encodeSerialize($array)
{
return serialize($array);
}
private function decodeSerialize($data)
{
return unserialize($data);
}
private function encodeExport($array)
{
return '<?php return '.var_export($array, true).';';
}
private function decodeExport($php)
{
return eval($php);
}
private function decodeCustomExport()
{
$data = include 'export.tmp';
return $data;
}
private function dump($method, $function, $time)
{
echo $method.' ('.$function.'): '.$time."\n";
}
}
if (count($argv) < 3) {
BenchmarkCache::printHelp($argv);
}
$benchmark = new BenchmarkCache();
try {
$benchmark->method($argv[1], $argv[2]);
} catch (\Exception $e) {
echo $e->getMessage()."\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.