Skip to content

Commit 9a80b3a

Browse files
committed
Add TypeParsingBench for phpbench
Isolates type parsing performance: 9 realistic type strings, 1000 revs x 5 iterations, comparing raw Parser vs CachingParser.
1 parent 8890b13 commit 9a80b3a

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace JMS\Serializer\Tests\Benchmark\Performance;
6+
7+
use JMS\Serializer\Type\CachingParser;
8+
use JMS\Serializer\Type\Parser;
9+
use PhpBench\Benchmark\Metadata\Annotations\BeforeMethods;
10+
use PhpBench\Benchmark\Metadata\Annotations\Iterations;
11+
use PhpBench\Benchmark\Metadata\Annotations\Revs;
12+
13+
/**
14+
* @BeforeMethods({"setUp"})
15+
* @Iterations(5)
16+
* @Revs(1000)
17+
*/
18+
class TypeParsingBench
19+
{
20+
/**
21+
* @var Parser
22+
*/
23+
private $parser;
24+
25+
/**
26+
* @var CachingParser
27+
*/
28+
private $cachingParser;
29+
30+
/**
31+
* @var string[]
32+
*/
33+
private $types = [
34+
'string',
35+
'int',
36+
'array<string>',
37+
'array<string, int>',
38+
'DateTime<\'Y-m-d\'>',
39+
'DateTime<\'Y-m-d\TH:i:s\', \'UTC\'>',
40+
'array<App\Entity\User>',
41+
'Foo<Bar<Baz, Qux>>',
42+
'DateTime<null, null, [\'Y-m-d\TH:i:s\', \'Y-m-d\TH:i:sP\']>',
43+
];
44+
45+
public function setUp(): void
46+
{
47+
$this->parser = new Parser();
48+
$this->cachingParser = new CachingParser(new Parser());
49+
}
50+
51+
public function benchParserWithoutCache(): void
52+
{
53+
foreach ($this->types as $type) {
54+
$this->parser->parse($type);
55+
}
56+
}
57+
58+
public function benchParserWithCache(): void
59+
{
60+
foreach ($this->types as $type) {
61+
$this->cachingParser->parse($type);
62+
}
63+
}
64+
}

0 commit comments

Comments
 (0)