|
| 1 | +<?php |
| 2 | + |
| 3 | +use Illuminate\Support\Collection; |
| 4 | +use Orchestra\Testbench\Concerns\CreatesApplication; |
| 5 | +use PhpBench\Attributes\BeforeMethods; |
| 6 | +use PhpBench\Attributes\Iterations; |
| 7 | +use PhpBench\Attributes\Revs; |
| 8 | +use PhpBench\Benchmark\Metadata\Annotations\Subject; |
| 9 | +use Spatie\LaravelData\LaravelDataServiceProvider; |
| 10 | +use Spatie\LaravelData\Tests\Fakes\ComplicatedData; |
| 11 | +use Spatie\LaravelData\Tests\Fakes\MultiNestedData; |
| 12 | +use Spatie\LaravelData\Tests\Fakes\SimpleData; |
| 13 | + |
| 14 | +class DataBench |
| 15 | +{ |
| 16 | + use CreatesApplication; |
| 17 | + |
| 18 | + public function __construct() |
| 19 | + { |
| 20 | + $this->createApplication(); |
| 21 | + } |
| 22 | + |
| 23 | + protected function getPackageProviders($app) |
| 24 | + { |
| 25 | + return [ |
| 26 | + LaravelDataServiceProvider::class, |
| 27 | + ]; |
| 28 | + } |
| 29 | + |
| 30 | + #[Revs(500), Iterations(2)] |
| 31 | + public function benchData() |
| 32 | + { |
| 33 | + MultiNestedData::from([ |
| 34 | + 'nested' => ['simple' => 'Hello'], |
| 35 | + 'nestedCollection' => [ |
| 36 | + ['simple' => 'I'], |
| 37 | + ['simple' => 'am'], |
| 38 | + ['simple' => 'groot'], |
| 39 | + ], |
| 40 | + ]); |
| 41 | + } |
| 42 | + |
| 43 | + #[Revs(500), Iterations(2)] |
| 44 | + public function benchDataCollection() |
| 45 | + { |
| 46 | + $collection = Collection::times( |
| 47 | + 15, |
| 48 | + fn() => [ |
| 49 | + 'withoutType' => 42, |
| 50 | + 'int' => 42, |
| 51 | + 'bool' => true, |
| 52 | + 'float' => 3.14, |
| 53 | + 'string' => 'Hello world', |
| 54 | + 'array' => [1, 1, 2, 3, 5, 8], |
| 55 | + 'nullable' => null, |
| 56 | + 'mixed' => 42, |
| 57 | + 'explicitCast' => '16-06-1994', |
| 58 | + 'defaultCast' => '1994-05-16T12:00:00+01:00', |
| 59 | + 'nestedData' => [ |
| 60 | + 'string' => 'hello', |
| 61 | + ], |
| 62 | + 'nestedCollection' => [ |
| 63 | + ['string' => 'never'], |
| 64 | + ['string' => 'gonna'], |
| 65 | + ['string' => 'give'], |
| 66 | + ['string' => 'you'], |
| 67 | + ['string' => 'up'], |
| 68 | + ], |
| 69 | + ] |
| 70 | + )->all(); |
| 71 | + |
| 72 | + ComplicatedData::collection($collection); |
| 73 | + } |
| 74 | +} |
0 commit comments