|
| 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