Skip to content

Commit fb25bd2

Browse files
committed
Updated and unit tested
1 parent 506c997 commit fb25bd2

23 files changed

+597
-161
lines changed

README.md

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,206 @@
11
# Benchmark
2+
3+
## Install
4+
```shell
5+
$ composer require best-served-cold/benchmark
6+
```
7+
8+
## Usage
9+
Call in the Benchmark and Output classes
10+
```php
11+
use BestServedCold\Benchmark\Benchmark;
12+
use BestServedCold\Benchmark\Output;
13+
```
14+
15+
Then benchmark a bunch of code:
16+
```php
17+
Benchmark::start();
18+
for ($i = 0; $i <= 100; $i++ ) {
19+
mt_rand(1, 1000);
20+
}
21+
Benchmark::stop();
22+
Output::output(Benchmark::get())->render();
23+
```
24+
25+
Returns something like this:
26+
```shell
27+
+---------------+-------------------+--------------------+
28+
| Name | Metric | Value |
29+
+---------------+-------------------+--------------------+
30+
| 58c4302514614 | MicroTime | 0.0022561550140381 |
31+
| 58c4302514614 | MemoryUsage | 160.05 KB |
32+
| 58c4302514614 | DeclaredInterface | 0 |
33+
| 58c4302514614 | DeclaredTrait | 2 |
34+
| 58c4302514614 | DefinedFunction | 0 |
35+
| 58c4302514614 | DefinedConstant | 0 |
36+
| 58c4302514614 | IncludedFile | 1 |
37+
| 58c4302514614 | DeclaredClass | 0 |
38+
| 58c4302514614 | PeakMemoryUsage | 1.54 MB |
39+
+---------------+-------------------+--------------------+
40+
```
41+
42+
As no $name argument is specified, a hashed name will be given
43+
as means of identification.
44+
45+
## Naming benchmarks
46+
```php
47+
Benchmark::reset(); // Clear existing benchmarks
48+
Benchmark::start('bob');
49+
$a = str_repeat('Hello', 24000);
50+
define('FOO', 'bar');
51+
Benchmark::stop('bob');
52+
Benchmark::start('mary');
53+
require_once('./SomeClass.php');
54+
sleep(1);
55+
Benchmark::stop('mary');
56+
```
57+
58+
Returns something like:
59+
```shell
60+
+------+-------------------+--------------------+
61+
| Name | Metric | Value |
62+
+------+-------------------+--------------------+
63+
| bob | MicroTime | 0.0014297962188721 |
64+
| bob | MemoryUsage | 191.77 KB |
65+
| bob | DeclaredInterface | 0 |
66+
| bob | DeclaredTrait | 0 |
67+
| bob | DefinedFunction | 0 |
68+
| bob | DefinedConstant | 1 |
69+
| bob | IncludedFile | 0 |
70+
| bob | DeclaredClass | 0 |
71+
| bob | PeakMemoryUsage | 2.18 MB |
72+
+------+-------------------+--------------------+
73+
| mary | MicroTime | 1.0012910366058 |
74+
| mary | MemoryUsage | 75.48 KB |
75+
| mary | DeclaredInterface | 0 |
76+
| mary | DeclaredTrait | 0 |
77+
| mary | DefinedFunction | 0 |
78+
| mary | DefinedConstant | 0 |
79+
| mary | IncludedFile | 1 |
80+
| mary | DeclaredClass | 1 |
81+
| mary | PeakMemoryUsage | 2.18 MB |
82+
+------+-------------------+--------------------+
83+
```
84+
85+
## Specifying output interface
86+
87+
```php Benchmark::reset();
88+
Benchmark::start('susan');
89+
require_once('./SomeTrait.php');
90+
Benchmark::stop('susan');
91+
Output::output(Benchmark::get(), 'apache')->render();
92+
```
93+
94+
Returns something like:
95+
<table>
96+
<thead>
97+
<tr>
98+
<th>
99+
Name
100+
</th>
101+
<th>
102+
Metric
103+
</th>
104+
<th>
105+
Value
106+
</th>
107+
</tr>
108+
</thead>
109+
<tbody>
110+
<tr>
111+
<td>
112+
susan
113+
</td>
114+
<td>
115+
MicroTime
116+
</td>
117+
<td>
118+
0.0014090538024902
119+
</td>
120+
</tr>
121+
<tr>
122+
<td>
123+
susan
124+
</td>
125+
<td>
126+
MemoryUsage
127+
</td>
128+
<td>
129+
76.03 KB
130+
</td>
131+
</tr>
132+
<tr>
133+
<td>
134+
susan
135+
</td>
136+
<td>
137+
DeclaredInterface
138+
</td>
139+
<td>
140+
</td>
141+
</tr>
142+
<tr>
143+
<td>
144+
susan
145+
</td>
146+
<td>
147+
DeclaredTrait
148+
</td>
149+
<td>
150+
1
151+
</td>
152+
</tr>
153+
<tr>
154+
<td>
155+
susan
156+
</td>
157+
<td>
158+
DefinedFunction
159+
</td>
160+
<td>
161+
</td>
162+
</tr>
163+
<tr>
164+
<td>
165+
susan
166+
</td>
167+
<td>
168+
DefinedConstant
169+
</td>
170+
<td>
171+
</td>
172+
</tr>
173+
<tr>
174+
<td>
175+
susan
176+
</td>
177+
<td>
178+
IncludedFile
179+
</td>
180+
<td>
181+
1
182+
</td>
183+
</tr>
184+
<tr>
185+
<td>
186+
susan
187+
</td>
188+
<td>
189+
DeclaredClass
190+
</td>
191+
<td>
192+
</td>
193+
</tr>
194+
<tr>
195+
<td>
196+
susan
197+
</td>
198+
<td>
199+
PeakMemoryUsage
200+
</td>
201+
<td>
202+
2.18 MB
203+
</td>
204+
</tr>
205+
</tbody>
206+
</table>

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
}
1414
],
1515
"require": {
16-
"best-served-cold/phalue-objects": "v0.0.9-alpha",
16+
"best-served-cold/phalue-objects": "v0.0.10-alpha",
1717
"symfony/console": "^3.2",
18-
"best-served-cold/html-builder": "^1.0"
18+
"best-served-cold/html-builder": "^2.0"
1919
},
2020
"require-dev": {
2121
"phpunit/phpunit": "^5.7",

0 commit comments

Comments
 (0)