|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator\Tests\Benchmark; |
|
6
|
|
|
|
|
7
|
|
|
use Generator; |
|
8
|
|
|
use PhpBench\Benchmark\Metadata\Annotations\BeforeMethods; |
|
9
|
|
|
use PhpBench\Benchmark\Metadata\Annotations\Iterations; |
|
10
|
|
|
use PhpBench\Benchmark\Metadata\Annotations\ParamProviders; |
|
11
|
|
|
use PhpBench\Benchmark\Metadata\Annotations\Revs; |
|
12
|
|
|
use PhpBench\Benchmark\Metadata\Annotations\Warmup; |
|
13
|
|
|
use Yiisoft\Validator\Rule\BooleanValue; |
|
14
|
|
|
use Yiisoft\Validator\Rule\Integer; |
|
15
|
|
|
use Yiisoft\Validator\Validator; |
|
16
|
|
|
use Yiisoft\Validator\ValidatorInterface; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @BeforeMethods("setUp") |
|
20
|
|
|
*/ |
|
21
|
|
|
final class MainBench |
|
22
|
|
|
{ |
|
23
|
|
|
private ValidatorInterface $validator; |
|
24
|
|
|
|
|
25
|
|
|
public function setUp(array $params): void |
|
|
|
|
|
|
26
|
|
|
{ |
|
27
|
|
|
$this->validator = new Validator(); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @Revs(1000) |
|
32
|
|
|
* |
|
33
|
|
|
* @Iterations(10) |
|
34
|
|
|
* |
|
35
|
|
|
* @ParamProviders("provider") |
|
36
|
|
|
* |
|
37
|
|
|
* @Warmup(1) |
|
38
|
|
|
*/ |
|
39
|
|
|
public function benchValidate(array $params): void |
|
40
|
|
|
{ |
|
41
|
|
|
$rules = $this->generateRules($params['rules'], $params['count']); |
|
42
|
|
|
$this->validator->validate($params['data'], $rules); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function provider(): Generator |
|
46
|
|
|
{ |
|
47
|
|
|
$data = [ |
|
48
|
|
|
'bool' => true, |
|
49
|
|
|
'int' => 555, |
|
50
|
|
|
]; |
|
51
|
|
|
yield 'simple 1' => [ |
|
52
|
|
|
'data' => $data, |
|
53
|
|
|
'rules' => [ |
|
54
|
|
|
'bool' => new BooleanValue(), |
|
55
|
|
|
'int' => new Integer(), |
|
56
|
|
|
], |
|
57
|
|
|
'count' => 1, |
|
58
|
|
|
]; |
|
59
|
|
|
yield 'simple 10' => [ |
|
60
|
|
|
'data' => $data, |
|
61
|
|
|
'rules' => [ |
|
62
|
|
|
'bool' => new BooleanValue(), |
|
63
|
|
|
'int' => new Integer(), |
|
64
|
|
|
], |
|
65
|
|
|
'count' => 10, |
|
66
|
|
|
]; |
|
67
|
|
|
yield 'simple 100' => [ |
|
68
|
|
|
'data' => $data, |
|
69
|
|
|
'rules' => [ |
|
70
|
|
|
'bool' => new BooleanValue(), |
|
71
|
|
|
'int' => new Integer(), |
|
72
|
|
|
], |
|
73
|
|
|
'count' => 100, |
|
74
|
|
|
]; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function generateRules(array $rules, int $count): iterable |
|
78
|
|
|
{ |
|
79
|
|
|
foreach ($rules as $attribute => $rule) { |
|
80
|
|
|
yield $attribute => $this->cloneRule($rule, $count); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
private function cloneRule(mixed $rule, int $count): Generator |
|
85
|
|
|
{ |
|
86
|
|
|
for ($i = 0; $i < $count; $i++) { |
|
87
|
|
|
yield clone $rule; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.