Skip to content

Commit

Permalink
Benchmarking
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenvanassche committed Dec 4, 2023
1 parent 9a310d9 commit 75f5c6f
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 1 deletion.
84 changes: 84 additions & 0 deletions tests/DataBenchTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

use Carbon\CarbonImmutable;
use Illuminate\Support\Collection;
use Spatie\LaravelData\DataCollection;
use Spatie\LaravelData\Optional;
use Spatie\LaravelData\Support\DataConfig;
use Spatie\LaravelData\Tests\Fakes\ComplicatedData;
use Spatie\LaravelData\Tests\Fakes\NestedData;
use Spatie\LaravelData\Tests\Fakes\SimpleData;

it('can transform collected data', function () {
$data = new ComplicatedData(
42,
42,
true,
3.14,
'Hello World',
[1, 1, 2, 3, 5, 8],
null,
Optional::create(),
42,
CarbonImmutable::create(1994, 05, 16),
new DateTime('1994-05-16T12:00:00+01:00'),
new SimpleData('hello'),
new DataCollection(NestedData::class, [
new NestedData(new SimpleData('I')),
new NestedData(new SimpleData('am')),
new NestedData(new SimpleData('groot')),
]),
[
new NestedData(new SimpleData('I')),
new NestedData(new SimpleData('am')),
new NestedData(new SimpleData('groot')),
],
);

// Preload data class info
app(DataConfig::class)->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");

}


22 changes: 21 additions & 1 deletion tests/Fakes/ComplicatedData.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
];
}
}
7 changes: 7 additions & 0 deletions tests/Fakes/NestedData.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,11 @@ public function __construct(
public SimpleData $simple
) {
}

public function toUserDefinedToArray(): array
{
return [
'simple' => $this->simple->toUserDefinedToArray(),
];
}
}
7 changes: 7 additions & 0 deletions tests/Fakes/SimpleData.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,11 @@ public static function fromString(string $string): self
{
return new self($string);
}

public function toUserDefinedToArray(): array
{
return [
'string' => $this->string,
];
}
}

0 comments on commit 75f5c6f

Please sign in to comment.