1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Phive Queue package. |
5
|
|
|
* |
6
|
|
|
* (c) Eugene Leonovich <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Phive\Queue\Tests\Queue; |
13
|
|
|
|
14
|
|
|
trait Performance |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @group performance |
18
|
|
|
* @dataProvider providePerformancePopDelay |
19
|
|
|
*/ |
20
|
|
|
public function testPushPopPerformance($delay) |
21
|
|
|
{ |
22
|
|
|
$queueSize = static::getPerformanceQueueSize(); |
23
|
|
|
$queueName = preg_replace('~^'.preg_quote(__NAMESPACE__).'\\\|Test$~', '', get_class($this)); |
24
|
|
|
$item = str_repeat('x', static::getPerformanceItemLength()); |
25
|
|
|
|
26
|
|
|
printf("\n%s::push()%s\n", $queueName, $delay ? ' (delayed)' : ''); |
27
|
|
|
|
28
|
|
|
$runtime = $this->benchmarkPush($queueSize, $item, $delay); |
29
|
|
|
$this->printPerformanceResult($queueSize, $runtime); |
30
|
|
|
|
31
|
|
|
if ($delay) { |
32
|
|
|
sleep($delay); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
printf("\n%s::pop()%s\n", $queueName, $delay ? ' (delayed)' : ''); |
36
|
|
|
|
37
|
|
|
$start = microtime(true); |
38
|
|
|
for ($i = $queueSize; $i; $i--) { |
39
|
|
|
$this->queue->pop(); |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$this->printPerformanceResult($queueSize, microtime(true) - $start); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function providePerformancePopDelay() |
46
|
|
|
{ |
47
|
|
|
return [[0], [1]]; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
protected function benchmarkPush($queueSize, $item, $delay) |
51
|
|
|
{ |
52
|
|
|
$eta = $delay ? time() + $delay : null; |
53
|
|
|
|
54
|
|
|
$start = microtime(true); |
55
|
|
|
for ($i = $queueSize; $i; $i--) { |
56
|
|
|
$this->queue->push($item, $eta); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return microtime(true) - $start; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
protected function printPerformanceResult($total, $runtime) |
63
|
|
|
{ |
64
|
|
|
printf(" Total operations: %d\n", $total); |
65
|
|
|
printf(" Operations per second: %01.3f [#/sec]\n", $total / $runtime); |
66
|
|
|
printf(" Time per operation: %01.3f [ms]\n", ($runtime / $total) * 1000000); |
67
|
|
|
printf(" Time taken for test: %01.3f [sec]\n", $runtime); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
protected static function getPerformanceQueueSize() |
71
|
|
|
{ |
72
|
|
|
return (int) getenv('PHIVE_PERF_QUEUE_SIZE'); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
protected static function getPerformanceItemLength() |
76
|
|
|
{ |
77
|
|
|
return (int) getenv('PHIVE_PERF_ITEM_LENGTH'); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: