Skip to content

Commit 98bc6d8

Browse files
committed
added php-benchmark suite
1 parent bb779af commit 98bc6d8

File tree

7 files changed

+710
-0
lines changed

7 files changed

+710
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@
88
composer.lock
99
composer.phar
1010
vendor
11+
12+
#phpbench
13+
_storage

bench/EnumBench.php

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
<?php
2+
3+
namespace MabeEnumBench;
4+
5+
use MabeEnumTest\TestAsset\Enum66;
6+
7+
/**
8+
* Benchmark of abstract class Enum tested with enumeration of 66 enumerators.
9+
*
10+
* @BeforeMethods({"init"})
11+
* @Revs(2500)
12+
* @Iterations(25)
13+
*/
14+
class EnumBench
15+
{
16+
/**
17+
* @var string[]
18+
*/
19+
private $names;
20+
21+
/**
22+
* @var mixed[]
23+
*/
24+
private $values;
25+
26+
/**
27+
* @var int[]
28+
*/
29+
private $ordinals;
30+
31+
/**
32+
* @var Enum66[]
33+
*/
34+
private $enumerators;
35+
36+
/**
37+
* Will be called before every subject
38+
*/
39+
public function init()
40+
{
41+
$this->names = Enum66::getNames();
42+
$this->values = Enum66::getValues();
43+
$this->ordinals = Enum66::getOrdinals();
44+
$this->enumerators = Enum66::getEnumerators();
45+
}
46+
47+
public function benchGetName()
48+
{
49+
foreach ($this->enumerators as $enumerator) {
50+
$enumerator->getName();
51+
}
52+
}
53+
54+
public function benchGetValue()
55+
{
56+
foreach ($this->enumerators as $enumerator) {
57+
$enumerator->getValue();
58+
}
59+
}
60+
61+
public function benchGetOrdinal()
62+
{
63+
foreach ($this->enumerators as $enumerator) {
64+
$enumerator->getOrdinal();
65+
}
66+
}
67+
68+
public function benchIsByEnumerator()
69+
{
70+
foreach ($this->enumerators as $enumerator) {
71+
$enumerator->is($enumerator);
72+
}
73+
}
74+
75+
public function benchIsByValue()
76+
{
77+
foreach ($this->enumerators as $enumerator) {
78+
$enumerator->is($enumerator->getValue());
79+
}
80+
}
81+
82+
public function benchGetConstants()
83+
{
84+
Enum66::getConstants();
85+
}
86+
87+
public function benchGetValues()
88+
{
89+
Enum66::getValues();
90+
}
91+
92+
public function benchGetNames()
93+
{
94+
Enum66::getNames();
95+
}
96+
97+
public function benchGetOrdinals()
98+
{
99+
Enum66::getOrdinals();
100+
}
101+
102+
public function benchGetEnumerators()
103+
{
104+
Enum66::getEnumerators();
105+
}
106+
107+
public function benchByValue()
108+
{
109+
foreach ($this->values as $value) {
110+
Enum66::byValue($value);
111+
}
112+
}
113+
114+
public function benchByName()
115+
{
116+
foreach ($this->names as $name) {
117+
Enum66::byName($name);
118+
}
119+
}
120+
121+
public function benchByOrdinal()
122+
{
123+
foreach ($this->ordinals as $ord) {
124+
Enum66::byOrdinal($ord);
125+
}
126+
}
127+
128+
public function benchGetByValues()
129+
{
130+
foreach ($this->values as $value) {
131+
Enum66::get($value);
132+
}
133+
}
134+
135+
public function benchGetByEnumerator()
136+
{
137+
foreach ($this->enumerators as $enumerator) {
138+
Enum66::get($enumerator);
139+
}
140+
}
141+
142+
public function benchGetByCallStatic()
143+
{
144+
foreach ($this->names as $name) {
145+
Enum66::$name();
146+
}
147+
}
148+
149+
public function benchHasByEnumerator()
150+
{
151+
foreach ($this->enumerators as $enumerator) {
152+
Enum66::has($enumerator);
153+
}
154+
}
155+
156+
public function benchHasByValue()
157+
{
158+
foreach ($this->values as $value) {
159+
Enum66::has($value);
160+
}
161+
}
162+
}

