Skip to content

Commit

Permalink
Create serialization of small JSON benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
steevanb authored and phpbenchmarks committed May 12, 2019
1 parent 4f44054 commit d6ff6d4
Show file tree
Hide file tree
Showing 6 changed files with 277 additions and 0 deletions.
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<p align="center">
<img src="http://www.phpbenchmarks.com/images/logo_github.png">
<br>
<a href="http://www.phpbenchmarks.com" target="_blank">www.phpbenchmarks.com</a>
</p>

## What is www.phpbenchmarks.com?

You will find lot of benchmarks for PHP frameworks and template engines.

You can compare results between Apache Bench and Siege, PHP 5.6 to 7.3 and versions of your favorites PHP code.

## What is this repository?

It's dependency for [small JSON serialization](http://www.phpbenchmarks.com/en/documentation/benchmark/json-serialization-small-overload) benchmark.

You can find how we benchmark it on [benchmarking protocol page](http://www.phpbenchmarks.com/en/documentation/benchmarking-protocol).

## Retrieve data to serialize

```php
use PhpBenchmarks\BenchmarkJsonSerializationSmallOverload\BenchmarkService;

BenchmarkService::getDataToSerialize();
```

## Validation of serialization

As serialization don't need to write anything to response body,
when we validate your serialization we add a parameter into query string
to indicate you need to write serialization result into response body.

```php
use PhpBenchmarks\BenchmarkJsonSerializationSmallOverload\BenchmarkService;

// Write serialized data to response body if isWriteToResponseBody() return true, to validate it.
if (BenchmarkService::isWriteToResponseBody()) {
echo $serializer->serialize(BenchmarkService::getDataToSerialize());

// Only serialize data if isWriteToResponseBody() return false, to benchmark it.
} else {
$serializer->serialize(BenchmarkService::getDataToSerialize());
}
```
105 changes: 105 additions & 0 deletions src/BenchmarkService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

namespace PhpBenchmarks\BenchmarkJsonSerializationSmallOverload;

use PhpBenchmarks\BenchmarkJsonSerializationSmallOverload\ObjectToSerializeFactory\ObjectToSerializeFactory;
use PhpBenchmarks\BenchmarkJsonSerializationSmallOverload\ObjectToSerializeFactory\ObjectToSerializeFactoryInterface;

class BenchmarkService
{
/** @var ObjectToSerializeFactoryInterface */
protected $objectFactory;

public function __construct(ObjectToSerializeFactoryInterface $objectFactory = null)
{
if ($objectFactory === null) {
$objectFactory = new ObjectToSerializeFactory();
}
$this->objectFactory = $objectFactory;
}

/** @return array */
public function getDataToSerialize()
{
$data = [
'int1' => 1,
'int2' => 2,
'int3' => 3,
'int4' => 4,
'int5' => 5,
'int6' => 6,
'int7' => 7,
'int8' => 8,
'int9' => 9,
'int10' => 10,
'float1' => 1.01,
'float2' => 2.01,
'float3' => 3.01,
'float4' => 4.01,
'float5' => 5.01,
'float6' => 6.01,
'float7' => 7.01,
'float8' => 8.01,
'float9' => 9.01,
'float10' => 10.01,
'string1' => 'phpbenchmarks1',
'string2' => 'phpbenchmarks2',
'string3' => 'phpbenchmarks3',
'string4' => 'phpbenchmarks4',
'string5' => 'phpbenchmarks5',
'string6' => 'phpbenchmarks6',
'string7' => 'phpbenchmarks7',
'string8' => 'phpbenchmarks8',
'string9' => 'phpbenchmarks9',
'string10' => 'phpbenchmarks10',
'boolean1' => true,
'boolean2' => true,
'boolean3' => true,
'boolean4' => true,
'boolean5' => true,
'boolean6' => true,
'boolean7' => true,
'boolean8' => true,
'boolean9' => true,
'boolean10' => true,
'null1' => null,
'null2' => null,
'null3' => null,
'null4' => null,
'null5' => null,
'null6' => null,
'null7' => null,
'null8' => null,
'null9' => null,
'null10' => null,
];

$return = [];
$objectToSerialize = $this->objectFactory->create();
for ($i = 0; $i < 100; $i++) {
$return[] = array_merge(
$data,
[
'array1' => ['object' => $objectToSerialize],
'array2' => ['object' => $objectToSerialize],
'array3' => ['object' => $objectToSerialize],
'array4' => ['object' => $objectToSerialize],
'array5' => ['object' => $objectToSerialize],
'array6' => ['object' => $objectToSerialize],
'array7' => ['object' => $objectToSerialize],
'array8' => ['object' => $objectToSerialize],
'array9' => ['object' => $objectToSerialize],
'array10' => ['object' => $objectToSerialize]
]
);
}

return $return;
}

/** @return bool */
public function isWriteToResponseBody()
{
return @$_GET['phpBenchmarksShowResults'] === '1';
}
}
66 changes: 66 additions & 0 deletions src/ObjectToSerialize/ObjectToSerialize.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace PhpBenchmarks\BenchmarkJsonSerializationSmallOverload\ObjectToSerialize;

class ObjectToSerialize implements ObjectToSerializeInterface
{
/** @return string */
public function getProperty1()
{
return 'ObjectToSerialize-property1';
}

/** @return string */
public function getProperty2()
{
return 'ObjectToSerialize-property2';
}

/** @return string */
public function getProperty3()
{
return 'ObjectToSerialize-property3';
}

/** @return string */
public function getProperty4()
{
return 'ObjectToSerialize-property4';
}

/** @return string */
public function getProperty5()
{
return 'ObjectToSerialize-property5';
}

/** @return string */
public function getProperty6()
{
return 'ObjectToSerialize-property6';
}

/** @return string */
public function getProperty7()
{
return 'ObjectToSerialize-property7';
}

/** @return string */
public function getProperty8()
{
return 'ObjectToSerialize-property8';
}

/** @return string */
public function getProperty9()
{
return 'ObjectToSerialize-property9';
}

/** @return string */
public function getProperty10()
{
return 'ObjectToSerialize-property10';
}
}
36 changes: 36 additions & 0 deletions src/ObjectToSerialize/ObjectToSerializeInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace PhpBenchmarks\BenchmarkJsonSerializationSmallOverload\ObjectToSerialize;

interface ObjectToSerializeInterface
{
/** @return string */
public function getProperty1();

/** @return string */
public function getProperty2();

/** @return string */
public function getProperty3();

/** @return string */
public function getProperty4();

/** @return string */
public function getProperty5();

/** @return string */
public function getProperty6();

/** @return string */
public function getProperty7();

/** @return string */
public function getProperty8();

/** @return string */
public function getProperty9();

/** @return string */
public function getProperty10();
}
15 changes: 15 additions & 0 deletions src/ObjectToSerializeFactory/ObjectToSerializeFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace PhpBenchmarks\BenchmarkJsonSerializationSmallOverload\ObjectToSerializeFactory;

use PhpBenchmarks\BenchmarkJsonSerializationSmallOverload\ObjectToSerialize\ObjectToSerialize;
use PhpBenchmarks\BenchmarkJsonSerializationSmallOverload\ObjectToSerialize\ObjectToSerializeInterface;

class ObjectToSerializeFactory implements ObjectToSerializeFactoryInterface
{
/** @return ObjectToSerializeInterface */
public function create()
{
return new ObjectToSerialize();
}
}
11 changes: 11 additions & 0 deletions src/ObjectToSerializeFactory/ObjectToSerializeFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace PhpBenchmarks\BenchmarkJsonSerializationSmallOverload\ObjectToSerializeFactory;

use PhpBenchmarks\BenchmarkJsonSerializationSmallOverload\ObjectToSerialize\ObjectToSerializeInterface;

interface ObjectToSerializeFactoryInterface
{
/** @return ObjectToSerializeInterface */
public function create();
}

0 comments on commit d6ff6d4

Please sign in to comment.