|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * SPDX-License-Identifier: MIT |
| 5 | + * SPDX-FileCopyrightText: (c) Respect Project Contributors |
| 6 | + */ |
| 7 | + |
| 8 | +declare(strict_types=1); |
| 9 | + |
| 10 | +namespace Respect\Validation\Benchmarks; |
| 11 | + |
| 12 | +use Generator; |
| 13 | +use PhpBench\Attributes as Bench; |
| 14 | +use Respect\Validation\Validator; |
| 15 | +use Respect\Validation\ValidatorBuilder; |
| 16 | +use Respect\Validation\Validators\Alnum; |
| 17 | +use Respect\Validation\Validators\Alpha; |
| 18 | +use Respect\Validation\Validators\BoolType; |
| 19 | +use Respect\Validation\Validators\Digit; |
| 20 | +use Respect\Validation\Validators\Even; |
| 21 | +use Respect\Validation\Validators\FloatType; |
| 22 | +use Respect\Validation\Validators\IntType; |
| 23 | +use Respect\Validation\Validators\Negative; |
| 24 | +use Respect\Validation\Validators\Positive; |
| 25 | +use Respect\Validation\Validators\StringType; |
| 26 | + |
| 27 | +final class CompositeValidatorsBench |
| 28 | +{ |
| 29 | + /** @param array{string, array<Validator>} $params */ |
| 30 | + #[Bench\ParamProviders(['provideValidatorBuilder'])] |
| 31 | + #[Bench\Iterations(5)] |
| 32 | + #[Bench\Revs(50)] |
| 33 | + #[Bench\Warmup(1)] |
| 34 | + #[Bench\Subject] |
| 35 | + public function isValid(array $params): void |
| 36 | + { |
| 37 | + ValidatorBuilder::__callStatic(...$params)->isValid(42); |
| 38 | + } |
| 39 | + |
| 40 | + public function provideValidatorBuilder(): Generator |
| 41 | + { |
| 42 | + yield 'allOf(10)' => ['allOf', $this->buildValidators(10)]; |
| 43 | + yield 'oneOf(10)' => ['oneOf', $this->buildValidators(10)]; |
| 44 | + yield 'anyOf(10)' => ['anyOf', $this->buildValidators(10)]; |
| 45 | + yield 'noneOf(10)' => ['noneOf', $this->buildValidators(10)]; |
| 46 | + yield 'allOf(100)' => ['allOf', $this->buildValidators(100)]; |
| 47 | + yield 'oneOf(100)' => ['oneOf', $this->buildValidators(100)]; |
| 48 | + yield 'anyOf(100)' => ['anyOf', $this->buildValidators(100)]; |
| 49 | + yield 'noneOf(100)' => ['noneOf', $this->buildValidators(100)]; |
| 50 | + } |
| 51 | + |
| 52 | + /** @return array<Validator> */ |
| 53 | + private function buildValidators(int $count): array |
| 54 | + { |
| 55 | + $validators = []; |
| 56 | + for ($i = 0; $i < $count; $i++) { |
| 57 | + $validators[] = $this->makeValidator($i); |
| 58 | + } |
| 59 | + |
| 60 | + return $validators; |
| 61 | + } |
| 62 | + |
| 63 | + private function makeValidator(int $index): Validator |
| 64 | + { |
| 65 | + return match ($index % 10) { |
| 66 | + 0 => new IntType(), |
| 67 | + 1 => new Positive(), |
| 68 | + 2 => new Negative(), |
| 69 | + 3 => new Even(), |
| 70 | + 4 => new FloatType(), |
| 71 | + 5 => new StringType(), |
| 72 | + 6 => new Alpha(), |
| 73 | + 7 => new Alnum(), |
| 74 | + 8 => new Digit(), |
| 75 | + default => new BoolType(), |
| 76 | + }; |
| 77 | + } |
| 78 | +} |
0 commit comments