bench/EnumMapBench.php

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<?php
2+
3+
namespace MabeEnumBench;
4+
5+
use MabeEnum\EnumMap;
6+
use MabeEnumTest\TestAsset\Enum66;
7+
8+
/**
9+
* Benchmark of EnumMap used with an enumeration of 66 enumerators.
10+
*
11+
* @BeforeMethods({"init"})
12+
* @Revs(2000)
13+
* @Iterations(25)
14+
*/
15+
class EnumMapBench
16+
{
17+
/**
18+
* @var mixed[]
19+
*/
20+
private $values;
21+
22+
/**
23+
* @var Enum66[]
24+
*/
25+
private $enumerators;
26+
27+
/**
28+
* @var EnumMap
29+
*/
30+
private $emptyMap;
31+
32+
/**
33+
* @var EnumMap
34+
*/
35+
private $fullMap;
36+
37+
/**
38+
* Will be called before every subject
39+
*/
40+
public function init()
41+
{
42+
$this->values = Enum66::getValues();
43+
$this->enumerators = Enum66::getEnumerators();
44+
45+
$this->emptyMap = new EnumMap(Enum66::class);
46+
$this->fullMap = new EnumMap(Enum66::class);
47+
foreach ($this->enumerators as $i => $enumerator) {
48+
$this->fullMap->offsetSet($enumerator, $i);
49+
}
50+
}
51+
52+
public function benchOffsetSetEnumerator()
53+
{
54+
foreach ($this->enumerators as $enumerator) {
55+
$this->emptyMap->offsetSet($enumerator);
56+
}
57+
}
58+
59+
public function benchOffsetSetValue()
60+
{
61+
foreach ($this->values as $value) {
62+
$this->emptyMap->offsetSet($value);
63+
}
64+
}
65+
66+
public function benchOffsetUnsetEnumerator()
67+
{
68+
foreach ($this->enumerators as $enumerator) {
69+
$this->fullMap->offsetUnset($enumerator);
70+
}
71+
}
72+
73+
public function benchOffsetUnsetValue()
74+
{
75+
foreach ($this->values as $value) {
76+
$this->fullMap->offsetUnset($value);
77+
}
78+
}
79+
80+
public function benchOffsetExistsEnumeratorTrue()
81+
{
82+
foreach ($this->enumerators as $enumerator) {
83+
$this->fullMap->offsetExists($enumerator);
84+
}
85+
}
86+
87+
public function benchOffsetExistsEnumeratorFalse()
88+
{
89+
foreach ($this->enumerators as $enumerator) {
90+
$this->fullMap->offsetExists($enumerator);
91+
}
92+
}
93+
94+
public function benchOffsetExistsValueTrue()
95+
{
96+
foreach ($this->values as $value) {
97+
$this->fullMap->offsetExists($value);
98+
}
99+
}
100+
101+
public function benchOffsetExistsValueFalse()
102+
{
103+
foreach ($this->values as $value) {
104+
$this->fullMap->offsetExists($value);
105+
}
106+
}
107+
108+
public function benchIterateFull()
109+
{
110+
foreach ($this->fullMap as $enumerator => $_) {
111+
$enumerator->getValue();
112+
}
113+
}
114+
115+
public function benchIterateEmpty()
116+
{
117+
foreach ($this->emptyMap as $enumerator => $_) {
118+
$enumerator->getValue();
119+
}
120+
}
121+
122+
public function benchCountFull()
123+
{
124+
$this->fullMap->count();
125+
}
126+
127+
public function benchCountEmpty()
128+
{
129+
$this->emptyMap->count();
130+
}
131+
}

0 commit comments

Comments
 (0)