Utility method to create tests on the fly
You may chain the test:
public add ( string $name, Closure $closure ) : SimpleTest | ||
$name | string | |
$closure | Closure | function to execute |
Résultat | SimpleTest |
function bench($value, $n = 1000000) { $benchmark = new Benchmark(); $benchmark->add('serialize', function () use(&$value) { serialize($value); }); $benchmark->add('json_encode', function () use(&$value) { json_encode($value); }); if (function_exists('bin_encode')) { $benchmark->add('bin_encode', function () use(&$value) { bin_encode($value); }); } if (function_exists('bson_encode')) { $benchmark->add('bson_encode', function () use(&$value) { bson_encode($value); }); } if (function_exists('msgpack_pack')) { $benchmark->add('msgpack_pack', function () use(&$value) { msgpack_pack($value); }); } if (function_exists('igbinary_serialize')) { $benchmark->add('igbinary_serialize', function () use(&$value) { igbinary_serialize($value); }); } $benchmark->add('var_export', function () use(&$value) { var_export($value, true); }); $benchmark->setCount($n); $benchmark->run(); }
function bench($value, $n = 1000000) { $benchmark = new Benchmark(); $serialized = serialize($value); $benchmark->add('unserialize', function () use(&$serialized) { unserialize($serialized); }); $jsonEncoded = json_encode($value); $benchmark->add('json_decode', function () use(&$jsonEncoded) { json_decode($jsonEncoded); }); if (function_exists('bin_decode')) { $binEncoded = bin_encode($value); $benchmark->add('bin_decode', function () use(&$binEncoded) { bin_decode($binEncoded); }); } if (function_exists('bson_decode')) { $bsonEncoded = bson_encode($value); $benchmark->add('bson_decode', function () use(&$bsonEncoded) { bson_decode($bsonEncoded); }); } if (function_exists('msgpack_pack')) { $msgPack = msgpack_pack($value); $benchmark->add('msgpack_unpack', function () use(&$msgPack) { msgpack_unpack($msgPack); }); } if (function_exists('igbinary_unserialize')) { $igbinarySerialized = igbinary_serialize($value); $benchmark->add('igbinary_unserialize', function () use(&$igbinarySerialized) { igbinary_unserialize($igbinarySerialized); }); } $benchmark->setCount($n); $benchmark->run(); }
<?php /** * JSON と MessagePack のデコード速度を比較 * * @link http://www.lifehacker.jp/2015/08/150808mockaroo_1000.html ダミーデータ * @link https://github.com/msgpack/msgpack-php msgpack */ require __DIR__ . DIRECTORY_SEPARATOR . './vendor/autoload.php'; use Lavoiesl\PhpBenchmark\Benchmark; $data = (include __DIR__ . DIRECTORY_SEPARATOR . './data/mock_data.php'); $json = file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . './data/MOCK_DATA.json'); $msgpack = file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . './data/mock_data.msgpack'); $php = serialize($data); $benchmark = new Benchmark(); $benchmark->add('json', function () use($json) { return json_decode($json); }); $benchmark->add('messagepack', function () use($msgpack) { return msgpack_pack($msgpack); }); $benchmark->add('php', function () use($php) { return unserialize($php); }); $benchmark->run();
<?php use Lavoiesl\PhpBenchmark\Benchmark; require_once '_autoload.php'; $benchmark = new Benchmark(); // @link http://www.php.net/manual/en/control-structures.declare.php#control-structures.declare.ticks declare (ticks=1); $benchmark->add('1024 * 256', function () { return str_repeat('a', 1024 * 256); }); $benchmark->add('1024 * 1024', function () { return str_repeat('a', 1024 * 1024); }); $benchmark->add('1024 * 1024 * 16', function () { return str_repeat('a', 1024 * 1024 * 16); }); $benchmark->guessCount(2); // aim for 2 seconds per test (default) $benchmark->run();
<?php /** * Created by PhpStorm. * User: ovr * Date: 06.12.15 * Time: 22:26 */ use Lavoiesl\PhpBenchmark\Benchmark; use Ovr\Bench\AssignOrNot\ReturnAfterAssignProperty; use Ovr\Bench\AssignOrNot\ReturnWithoutAfterAssignProperty; include_once __DIR__ . '/vendor/autoload.php'; $benchmark = new Benchmark(); $benchmark->add('rand', function () { rand(0, getrandmax()); }); $benchmark->add('mt_rand', function () { mt_rand(0, getrandmax()); }); $benchmark->add('random_int', function () { random_int(0, getrandmax()); }); $benchmark->add('phpd-utils-crypt', function () { $gen = false; \PHPDaemon\Utils\Crypt::randomInts(1, function ($result) use(&$gen) { $gen = true; }); while (!$gen) { } }); $benchmark->setCount(100000);
<?php /** * Created by PhpStorm. * User: ovr * Date: 06.12.15 * Time: 22:26 */ use Lavoiesl\PhpBenchmark\Benchmark; use Ovr\Bench\AssignOrNot\ReturnAfterAssignProperty; use Ovr\Bench\AssignOrNot\ReturnWithoutAfterAssignProperty; include_once __DIR__ . '/vendor/autoload.php'; $benchmark = new Benchmark(); $benchmark->add('(bool) 1', function () use(&$serialized) { return (bool) 1; }); $benchmark->add('boolval(1)', function () use(&$serialized) { return boolval(1); }); $benchmark->setCount(1000000); $benchmark->run();
<?php /** * Created by PhpStorm. * User: ovr * Date: 06.12.15 * Time: 22:26 */ use Lavoiesl\PhpBenchmark\Benchmark; use Ovr\Bench\AssignOrNot\ReturnAfterAssignProperty; use Ovr\Bench\AssignOrNot\ReturnWithoutAfterAssignProperty; include_once __DIR__ . '/vendor/autoload.php'; $benchmark = new Benchmark(); $benchmark->add('md5', function () { md5("test"); }); $benchmark->add('sha1', function () { sha1("test"); }); $benchmark->add('sha256', function () { hash("sha256", "test"); }); $benchmark->add('sha384', function () { hash("sha384", "test"); }); $benchmark->add('sha512', function () { hash("sha512", "test"); }); $benchmark->add('haval128,3', function () { hash("haval128,3", "test"); });
<?php use Lavoiesl\PhpBenchmark\Benchmark; use Ovr\Bench\AssignOrNot\ReturnAfterAssignProperty; use Ovr\Bench\AssignOrNot\ReturnWithoutAfterAssignProperty; include_once __DIR__ . '/vendor/autoload.php'; $benchmark = new Benchmark(); $str = ''; for ($i = 0; $i < 1024; ++$i) { $str .= chr(mt_rand(0, 255)); } $benchmark->add('substr($str, 512, 32)', function () use(&$str) { return substr($str, 512, 32); }); $benchmark->add('mb_substr($str, 512, 32, \'8bit\')', function () use(&$str) { return mb_substr($str, 512, 32, '8bit'); }); $benchmark->setCount(1000000); $benchmark->run();
<?php /** * Created by PhpStorm. * User: ovr * Date: 06.12.15 * Time: 22:26 */ use Lavoiesl\PhpBenchmark\Benchmark; use Ovr\Bench\AssignOrNot\ReturnAfterAssignProperty; use Ovr\Bench\AssignOrNot\ReturnWithoutAfterAssignProperty; include_once __DIR__ . '/vendor/autoload.php'; $benchmark = new Benchmark(); $benchmark->add("date('Y')", function () { return date('Y'); }); class_exists(Twig_Extension_Core::class, true); $loader = new Twig_Loader_Filesystem(__DIR__ . '/twig'); $twig = new Twig_Environment($loader, []); $benchmark->add("Twig {{ 'now' | date('Y') | raw }}", function () use($twig) { return twig_date_format_filter($twig, "now", "Y"); }); $benchmark->setCount(10000); $benchmark->run();
* Created by PhpStorm. * User: ovr * Date: 06.12.15 * Time: 22:26 */ namespace Test; use Lavoiesl\PhpBenchmark\Benchmark; use Ovr\Bench\AssignOrNot\ReturnAfterAssignProperty; use Ovr\Bench\AssignOrNot\ReturnWithoutAfterAssignProperty; include_once __DIR__ . '/vendor/autoload.php'; class FnHodler { public static function test() { return 1; } } function test() { return 1; } $benchmark = new Benchmark(); $benchmark->add('Test\\FnHodler::test', function () use(&$serialized) { \Test\FnHodler::test(); }); $benchmark->add('Test\\test', function () use(&$serialized) { \Test\test(); }); $benchmark->setCount(10000000); $benchmark->run();
<?php /** * Created by PhpStorm. * User: ovr * Date: 06.12.15 * Time: 22:26 */ use Lavoiesl\PhpBenchmark\Benchmark; use Ovr\Bench\AssignOrNot\ReturnAfterAssignProperty; use Ovr\Bench\AssignOrNot\ReturnWithoutAfterAssignProperty; include_once __DIR__ . '/vendor/autoload.php'; $benchmark = new Benchmark(); $benchmark->add('for1', function () { for ($i = 0; $i < 100000; $i++) { } }); $benchmark->add('for2', function () { for ($i = 0; $i < 100000; ++$i) { } }); $benchmark->add('while', function () { $i = 0; while ($i < 100000) { $i++; } }); $benchmark->add('while2', function () { $i = 0; while ($i < 100000) { ++$i;
<?php use Lavoiesl\PhpBenchmark\Benchmark; use Ovr\Bench\AssignOrNot\ReturnAfterAssignProperty; use Ovr\Bench\AssignOrNot\ReturnWithoutAfterAssignProperty; include_once __DIR__ . '/vendor/autoload.php'; $benchmark = new Benchmark(); function proxy($a, $b, $c) { return true; } function test1() { return proxy(...func_get_args()); } function test2(...$args) { return proxy(...$args); } $benchmark->add('...func_get_args()', function () use(&$str) { return test1(1, 2, 3); }); $benchmark->add('....$args', function () use(&$str) { return test2(1, 2, 3); }); $benchmark->setCount(10000000); $benchmark->run();
<?php /** * JSON と MessagePack のシリアライズ速度を比較 * * @link http://www.lifehacker.jp/2015/08/150808mockaroo_1000.html ダミーデータ * @link https://github.com/msgpack/msgpack-php msgpack */ require __DIR__ . DIRECTORY_SEPARATOR . './vendor/autoload.php'; use Lavoiesl\PhpBenchmark\Benchmark; $data = (include __DIR__ . DIRECTORY_SEPARATOR . './data/mock_data.php'); $benchmark = new Benchmark(); $benchmark->add('big json', function () use($data) { return json_encode($data); }); $benchmark->add('big messagepack', function () use($data) { return msgpack_pack($data); }); $benchmark->add('big php', function () use($data) { return serialize($data); }); $benchmark->add('little json', function () use($data) { return json_encode($data[0]); }); $benchmark->add('little messagepack', function () use($data) { return msgpack_pack($data[0]); }); $benchmark->add('little php', function () use($data) { return serialize($data[0]); }); $benchmark->run();
$driverMemcached = new Driver\Memcached(); $benchmark->add('memcached get', function () use(&$driverMemcached) { $driverMemcached->get('test'); }); $driverNative = new Driver\NativeArray(); $benchmark->add('native get', function () use(&$driverNative) { $driverNative->get('test'); }); foreach ($cycles as $count) { $benchmark->setCount($count); $benchmark->run(); } $benchmark = new Benchmark(); $driverMemcache = new Driver\Memcache(); $benchmark->add('memcache set', function () use(&$driverMemcache) { $driverMemcache->save(rand(), 'tests'); }); $driverRedis = new Driver\Redis(); $benchmark->add('redis set', function () use(&$driverRedis) { $driverRedis->save(rand(), 'tests'); }); $driverMemcached = new Driver\Memcached(); $benchmark->add('memcached set', function () use(&$driverMemcached) { $driverMemcached->save(rand(), 'tests'); }); $driverNative = new Driver\NativeArray(); $benchmark->add('array set', function () use(&$driverNative) { $driverNative->save(rand(), 'tests'); }); foreach ($cycles as $count) { $benchmark->setCount($count);
<?php /** * Created by PhpStorm. * User: ovr * Date: 06.12.15 * Time: 22:26 */ use Lavoiesl\PhpBenchmark\Benchmark; use Ovr\Bench\AssignOrNot\ReturnAfterAssignProperty; use Ovr\Bench\AssignOrNot\ReturnWithoutAfterAssignProperty; include_once __DIR__ . '/vendor/autoload.php'; $benchmark = new Benchmark(); $benchmark->add('with-return', function () { $class = new ReturnAfterAssignProperty(); $class->getProperty(); }); $benchmark->add('without-return', function () { $class = new ReturnWithoutAfterAssignProperty(); $class->getProperty(); }); $benchmark->setCount(10000000); $benchmark->run();
<?php /** * Created by PhpStorm. * User: ovr * Date: 06.12.15 * Time: 22:26 */ use Lavoiesl\PhpBenchmark\Benchmark; use Ovr\Bench\AssignOrNot\ReturnAfterAssignProperty; use Ovr\Bench\AssignOrNot\ReturnWithoutAfterAssignProperty; include_once __DIR__ . '/vendor/autoload.php'; class User { const STATUS_ACTIVE = 'active'; public $status; } $benchmark = new Benchmark(); $user = new User(); $user->status = User::STATUS_ACTIVE; $benchmark->add('$a == $b', function () use($user) { return $user->status == User::STATUS_ACTIVE; }); $benchmark->add('$a === $b', function () use($user) { return $user->status === User::STATUS_ACTIVE; }); $benchmark->run(100000000000);
} EventLoop::$instance = new EventLoop(); class A { protected $ev; public function __construct(EventLoop $ev) { $this->ev = $ev; } public function test() { return $this->ev; } } class B { public function test() { return EventLoop::$instance; } } $aInstance = new A(new EventLoop()); $benchmark->add('without-static-normal-injection', function () use($aInstance) { return $aInstance->test(); }); $bInstance = new B(); $benchmark->add('with-static-global-state', function () use($bInstance) { return $bInstance->test(); }); $benchmark->setCount(10000000); $benchmark->run();
<?php use Lavoiesl\PhpBenchmark\Benchmark; require_once '_autoload.php'; // Generate long string for tests $string = ''; for ($i = 0; $i < 5000; $i++) { $string .= sha1(microtime(true)); } $benchmark = new Benchmark(); $benchmark->add('md5', function () use($string) { return md5($string); }); $benchmark->add('sha1', function () use($string) { return sha1($string); }); $benchmark->add('crc32', function () use($string) { return crc32($string); }); $benchmark->guessCount(2); // aim for 2 seconds per test (default) $benchmark->run();