Skip to content

Commit 7dc96ae

Browse files
committed
Add benchmark
1 parent 8cf34f2 commit 7dc96ae

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

Diff for: composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
},
3434
"require-dev": {
3535
"dms/phpunit-arraysubset-asserts": "^0.5.0",
36+
"phpbench/phpbench": "^1.2",
3637
"phpunit/phpunit": "^10.2.2"
3738
},
3839
"scripts": {

Diff for: phpbench.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"$schema": "vendor/phpbench/phpbench/phpbench.schema.json",
3+
"runner.bootstrap": "vendor/autoload.php",
4+
"runner.path": "tests/benchmark"
5+
}

Diff for: tests/benchmark/CompoundBench.php

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
namespace Tobyz\Tests\JsonApiServer\benchmark;
4+
5+
use Nyholm\Psr7\ServerRequest;
6+
use PhpBench\Attributes\BeforeMethods;
7+
use PhpBench\Attributes\Iterations;
8+
use PhpBench\Attributes\Revs;
9+
use Tobyz\JsonApiServer\Endpoint\Index;
10+
use Tobyz\JsonApiServer\JsonApi;
11+
use Tobyz\JsonApiServer\Schema\Field\Str;
12+
use Tobyz\JsonApiServer\Schema\Field\ToMany;
13+
use Tobyz\JsonApiServer\Schema\Field\ToOne;
14+
use Tobyz\Tests\JsonApiServer\MockResource;
15+
16+
#[BeforeMethods('setUp')]
17+
class CompoundBench
18+
{
19+
private JsonApi $api;
20+
21+
public function setUp()
22+
{
23+
$this->api = new JsonApi();
24+
25+
$this->api->resource(
26+
new MockResource(
27+
'people',
28+
models: [
29+
($user2 = (object) ['id' => '2']),
30+
($user9 = (object) [
31+
'id' => '9',
32+
'firstName' => 'Dan',
33+
'lastName' => 'Gebhardt',
34+
'twitter' => 'dgeb',
35+
]),
36+
],
37+
fields: [Str::make('firstName'), Str::make('lastName'), Str::make('twitter')],
38+
),
39+
);
40+
41+
$this->api->resource(
42+
new MockResource(
43+
'comments',
44+
models: [
45+
($comment5 = (object) ['id' => '5', 'body' => 'First!', 'author' => $user2]),
46+
($comment12 = (object) [
47+
'id' => '12',
48+
'body' => 'I like XML better',
49+
'author' => $user9,
50+
]),
51+
],
52+
fields: [Str::make('body'), ToOne::make('author')->type('people')],
53+
),
54+
);
55+
56+
$this->api->resource(
57+
new MockResource(
58+
'articles',
59+
models: [
60+
'1' => (object) [
61+
'id' => '1',
62+
'title' => 'JSON:API paints my bikeshed!',
63+
'author' => $user9,
64+
'comments' => [$comment5, $comment12],
65+
],
66+
],
67+
endpoints: [Index::make()],
68+
fields: [
69+
Str::make('title'),
70+
ToOne::make('author')
71+
->type('people')
72+
->includable(),
73+
ToMany::make('comments')->includable(),
74+
],
75+
),
76+
);
77+
}
78+
79+
#[Revs(10000), Iterations(5)]
80+
public function benchCompound(): void
81+
{
82+
$this->api->handle(new ServerRequest('GET', '/articles?include=author,comments'));
83+
}
84+
}

0 commit comments

Comments
 (0)