Skip to content

Commit

Permalink
Initial work on extra benchmarking
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenvanassche committed Oct 20, 2023
1 parent f47d3f0 commit 10cce0c
Show file tree
Hide file tree
Showing 5 changed files with 283 additions and 70 deletions.
96 changes: 32 additions & 64 deletions benchmarks/DataBench.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
<?php

use Carbon\CarbonImmutable;
use Illuminate\Support\Collection;
use Orchestra\Testbench\Concerns\CreatesApplication;
use PhpBench\Attributes\BeforeMethods;
use PhpBench\Attributes\Iterations;
use PhpBench\Attributes\Revs;
use PhpBench\Benchmark\Metadata\Annotations\Subject;
use Spatie\LaravelData\DataCollection;
use Spatie\LaravelData\LaravelDataServiceProvider;
use Spatie\LaravelData\Optional;
use Spatie\LaravelData\Tests\Fakes\ComplicatedData;
use Spatie\LaravelData\Tests\Fakes\MultiNestedData;
use Spatie\LaravelData\Tests\Fakes\NestedData;
use Spatie\LaravelData\Tests\Fakes\SimpleData;
Expand Down Expand Up @@ -44,6 +38,19 @@ public function benchDataCreation()
]);
}

#[Revs(500), Iterations(2)]
public function benchPlainOldPHPDataCreation()
{
new MultiNestedData(
nested: new NestedData(new SimpleData('Hello')),
nestedCollection: new DataCollection(SimpleData::class, [
new SimpleData('I'),
new SimpleData('am'),
new SimpleData('groot'),
]),
);
}

#[Revs(500), Iterations(2)]
public function benchDataTransformation()
{
Expand All @@ -60,65 +67,26 @@ public function benchDataTransformation()
}

#[Revs(500), Iterations(2)]
public function benchDataCollectionCreation()
public function benchPlainOldPHPDataTransformation()
{
$collection = Collection::times(
15,
fn() => [
'withoutType' => 42,
'int' => 42,
'bool' => true,
'float' => 3.14,
'string' => 'Hello world',
'array' => [1, 1, 2, 3, 5, 8],
'nullable' => null,
'mixed' => 42,
'explicitCast' => '16-06-1994',
'defaultCast' => '1994-05-16T12:00:00+01:00',
'nestedData' => [
'string' => 'hello',
],
'nestedCollection' => [
['string' => 'never'],
['string' => 'gonna'],
['string' => 'give'],
['string' => 'you'],
['string' => 'up'],
],
]
)->all();

ComplicatedData::collection($collection);
}

#[Revs(500), Iterations(2)]
public function benchDataCollectionTransformation()
{
$collection = Collection::times(
15,
fn() => 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')),
])
)
)->all();

$collection = ComplicatedData::collection($collection);
$data = new MultiNestedData(
new NestedData(new SimpleData('Hello')),
new DataCollection(NestedData::class, [
new NestedData(new SimpleData('I')),
new NestedData(new SimpleData('am')),
new NestedData(new SimpleData('groot')),
])
);

$collection->toArray();
[
'nested' => [
'simple' => $data->nested->simple,
],
'nestedCollection' => $data->nestedCollection->toCollection()->map(
fn (NestedData $nestedData) => [
'simple' => $nestedData->simple,
]
)->all(),
];
}
}
238 changes: 238 additions & 0 deletions benchmarks/DataCollectionBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@
<?php

use Carbon\CarbonImmutable;
use Illuminate\Support\Collection;
use Orchestra\Testbench\Concerns\CreatesApplication;
use PhpBench\Attributes\Iterations;
use PhpBench\Attributes\ParamProviders;
use PhpBench\Attributes\Revs;
use Spatie\LaravelData\DataCollection;
use Spatie\LaravelData\LaravelDataServiceProvider;
use Spatie\LaravelData\Optional;
use Spatie\LaravelData\Tests\Fakes\ComplicatedData;
use Spatie\LaravelData\Tests\Fakes\MultiNestedData;
use Spatie\LaravelData\Tests\Fakes\NestedData;
use Spatie\LaravelData\Tests\Fakes\SimpleData;

class DataCollectionBench
{
use CreatesApplication;

public function __construct()
{
$this->createApplication();
}

protected function getPackageProviders($app)
{
return [
LaravelDataServiceProvider::class,
];
}

#[Revs(10), Iterations(20), ParamProviders(['provideItemCount'])]
public function benchDataCollectionCreation(array $params)
{
$collection = Collection::times(
$params['itemCount'],
fn () => [
'withoutType' => 42,
'int' => 42,
'bool' => true,
'float' => 3.14,
'string' => 'Hello world',
'array' => [1, 1, 2, 3, 5, 8],
'nullable' => null,
'mixed' => 42,
'explicitCast' => '16-06-1994',
'defaultCast' => '1994-05-16T12:00:00+01:00',
'nestedData' => [
'string' => 'hello',
],
'nestedCollection' => [
['string' => 'never'],
['string' => 'gonna'],
['string' => 'give'],
['string' => 'you'],
['string' => 'up'],
],
]
)->all();

ComplicatedData::collection($collection);
}

#[Revs(10), Iterations(20), ParamProviders(['provideItemCount'])]
public function benchDataArrayCollectionCreation(array $params)
{
Collection::times(
$params['itemCount'],
fn () => [
'withoutType' => 42,
'int' => 42,
'bool' => true,
'float' => 3.14,
'string' => 'Hello world',
'array' => [1, 1, 2, 3, 5, 8],
'nullable' => null,
'mixed' => 42,
'explicitCast' => '16-06-1994',
'defaultCast' => '1994-05-16T12:00:00+01:00',
'nestedData' => [
'string' => 'hello',
],
'nestedCollection' => [
['string' => 'never'],
['string' => 'gonna'],
['string' => 'give'],
['string' => 'you'],
['string' => 'up'],
],
]
)->map(fn (array $data) => ComplicatedData::from($data))->all();
}

#[Revs(10), Iterations(20), ParamProviders(['provideItemCount'])]
public function benchPlainOldPHPDataCollectionCreation(array $params)
{
$collection = Collection::times(
$params['itemCount'],
fn () => 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('never')),
new NestedData(new SimpleData('gonna')),
new NestedData(new SimpleData('give')),
new NestedData(new SimpleData('you')),
new NestedData(new SimpleData('up')),
])
)
)->all();

new DataCollection(ComplicatedData::class, $collection);
}

#[Revs(10), Iterations(20), ParamProviders(['provideItemCount'])]
public function benchDataCollectionTransformation(array $params)
{
$collection = Collection::times(
$params['itemCount'],
fn () => 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')),
])
)
)->all();

$collection = ComplicatedData::collection($collection);

$collection->toArray();
}

#[Revs(10), Iterations(20), ParamProviders(['provideItemCount'])]
public function benchDataArrayTransformation(array $params)
{
Collection::times(
$params['itemCount'],
fn () => 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')),
])
)
)->toArray();
}

#[Revs(50), Iterations(40), ParamProviders(['provideItemCount'])]
public function benchPlainOldPHPArrayTransformation(array $params)
{
Collection::times(
$params['itemCount'],
fn () => 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')),
])
))->map(function (ComplicatedData $data) {
return [
'withoutType' => $data->withoutType,
'int' => $data->int,
'bool' => $data->bool,
'float' => $data->float,
'string' => $data->string,
'array' => $data->array,
'nullable' => $data->nullable,
'undefinable' => $data->undefinable,
'mixed' => $data->mixed,
'explicitCast' => $data->explicitCast,
'defaultCast' => $data->defaultCast,
'nestedData' => [
'string' => $data->nestedData->string,
],
'nestedCollection' => $data->nestedCollection->toCollection()->map(function (NestedData $data) {
return [
'string' => $data->simple->string,
];
})->all(),
];
});
}

function provideItemCount(): Generator
{
yield '10 items' => ['itemCount' => 10];
yield '100 items' => ['itemCount' => 100];
yield '1000 items' => ['itemCount' => 1000];
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"test" : "./vendor/bin/pest --no-coverage",
"test-coverage" : "vendor/bin/pest --coverage-html coverage",
"format" : "vendor/bin/php-cs-fixer fix --allow-risky=yes",
"benchmark" : "vendor/bin/phpbench run --report=default",
"benchmark" : "vendor/bin/phpbench run --report=aggregate",
"benchmark-profiled" : "vendor/bin/phpbench xdebug:profile"
},
"config" : {
Expand Down
6 changes: 5 additions & 1 deletion src/Transformers/DataTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,16 @@ class DataTransformer
{
protected DataConfig $config;

private static ?self $instance = null;

public static function create(
bool $transformValues,
WrapExecutionType $wrapExecutionType,
bool $mapPropertyNames,
): self {
return new self($transformValues, $wrapExecutionType, $mapPropertyNames);
return self::$instance ??= new self($transformValues, $wrapExecutionType, $mapPropertyNames);

// return new self($transformValues, $wrapExecutionType, $mapPropertyNames);
}

public function __construct(
Expand Down
Loading

0 comments on commit 10cce0c

Please sign in to comment.