Skip to content

Commit

Permalink
Merge pull request #1365 from scyzoryck/add-phpbench
Browse files Browse the repository at this point in the history
Add performnace benchmarks
  • Loading branch information
goetas committed Nov 22, 2021
2 parents 914197e + 9836199 commit 9b0b520
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 73 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/benchmark.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Benchmarks

on:
pull_request:
push:
branches:
- master

jobs:
phpbench:
name: "PHPBench"
runs-on: "ubuntu-20.04"

strategy:
matrix:
php-version:
- "7.2"
- "8.0"

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}

- name: "Install dependencies with Composer"
uses: "ramsey/composer-install@v1"

- name: Run tests
run: |
vendor/bin/phpbench run tests/Benchmark/ --report=aggregate
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"doctrine/phpcr-odm": "^1.3|^2.0",
"jackalope/jackalope-doctrine-dbal": "^1.1.5",
"ocramius/proxy-manager": "^1.0|^2.0",
"phpbench/phpbench": "^1.0",
"phpstan/phpstan": "^1.0.2",
"phpunit/phpunit": "^8.0||^9.0",
"psr/container": "^1.0",
Expand Down
5 changes: 5 additions & 0 deletions phpbench.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"runner.bootstrap": "tests/bootstrap.php",
"runner.iterations": 3,
"runner.revs": 1
}
75 changes: 75 additions & 0 deletions tests/Benchmark/AbstractSerializationBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

declare(strict_types=1);

namespace JMS\Serializer\Tests\Benchmark;

use JMS\Serializer\SerializationContext;
use JMS\Serializer\Serializer;
use JMS\Serializer\SerializerBuilder;
use JMS\Serializer\Tests\Fixtures\Author;
use JMS\Serializer\Tests\Fixtures\BlogPost;
use JMS\Serializer\Tests\Fixtures\Comment;
use JMS\Serializer\Tests\Fixtures\Publisher;

abstract class AbstractSerializationBench
{
/**
* @var Serializer
*/
private $serializer;

/**
* @var array|BlogPost[]
*/
private $collection;

/**
* @var string
*/
private $format;

public function __construct()
{
$this->serializer = SerializerBuilder::create()->build();
$this->collection = $this->createCollection();
$this->format = $this->getFormat();
}

public function benchSerialization(): void
{
$this->serializer->serialize($this->collection, $this->format, $this->createContext());
}

abstract protected function getFormat(): string;

protected function createContext(): SerializationContext
{
return new SerializationContext();
}

private function createCollection()
{
$collection = [];
for ($i = 0; $i < 200; $i++) {
$collection[] = $this->createPost();
}

return $collection;
}

private function createPost()
{
$post = new BlogPost(
'FooooooooooooooooooooooBAR',
new Author('Foo'),
new \DateTime(),
new Publisher('bar')
);
for ($i = 0; $i < 100; $i++) {
$post->addComment(new Comment(new Author('foo'), 'foobar'));
}

return $post;
}
}
15 changes: 15 additions & 0 deletions tests/Benchmark/JsonMaxDepthSerializationBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace JMS\Serializer\Tests\Benchmark;

use JMS\Serializer\SerializationContext;

class JsonMaxDepthSerializationBench extends JsonSerializationBench
{
protected function createContext(): SerializationContext
{
return parent::createContext()->enableMaxDepthChecks();
}
}
13 changes: 13 additions & 0 deletions tests/Benchmark/JsonSerializationBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace JMS\Serializer\Tests\Benchmark;

class JsonSerializationBench extends AbstractSerializationBench
{
protected function getFormat(): string
{
return 'json';
}
}
13 changes: 13 additions & 0 deletions tests/Benchmark/XmlSerializationBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace JMS\Serializer\Tests\Benchmark;

class XmlSerializationBench extends AbstractSerializationBench
{
protected function getFormat(): string
{
return 'xml';
}
}
73 changes: 0 additions & 73 deletions tests/benchmark.php

This file was deleted.

0 comments on commit 9b0b520

Please sign in to comment.