Skip to content

Instantly share code, notes, and snippets.

@rybakit
Last active January 23, 2017 08:41
Show Gist options
  • Save rybakit/faadbcba631b2a148dddeea3eecf45b2 to your computer and use it in GitHub Desktop.
Save rybakit/faadbcba631b2a148dddeea3eecf45b2 to your computer and use it in GitHub Desktop.
PHP Serialization Benchmarks
\SKM\Benchmarks\Serialization\BooleanSerializationBench
benchSerializeJSON I9 P0 [μ Mo]/r: 0.201 0.195 (μs) [μSD μRSD]/r: 0.011μs 5.29%
benchSerializeJSON I9 P1 [μ Mo]/r: 0.202 0.196 (μs) [μSD μRSD]/r: 0.011μs 5.61%
benchDeserializeJSON I9 P0 [μ Mo]/r: 0.386 0.385 (μs) [μSD μRSD]/r: 0.002μs 0.48%
benchDeserializeJSON I9 P1 [μ Mo]/r: 0.447 0.410 (μs) [μSD μRSD]/r: 0.107μs 23.81%
benchSerializeNative I9 P0 [μ Mo]/r: 0.221 0.218 (μs) [μSD μRSD]/r: 0.009μs 4.18%
benchSerializeNative I9 P1 [μ Mo]/r: 0.221 0.218 (μs) [μSD μRSD]/r: 0.009μs 4.21%
benchDeserializeNative I9 P0 [μ Mo]/r: 0.441 0.431 (μs) [μSD μRSD]/r: 0.019μs 4.31%
benchDeserializeNative I9 P1 [μ Mo]/r: 0.441 0.430 (μs) [μSD μRSD]/r: 0.019μs 4.26%
benchSerializeMsgPack I9 P0 [μ Mo]/r: 0.236 0.216 (μs) [μSD μRSD]/r: 0.061μs 25.69%
benchSerializeMsgPack I9 P1 [μ Mo]/r: 0.215 0.213 (μs) [μSD μRSD]/r: 0.004μs 1.66%
benchDeserializeMsgPack I9 P0 [μ Mo]/r: 0.311 0.308 (μs) [μSD μRSD]/r: 0.008μs 2.42%
benchDeserializeMsgPack I9 P1 [μ Mo]/r: 0.372 0.317 (μs) [μSD μRSD]/r: 0.109μs 29.35%
benchSerializeMsgPackPure I9 P0 [μ Mo]/r: 0.250 0.245 (μs) [μSD μRSD]/r: 0.007μs 2.79%
benchSerializeMsgPackPure I9 P1 [μ Mo]/r: 0.286 0.251 (μs) [μSD μRSD]/r: 0.091μs 31.71%
benchDeserializeMsgPackPure I9 P0 [μ Mo]/r: 0.554 0.545 (μs) [μSD μRSD]/r: 0.025μs 4.55%
benchDeserializeMsgPackPure I9 P1 [μ Mo]/r: 0.586 0.552 (μs) [μSD μRSD]/r: 0.097μs 16.48%
\SKM\Benchmarks\Serialization\IntegerSerializationBench
benchSerializeJSON I9 P0 [μ Mo]/r: 0.205 0.202 (μs) [μSD μRSD]/r: 0.006μs 2.82%
benchSerializeJSON I9 P1 [μ Mo]/r: 0.217 0.207 (μs) [μSD μRSD]/r: 0.020μs 9.13%
benchSerializeJSON I9 P2 [μ Mo]/r: 0.214 0.208 (μs) [μSD μRSD]/r: 0.013μs 6.19%
benchSerializeJSON I9 P3 [μ Mo]/r: 0.210 0.208 (μs) [μSD μRSD]/r: 0.002μs 0.80%
benchDeserializeJSON I9 P0 [μ Mo]/r: 0.403 0.394 (μs) [μSD μRSD]/r: 0.012μs 3.08%
benchDeserializeJSON I9 P1 [μ Mo]/r: 0.407 0.405 (μs) [μSD μRSD]/r: 0.004μs 0.89%
benchDeserializeJSON I9 P2 [μ Mo]/r: 0.461 0.422 (μs) [μSD μRSD]/r: 0.115μs 24.86%
benchDeserializeJSON I9 P3 [μ Mo]/r: 0.433 0.429 (μs) [μSD μRSD]/r: 0.009μs 2.13%
benchSerializeNative I9 P0 [μ Mo]/r: 0.235 0.231 (μs) [μSD μRSD]/r: 0.010μs 4.38%
benchSerializeNative I9 P1 [μ Mo]/r: 0.235 0.232 (μs) [μSD μRSD]/r: 0.010μs 4.41%
benchSerializeNative I9 P2 [μ Mo]/r: 0.240 0.236 (μs) [μSD μRSD]/r: 0.010μs 4.28%
benchSerializeNative I9 P3 [μ Mo]/r: 0.240 0.236 (μs) [μSD μRSD]/r: 0.010μs 4.24%
benchDeserializeNative I9 P0 [μ Mo]/r: 0.442 0.432 (μs) [μSD μRSD]/r: 0.021μs 4.67%
benchDeserializeNative I9 P1 [μ Mo]/r: 0.463 0.445 (μs) [μSD μRSD]/r: 0.044μs 9.57%
benchDeserializeNative I9 P2 [μ Mo]/r: 0.460 0.449 (μs) [μSD μRSD]/r: 0.022μs 4.80%
benchDeserializeNative I9 P3 [μ Mo]/r: 0.458 0.458 (μs) [μSD μRSD]/r: 0.001μs 0.32%
benchSerializeMsgPack I9 P0 [μ Mo]/r: 0.220 0.217 (μs) [μSD μRSD]/r: 0.006μs 2.62%
benchSerializeMsgPack I9 P1 [μ Mo]/r: 0.219 0.217 (μs) [μSD μRSD]/r: 0.006μs 2.52%
benchSerializeMsgPack I9 P2 [μ Mo]/r: 0.219 0.218 (μs) [μSD μRSD]/r: 0.004μs 1.88%
benchSerializeMsgPack I9 P3 [μ Mo]/r: 0.220 0.219 (μs) [μSD μRSD]/r: 0.003μs 1.36%
benchDeserializeMsgPack I9 P0 [μ Mo]/r: 0.312 0.297 (μs) [μSD μRSD]/r: 0.029μs 9.43%
benchDeserializeMsgPack I9 P1 [μ Mo]/r: 0.303 0.297 (μs) [μSD μRSD]/r: 0.014μs 4.50%
benchDeserializeMsgPack I9 P2 [μ Mo]/r: 0.304 0.297 (μs) [μSD μRSD]/r: 0.015μs 4.94%
benchDeserializeMsgPack I9 P3 [μ Mo]/r: 0.336 0.331 (μs) [μSD μRSD]/r: 0.014μs 4.25%
benchSerializeMsgPackPure I9 P0 [μ Mo]/r: 0.262 0.256 (μs) [μSD μRSD]/r: 0.015μs 5.69%
benchSerializeMsgPackPure I9 P1 [μ Mo]/r: 0.257 0.256 (μs) [μSD μRSD]/r: 0.005μs 1.95%
benchSerializeMsgPackPure I9 P2 [μ Mo]/r: 0.284 0.261 (μs) [μSD μRSD]/r: 0.065μs 22.99%
benchSerializeMsgPackPure I9 P3 [μ Mo]/r: 0.421 0.359 (μs) [μSD μRSD]/r: 0.124μs 29.49%
benchDeserializeMsgPackPure I9 P0 [μ Mo]/r: 0.468 0.447 (μs) [μSD μRSD]/r: 0.066μs 14.09%
benchDeserializeMsgPackPure I9 P1 [μ Mo]/r: 0.453 0.443 (μs) [μSD μRSD]/r: 0.022μs 4.84%
benchDeserializeMsgPackPure I9 P2 [μ Mo]/r: 0.482 0.452 (μs) [μSD μRSD]/r: 0.048μs 9.89%
benchDeserializeMsgPackPure I9 P3 [μ Mo]/r: 0.871 0.859 (μs) [μSD μRSD]/r: 0.024μs 2.73%
\SKM\Benchmarks\Serialization\StringSerializationBench
benchSerializeJSON I9 P0 [μ Mo]/r: 0.218 0.212 (μs) [μSD μRSD]/r: 0.013μs 5.82%
benchSerializeJSON I9 P1 [μ Mo]/r: 0.213 0.212 (μs) [μSD μRSD]/r: 0.002μs 0.85%
benchSerializeJSON I9 P2 [μ Mo]/r: 0.237 0.216 (μs) [μSD μRSD]/r: 0.063μs 26.37%
benchSerializeJSON I9 P3 [μ Mo]/r: 0.225 0.221 (μs) [μSD μRSD]/r: 0.013μs 5.92%
benchDeserializeJSON I9 P0 [μ Mo]/r: 0.413 0.403 (μs) [μSD μRSD]/r: 0.019μs 4.71%
benchDeserializeJSON I9 P1 [μ Mo]/r: 0.423 0.412 (μs) [μSD μRSD]/r: 0.019μs 4.59%
benchDeserializeJSON I9 P2 [μ Mo]/r: 0.424 0.420 (μs) [μSD μRSD]/r: 0.007μs 1.66%
benchDeserializeJSON I9 P3 [μ Mo]/r: 0.449 0.432 (μs) [μSD μRSD]/r: 0.032μs 7.17%
benchSerializeNative I9 P0 [μ Mo]/r: 0.253 0.238 (μs) [μSD μRSD]/r: 0.043μs 17.06%
benchSerializeNative I9 P1 [μ Mo]/r: 0.238 0.237 (μs) [μSD μRSD]/r: 0.002μs 0.65%
benchSerializeNative I9 P2 [μ Mo]/r: 0.250 0.239 (μs) [μSD μRSD]/r: 0.016μs 6.37%
benchSerializeNative I9 P3 [μ Mo]/r: 0.247 0.239 (μs) [μSD μRSD]/r: 0.013μs 5.43%
benchDeserializeNative I9 P0 [μ Mo]/r: 0.486 0.479 (μs) [μSD μRSD]/r: 0.021μs 4.41%
benchDeserializeNative I9 P1 [μ Mo]/r: 0.495 0.487 (μs) [μSD μRSD]/r: 0.021μs 4.20%
benchDeserializeNative I9 P2 [μ Mo]/r: 0.507 0.503 (μs) [μSD μRSD]/r: 0.011μs 2.24%
benchDeserializeNative I9 P3 [μ Mo]/r: 0.518 0.514 (μs) [μSD μRSD]/r: 0.009μs 1.68%
benchSerializeMsgPack I9 P0 [μ Mo]/r: 0.231 0.224 (μs) [μSD μRSD]/r: 0.014μs 5.96%
benchSerializeMsgPack I9 P1 [μ Mo]/r: 0.226 0.224 (μs) [μSD μRSD]/r: 0.007μs 3.14%
benchSerializeMsgPack I9 P2 [μ Mo]/r: 0.231 0.225 (μs) [μSD μRSD]/r: 0.013μs 5.62%
benchSerializeMsgPack I9 P3 [μ Mo]/r: 0.298 0.236 (μs) [μSD μRSD]/r: 0.104μs 34.71%
benchDeserializeMsgPack I9 P0 [μ Mo]/r: 0.344 0.339 (μs) [μSD μRSD]/r: 0.010μs 2.90%
benchDeserializeMsgPack I9 P1 [μ Mo]/r: 0.356 0.345 (μs) [μSD μRSD]/r: 0.023μs 6.39%
benchDeserializeMsgPack I9 P2 [μ Mo]/r: 0.354 0.352 (μs) [μSD μRSD]/r: 0.007μs 1.98%
benchDeserializeMsgPack I9 P3 [μ Mo]/r: 0.377 0.362 (μs) [μSD μRSD]/r: 0.024μs 6.41%
benchSerializeMsgPackPure I9 P0 [μ Mo]/r: 0.350 0.343 (μs) [μSD μRSD]/r: 0.016μs 4.60%
benchSerializeMsgPackPure I9 P1 [μ Mo]/r: 0.376 0.342 (μs) [μSD μRSD]/r: 0.101μs 27.02%
benchSerializeMsgPackPure I9 P2 [μ Mo]/r: 0.354 0.344 (μs) [μSD μRSD]/r: 0.025μs 7.11%
benchSerializeMsgPackPure I9 P3 [μ Mo]/r: 0.351 0.344 (μs) [μSD μRSD]/r: 0.016μs 4.56%
benchDeserializeMsgPackPure I9 P0 [μ Mo]/r: 0.736 0.676 (μs) [μSD μRSD]/r: 0.180μs 24.47%
benchDeserializeMsgPackPure I9 P1 [μ Mo]/r: 0.693 0.671 (μs) [μSD μRSD]/r: 0.063μs 9.16%
benchDeserializeMsgPackPure I9 P2 [μ Mo]/r: 0.698 0.677 (μs) [μSD μRSD]/r: 0.041μs 5.92%
benchDeserializeMsgPackPure I9 P3 [μ Mo]/r: 0.687 0.685 (μs) [μSD μRSD]/r: 0.005μs 0.75%
\SKM\Benchmarks\Serialization\StructSerializationBench
benchSerializeJSON I9 P0 [μ Mo]/r: 0.555 0.519 (μs) [μSD μRSD]/r: 0.077μs 13.96%
benchSerializeJSON I9 P1 [μ Mo]/r: 1.276 1.190 (μs) [μSD μRSD]/r: 0.253μs 19.84%
benchSerializeJSON I9 P2 [μ Mo]/r: 3.367 3.337 (μs) [μSD μRSD]/r: 0.070μs 2.07%
benchDeserializeJSON I9 P0 [μ Mo]/r: 1.730 1.724 (μs) [μSD μRSD]/r: 0.010μs 0.60%
benchDeserializeJSON I9 P1 [μ Mo]/r: 4.106 4.078 (μs) [μSD μRSD]/r: 0.061μs 1.48%
benchDeserializeJSON I9 P2 [μ Mo]/r: 12.589 12.367 (μs) [μSD μRSD]/r: 0.404μs 3.21%
benchSerializeNative I9 P0 [μ Mo]/r: 0.440 0.431 (μs) [μSD μRSD]/r: 0.020μs 4.63%
benchSerializeNative I9 P1 [μ Mo]/r: 0.744 0.737 (μs) [μSD μRSD]/r: 0.014μs 1.90%
benchSerializeNative I9 P2 [μ Mo]/r: 1.782 1.738 (μs) [μSD μRSD]/r: 0.073μs 4.11%
benchDeserializeNative I9 P0 [μ Mo]/r: 1.701 1.698 (μs) [μSD μRSD]/r: 0.005μs 0.30%
benchDeserializeNative I9 P1 [μ Mo]/r: 4.060 3.911 (μs) [μSD μRSD]/r: 0.445μs 10.95%
benchDeserializeNative I9 P2 [μ Mo]/r: 11.953 11.534 (μs) [μSD μRSD]/r: 0.859μs 7.19%
benchSerializeMsgPack I9 P0 [μ Mo]/r: 0.587 0.586 (μs) [μSD μRSD]/r: 0.004μs 0.61%
benchSerializeMsgPack I9 P1 [μ Mo]/r: 0.829 0.825 (μs) [μSD μRSD]/r: 0.006μs 0.71%
benchSerializeMsgPack I9 P2 [μ Mo]/r: 1.989 1.952 (μs) [μSD μRSD]/r: 0.088μs 4.45%
benchDeserializeMsgPack I9 P0 [μ Mo]/r: 1.362 1.329 (μs) [μSD μRSD]/r: 0.068μs 4.97%
benchDeserializeMsgPack I9 P1 [μ Mo]/r: 3.172 3.019 (μs) [μSD μRSD]/r: 0.220μs 6.92%
benchDeserializeMsgPack I9 P2 [μ Mo]/r: 9.296 8.971 (μs) [μSD μRSD]/r: 0.791μs 8.51%
benchSerializeMsgPackPure I9 P0 [μ Mo]/r: 1.757 1.720 (μs) [μSD μRSD]/r: 0.094μs 5.33%
benchSerializeMsgPackPure I9 P1 [μ Mo]/r: 3.414 3.348 (μs) [μSD μRSD]/r: 0.130μs 3.80%
benchSerializeMsgPackPure I9 P2 [μ Mo]/r: 10.329 10.042 (μs) [μSD μRSD]/r: 0.545μs 5.28%
benchDeserializeMsgPackPure I9 P0 [μ Mo]/r: 4.598 4.468 (μs) [μSD μRSD]/r: 0.395μs 8.59%
benchDeserializeMsgPackPure I9 P1 [μ Mo]/r: 10.119 10.032 (μs) [μSD μRSD]/r: 0.201μs 1.99%
benchDeserializeMsgPackPure I9 P2 [μ Mo]/r: 30.939 30.173 (μs) [μSD μRSD]/r: 1.640μs 5.30%
32 subjects, 1,040 iterations, 104,000 revs, 0 rejects
(best [mean mode] worst) = 0.194 [1.455 1.417] 0.213 (μs)
⅀T: 1,512.712μs μSD/r 0.084μs μRSD/r: 7.241%
diff --git a/benchmarks/SerializationBench.php b/benchmarks/SerializationBench.php
index 03c9484..12d0ce3 100644
--- a/benchmarks/SerializationBench.php
+++ b/benchmarks/SerializationBench.php
@@ -2,6 +2,8 @@
namespace SKM\Benchmarks\Serialization;
+use MessagePack\BufferUnpacker;
+use MessagePack\Packer;
use PhpBench\Benchmark\Metadata\Annotations\BeforeMethods;
use PhpBench\Benchmark\Metadata\Annotations\Groups;
use PhpBench\Benchmark\Metadata\Annotations\Iterations;
@@ -29,8 +31,8 @@ abstract class SerializationBench
private $msgPackUnpacker;
public function init() {
- $this->msgPackPacker = new \MessagePack\Packer();
- $this->msgPackUnpacker = new \MessagePack\Unpacker();
+ $this->msgPackPacker = new Packer(Packer::FORCE_STR | Packer::FORCE_ARR);
+ $this->msgPackUnpacker = new BufferUnpacker();
}
/**
@@ -48,7 +50,7 @@ abstract class SerializationBench
*/
public function benchDeserializeJSON($params)
{
- json_decode($params['data'], true);
+ json_decode(utf8_decode($params['data']), true);
}
/**
@@ -66,7 +68,7 @@ abstract class SerializationBench
*/
public function benchDeserializeNative($params)
{
- unserialize($params['data']);
+ unserialize(utf8_decode($params['data']));
}
/**
@@ -76,7 +78,7 @@ abstract class SerializationBench
*/
public function benchSerializeMsgPack($params)
{
- $this->msgPackPacker->pack($params['data']);
+ msgpack_pack($params['data']);
}
/**
@@ -88,7 +90,29 @@ abstract class SerializationBench
*/
public function benchDeserializeMsgPack($params)
{
- $this->msgPackUnpacker->unpack(utf8_decode($params['data']));
+ msgpack_unpack(utf8_decode($params['data']));
+ }
+
+ /**
+ * @ParamProviders({"provideData"})
+ * @Warmup(2)
+ * @Groups({"serialization"})
+ */
+ public function benchSerializeMsgPackPure($params)
+ {
+ $this->msgPackPacker->pack($params['data']);
+ }
+
+ /**
+ * @ParamProviders({"provideMsgPackData"})
+ * @Warmup(2)
+ * @Groups({"deserialization"})
+ *
+ * TODO: find out why the UTF-8 hack is necessary
+ */
+ public function benchDeserializeMsgPackPure($params)
+ {
+ $this->msgPackUnpacker->reset(utf8_decode($params['data']))->unpack();
}
public function provideData()
@@ -110,7 +134,7 @@ abstract class SerializationBench
foreach ($this->dataProvider() as $d) {
$data[] = [
- 'data' => json_encode($d),
+ 'data' => utf8_encode(json_encode($d)),
];
}
@@ -123,7 +147,7 @@ abstract class SerializationBench
foreach ($this->dataProvider() as $d) {
$data[] = [
- 'data' => serialize($d),
+ 'data' => utf8_encode(serialize($d)),
];
}
PHP 7.1.0 (cli) (built: Dec 20 2016 17:28:16) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.1.0-dev, Copyright (c) 1998-2016 Zend Technologies
msgpack 2.0.2
PhpBench 0.13.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment