Skip to content

Commit

Permalink
Merge pull request #383 from spatie/benchmarks-pipeline
Browse files Browse the repository at this point in the history
Benchmarks pipeline
  • Loading branch information
rubenvanassche authored Mar 17, 2023
2 parents da6ee60 + 9465cb8 commit c4f6329
Show file tree
Hide file tree
Showing 15 changed files with 316 additions and 148 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/benchmark.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Benchmarks

on:
push:
paths:
- '**.php'
- 'phpbench.json'
pull_request:
paths:
- '**.php'
- 'phpbench.json'

jobs:
phpstan:
name: phpstan
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.2'
coverage: none

- name: Install composer dependencies
uses: ramsey/composer-install@v2

- name: Run Benchmark
run: composer benchmark
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ testbench.yaml
vendor
node_modules
.php-cs-fixer.cache
.phpbench
.DS_Store
124 changes: 124 additions & 0 deletions benchmarks/DataBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?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;

class DataBench
{
use CreatesApplication;

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

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

#[Revs(500), Iterations(2)]
public function benchDataCreation()
{
MultiNestedData::from([
'nested' => ['simple' => 'Hello'],
'nestedCollection' => [
['simple' => 'I'],
['simple' => 'am'],
['simple' => 'groot'],
],
]);
}

#[Revs(500), Iterations(2)]
public function benchDataTransformation()
{
$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')),
])
);

$data->toArray();
}

#[Revs(500), Iterations(2)]
public function benchDataCollectionCreation()
{
$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);

$collection->toArray();
}
}
38 changes: 20 additions & 18 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,23 @@
"spatie/laravel-package-tools" : "^1.9.0"
},
"require-dev" : {
"fakerphp/faker": "^1.14",
"friendsofphp/php-cs-fixer": "^3.0",
"inertiajs/inertia-laravel": "^0.6.3",
"nesbot/carbon": "^2.63",
"nette/php-generator": "^3.5",
"nunomaduro/larastan": "^2.0",
"orchestra/testbench": "^7.6|^8.0",
"pestphp/pest": "^1.22",
"pestphp/pest-plugin-laravel": "^1.3",
"phpbench/phpbench": "^1.2",
"phpstan/extension-installer": "^1.1",
"phpunit/phpunit": "^9.3",
"spatie/invade": "^1.0",
"spatie/laravel-typescript-transformer": "^2.1.6",
"spatie/pest-plugin-snapshots": "^1.1",
"spatie/phpunit-snapshot-assertions": "^4.2",
"spatie/test-time": "^1.2"
"fakerphp/faker" : "^1.14",
"friendsofphp/php-cs-fixer" : "^3.0",
"inertiajs/inertia-laravel" : "^0.6.3",
"nesbot/carbon" : "^2.63",
"nette/php-generator" : "^3.5",
"nunomaduro/larastan" : "^2.0",
"orchestra/testbench" : "^7.6|^8.0",
"pestphp/pest" : "^1.22",
"pestphp/pest-plugin-laravel" : "^1.3",
"phpbench/phpbench" : "^1.2",
"phpstan/extension-installer" : "^1.1",
"phpunit/phpunit" : "^9.3",
"spatie/invade" : "^1.0",
"spatie/laravel-typescript-transformer" : "^2.1.6",
"spatie/pest-plugin-snapshots" : "^1.1",
"spatie/phpunit-snapshot-assertions" : "^4.2",
"spatie/test-time" : "^1.2"
},
"autoload" : {
"psr-4" : {
Expand All @@ -55,7 +55,9 @@
"analyse" : "vendor/bin/phpstan analyse",
"test" : "./vendor/bin/pest --no-coverage",
"test-coverage" : "vendor/bin/pest --coverage-html coverage",
"format" : "vendor/bin/php-cs-fixer fix --allow-risky=yes"
"format" : "vendor/bin/php-cs-fixer fix --allow-risky=yes",
"benchmark" : "vendor/bin/phpbench run --report=default",
"benchmark-profiled" : "vendor/bin/phpbench xdebug:profile"
},
"config" : {
"sort-packages" : true,
Expand Down
5 changes: 5 additions & 0 deletions phpbench.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"$schema":"./vendor/phpbench/phpbench/phpbench.schema.json",
"runner.bootstrap": "vendor/autoload.php",
"runner.path" : "benchmarks"
}
9 changes: 0 additions & 9 deletions phpbench.json.dist

This file was deleted.

56 changes: 20 additions & 36 deletions src/DataPipeline.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

use Illuminate\Support\Collection;
use Spatie\LaravelData\DataPipes\DataPipe;
use Spatie\LaravelData\Exceptions\CannotCreateData;
use Spatie\LaravelData\Normalizers\Normalizer;
use Spatie\LaravelData\Support\DataConfig;
use Spatie\LaravelData\Support\ResolvedDataPipeline;

class DataPipeline
{
Expand All @@ -27,13 +27,6 @@ public static function create(): static
return app(static::class);
}

public function using(mixed $value): static
{
$this->value = $value;

return $this;
}

public function into(string $classString): static
{
$this->classString = $classString;
Expand Down Expand Up @@ -62,12 +55,17 @@ public function firstThrough(string|DataPipe $pipe): static
return $this;
}

public function execute(): Collection
public function resolve(): ResolvedDataPipeline
{
$normalizers = array_merge(
$this->normalizers,
$this->classString::normalizers()
);

/** @var \Spatie\LaravelData\Normalizers\Normalizer[] $normalizers */
$normalizers = array_map(
fn (string|Normalizer $normalizer) => is_string($normalizer) ? app($normalizer) : $normalizer,
$this->normalizers
$normalizers
);

/** @var \Spatie\LaravelData\DataPipes\DataPipe[] $pipes */
Expand All @@ -76,32 +74,18 @@ public function execute(): Collection
$this->pipes
);

$properties = null;

foreach ($normalizers as $normalizer) {
$properties = $normalizer->normalize($this->value);

if ($properties !== null) {
break;
}
}

if ($properties === null) {
throw CannotCreateData::noNormalizerFound($this->classString, $this->value);
}

$properties = collect($properties);

$class = $this->dataConfig->getDataClass($this->classString);

$properties = ($class->name)::prepareForPipeline($properties);

foreach ($pipes as $pipe) {
$piped = $pipe->handle($this->value, $class, $properties);

$properties = $piped;
}
return new ResolvedDataPipeline(
$normalizers,
$pipes,
$this->dataConfig->getDataClass($this->classString)
);
}

return $properties;
/** @deprecated */
public function execute(): Collection
{
return $this->dataConfig
->getResolvedDataPipeline($this->classString)
->execute($this->value);
}
}
47 changes: 12 additions & 35 deletions src/Resolvers/DataFromSomethingResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Spatie\LaravelData\Contracts\BaseData;
use Spatie\LaravelData\Normalizers\ArrayableNormalizer;
use Spatie\LaravelData\Support\DataConfig;
use Spatie\LaravelData\Support\DataMethod;

Expand Down Expand Up @@ -39,35 +38,15 @@ public function execute(string $class, mixed ...$payloads): BaseData
return $data;
}

// $properties = new Collection();
//
// foreach ($payloads as $payload) {
// /** @var BaseData $class */
// $pipeline = $class::pipeline();
//
// foreach ($class::normalizers() as $normalizer) {
// $pipeline->normalizer($normalizer);
// }
//
// foreach ($pipeline->using($payload)->execute() as $key => $value) {
// $properties[$key] = $value;
// }
// }

$properties = array_reduce(
$payloads,
function (Collection $carry, mixed $payload) use ($class) {
/** @var BaseData $class */
$pipeline = $class::pipeline();

foreach ($class::normalizers() as $normalizer) {
$pipeline->normalizer($normalizer);
}

return $carry->merge($pipeline->using($payload)->execute());
},
collect(),
);
$properties = new Collection();

$pipeline = $this->dataConfig->getResolvedDataPipeline($class);

foreach ($payloads as $payload) {
foreach ($pipeline->execute($payload) as $key => $value) {
$properties[$key] = $value;
}
}

return $this->dataFromArrayResolver->execute($class, $properties);
}
Expand Down Expand Up @@ -101,13 +80,11 @@ protected function createFromCustomCreationMethod(string $class, array $payloads
return null;
}

$pipeline = $this->dataConfig->getResolvedDataPipeline($class);

foreach ($payloads as $payload) {
if ($payload instanceof Request) {
$class::pipeline()
->normalizer(ArrayableNormalizer::class)
->into($class)
->using($payload)
->execute();
$pipeline->execute($payload);
}
}

Expand Down
Loading

0 comments on commit c4f6329

Please sign in to comment.