Created
April 23, 2016 09:46
-
-
Save rybakit/b970a0d5c4db70a0455a3140995d01cc to your computer and use it in GitHub Desktop.
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 | |
// Borrowed from https://gist.github.com/denji/7c3c9d4b449f2699ec73 | |
require __DIR__.'/../vendor/autoload.php'; | |
ini_set("display_errors", 1); | |
ini_set("display_startup_errors", 1); | |
ini_set("error_reporting", E_ALL); | |
class SimpleObjectTransformer implements \MessagePack\TypeTransformer\TypeTransformer | |
{ | |
private $id; | |
public function __construct($id) | |
{ | |
$this->id = $id; | |
} | |
public function getId() | |
{ | |
return $this->id; | |
} | |
public function supports($value) | |
{ | |
return $value instanceof \stdClass; | |
} | |
public function transform($object) | |
{ | |
return (array) $object; | |
} | |
public function reverseTransform($array) | |
{ | |
return (object) $array; | |
} | |
} | |
$packer = new \MessagePack\Packer(); | |
$unpacker = new \MessagePack\BufferUnpacker(); | |
$coll = new \MessagePack\TypeTransformer\Collection([new SimpleObjectTransformer(5)]); | |
$packer->setTransformers($coll); | |
$unpacker->setTransformers($coll); | |
$_testStrings = array( | |
'AK' => 'Alaska', 'AZ' => 'Arizona', 'VT' => 'Vermont', | |
'VA' => 'Virginia', 'AZ' => 'West Virginia', | |
); | |
$_testIntegers = array(0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 84, 144,); | |
$_testBooleans = array(TRUE, TRUE, FALSE, FALSE, FALSE, TRUE, TRUE,); | |
$_testFloats = array( | |
0, 1.1, 1.1, 2.22, 3.33, 5.55, 8.88, 13.13, 21.2121, 34.3434, | |
55.5555, 84.8484, 144.144, | |
); | |
$_testMixed = array( | |
'one', 13 => 'two', 0 => 25.46, 'four' => 0.007, | |
'five' => TRUE, TRUE => 42, | |
); | |
$_objectOne = new stdClass(); | |
$_objectOne->firstname = 'Leroy'; | |
$_objectOne->lastname = 'Jenkins'; | |
$_objectOne->profession = 'Gamer'; | |
$_objectOne->status = 'Legend'; | |
$_objectTwo = new stdClass(); | |
$_objectTwo->series = 'Fibonacci'; | |
$_objectTwo->data = $_testIntegers; | |
$_testObjects = array($_objectOne, $_objectTwo,); | |
$_maxLoop = 100000; | |
$_templateEncode = "%s [%s]: Size: %s bytes, %s time to encode\r\n"; | |
$_templateDecode = "%s [%s]: %s time to decode\r\n"; | |
set_time_limit(0); | |
$_output = ''; | |
/** | |
* Set the source arrays | |
*/ | |
$_allTestData = array( | |
'str' => $_testStrings, | |
'int' => $_testIntegers, | |
'bln' => $_testBooleans, | |
'flt' => $_testFloats, | |
'mix' => $_testMixed, | |
'obj' => $_testObjects, | |
); | |
$_testSources = array( | |
'strings' => $_testStrings, | |
'integers' => $_testIntegers, | |
'booleans' => $_testBooleans, | |
'floats' => $_testFloats, | |
'mixed' => $_testMixed, | |
'objects' => $_testObjects, | |
'all' => $_allTestData, | |
); | |
/** | |
* ENCODE DATA | |
*/ | |
/** | |
* Start each test | |
*/ | |
foreach ($_testSources as $_area => $_source) | |
{ | |
/** | |
* pure msgpack | |
*/ | |
$_pureMsgpackStart = microtime(TRUE); | |
for ($_counter = 0; $_counter < $_maxLoop; $_counter++) | |
{ | |
$packer->pack($_source); | |
} | |
$_pureMsgpackEnd = microtime(TRUE); | |
$_pureMsgpackOutput = $packer->pack($_source); | |
$_output .= sprintf( | |
$_templateEncode, | |
'Packer::pack()', | |
$_area, | |
strlen($_pureMsgpackOutput), | |
$_pureMsgpackEnd - $_pureMsgpackStart | |
); | |
} | |
$_output .= "\r\n"; | |
/** | |
* DECODE DATA | |
*/ | |
/** | |
* Start each test | |
*/ | |
foreach ($_testSources as $_area => $_source) | |
{ | |
$_data = $packer->pack($_source); | |
$_pureMsgpackStart = microtime(TRUE); | |
for ($_counter = 0; $_counter < $_maxLoop; $_counter++) | |
{ | |
$unpacker->reset($_data)->unpack(); | |
} | |
$_pureMsgpackEnd = microtime(TRUE); | |
$_output .= sprintf( | |
$_templateDecode, | |
'BufferUnpacker::unpack()', | |
$_area, | |
$_pureMsgpackEnd - $_pureMsgpackStart | |
); | |
} | |
echo $_output; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment