From 75f5c6f543658a76d34f353322fa02cee27c93fe Mon Sep 17 00:00:00 2001 From: Ruben Van Assche Date: Mon, 4 Dec 2023 09:20:51 +0100 Subject: [PATCH] Benchmarking --- tests/DataBenchTest.php | 84 +++++++++++++++++++++++++++++++++ tests/Fakes/ComplicatedData.php | 22 ++++++++- tests/Fakes/NestedData.php | 7 +++ tests/Fakes/SimpleData.php | 7 +++ 4 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 tests/DataBenchTest.php diff --git a/tests/DataBenchTest.php b/tests/DataBenchTest.php new file mode 100644 index 00000000..b04ab70b --- /dev/null +++ b/tests/DataBenchTest.php @@ -0,0 +1,84 @@ +getDataClass(ComplicatedData::class); + app(DataConfig::class)->getDataClass(SimpleData::class); + app(DataConfig::class)->getDataClass(NestedData::class); + + $collection = Collection::times(1000, fn () => clone $data); + + $dataCollection = ComplicatedData::collect($collection, DataCollection::class); + + bench( + fn () => $dataCollection->toArray(), + fn () => $dataCollection->toCollection()->map(fn (ComplicatedData $data) => $data->toUserDefinedToArray()), + name: 'collection', + times: 10 + ); + + bench( + fn() => $data->toArray(), + fn() => $data->toUserDefinedToArray(), + name: 'single', + ); +}); + +function benchSingle(Closure $closure, $times): float{ + $start = microtime(true); + + for ($i = 0; $i < $times; $i++) { + $closure(); + } + + $end = microtime(true); + + return ($end - $start) / $times; +} + +function bench(Closure $data, Closure $userDefined, string $name, $times = 100): void +{ + $dataBench = benchSingle($data, $times); + $userDefinedBench = benchSingle($userDefined, $times); + + dump("{$name} data - " . number_format($dataBench, 10)); + dump("{$name} user defined - " . number_format($userDefinedBench, 10)); + dump("{$name} data is " . round($dataBench / $userDefinedBench,0) . " times slower than user defined"); + +} + + diff --git a/tests/Fakes/ComplicatedData.php b/tests/Fakes/ComplicatedData.php index 12e6eeb4..e815a03b 100644 --- a/tests/Fakes/ComplicatedData.php +++ b/tests/Fakes/ComplicatedData.php @@ -26,11 +26,31 @@ public function __construct( #[WithCast(DateTimeInterfaceCast::class, format: 'd-m-Y', type: CarbonImmutable::class)] public $explicitCast, public DateTime $defaultCast, - public SimpleData $nestedData, + public ?SimpleData $nestedData, /** @var \Spatie\LaravelData\Tests\Fakes\SimpleData[] */ public DataCollection $nestedCollection, #[DataCollectionOf(SimpleData::class)] public array $nestedArray, ) { } + + public function toUserDefinedToArray(): array + { + return [ + 'withoutType' => $this->withoutType, + 'int' => $this->int, + 'bool' => $this->bool, + 'float' => $this->float, + 'string' => $this->string, + 'array' => $this->array, + 'nullable' => $this->nullable, + 'undefinable' => $this->undefinable, + 'mixed' => $this->mixed, + 'explicitCast' => $this->explicitCast, + 'defaultCast' => $this->defaultCast, + 'nestedData' => $this->nestedData?->toUserDefinedToArray(), + 'nestedCollection' => array_map(fn(NestedData $data) => $data->toUserDefinedToArray(), $this->nestedCollection->toCollection()->all()), + 'nestedArray' => array_map(fn(NestedData $data) => $data->toUserDefinedToArray(), $this->nestedArray), + ]; + } } diff --git a/tests/Fakes/NestedData.php b/tests/Fakes/NestedData.php index 20beb41d..d4f24fc7 100644 --- a/tests/Fakes/NestedData.php +++ b/tests/Fakes/NestedData.php @@ -10,4 +10,11 @@ public function __construct( public SimpleData $simple ) { } + + public function toUserDefinedToArray(): array + { + return [ + 'simple' => $this->simple->toUserDefinedToArray(), + ]; + } } diff --git a/tests/Fakes/SimpleData.php b/tests/Fakes/SimpleData.php index bfe4b97a..1fb270fe 100644 --- a/tests/Fakes/SimpleData.php +++ b/tests/Fakes/SimpleData.php @@ -15,4 +15,11 @@ public static function fromString(string $string): self { return new self($string); } + + public function toUserDefinedToArray(): array + { + return [ + 'string' => $this->string, + ]; + } }