Skip to content

Commit a9507ff

Browse files
authored
Merge pull request #6 from sergix44/v2
version 2 and bugfixes
2 parents 2ed951a + ec71fbf commit a9507ff

File tree

4 files changed

+415
-68
lines changed

4 files changed

+415
-68
lines changed

README.md

Lines changed: 109 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,116 @@ curl https://raw.githubusercontent.com/SergiX44/php-benchmark-script/master/benc
1414

1515
Or upload the file `bench.php` to your web document root and visit it.
1616

17-
# Example Output
17+
#### Parameters
18+
19+
You can change the difficulty multiplier by passing a `multiplier` parameter:
20+
21+
```sh
22+
php bench.php --multiplier=2
1823
```
19-
--------------------------------------------------
20-
| PHP BENCHMARK SCRIPT v.1.0 by @SergiX44 |
21-
--------------------------------------------------
22-
PHP version................................. 8.2.1
23-
Platform.................................... Linux
24-
Server:.................................. hostname
25-
OPCache status:........................... enabled
26-
OPCache JIT:.............................. enabled
27-
PCRE JIT:................................. enabled
28-
Started at:............... 17/01/2023 15:22:51.079
29-
--------------------------------------------------
30-
math..................................... 0.1551 s
31-
loops.................................... 0.0223 s
32-
ifelse................................... 0.0305 s
33-
switch................................... 0.0283 s
34-
strings.................................. 0.4240 s
35-
array.................................... 0.7684 s
36-
regex.................................... 0.2929 s
37-
is_{type}................................ 0.0611 s
38-
--------------------------------------------------
39-
Total time............................... 1.7828 s
40-
Peak memory usage........................... 2 MiB
24+
25+
### Additional tests
26+
27+
You must have the `bench.php` file to your server.
28+
You can download additional benchmarks (must be in the same directory as `bench.php`) using:
29+
30+
```sh
31+
# rand: random number generation
32+
wget https://raw.githubusercontent.com/SergiX44/php-benchmark-script/master/rand.bench.php
33+
```
34+
35+
Then you can run the benchmark using:
36+
37+
```sh
38+
php bench.php
39+
```
40+
41+
If the file is in the same directory as `bench.php`, it will be automatically loaded.
42+
43+
## Custom tests
44+
45+
You can create your own tests by creating a file in the same directory as `bench.php` and the file must be
46+
named `*.bench.php`.
47+
48+
The file must return a closure, or an array of closures. Each closure should take a parameter `$multiplier` which is
49+
how hard the test should be. The higher the `$multiplier`, the longer the test will take, by default it is `1`.
50+
You should choose a reasonable number of iterations for your test (e.g. 1000) and multiply it by the `$multiplier`.
51+
52+
Example:
53+
54+
```php
55+
// mytest.bench.php
56+
57+
return function ($multiplier = 1) {
58+
$iterations = 1000 * $multiplier;
59+
for ($i = 0; $i < $iterations; ++$i) {
60+
// do something
61+
}
62+
};
63+
```
64+
65+
Or with multiple tests:
66+
67+
```php
68+
// mytest.bench.php
69+
70+
return [
71+
'my_test' => function ($multiplier = 1) {
72+
$iterations = 1000 * $multiplier;
73+
for ($i = 0; $i < $iterations; ++$i) {
74+
// do something
75+
}
76+
},
77+
'another_test' => function ($multiplier = 1) {
78+
$iterations = 1000 * $multiplier;
79+
for ($i = 0; $i < $iterations; ++$i) {
80+
// do something else
81+
}
82+
},
83+
];
84+
```
85+
86+
# Example Output
87+
88+
```sh
89+
-------------------------------------------------------
90+
| PHP BENCHMARK SCRIPT v.2.0 by @SergiX44 |
91+
-------------------------------------------------------
92+
PHP............................................. 8.2.10
93+
Platform........................................ Darwin
94+
Arch............................................. arm64
95+
Server........................................ hostname
96+
Max memory usage.................................. 512M
97+
OPCache status................................. enabled
98+
OPCache JIT.................................... enabled
99+
PCRE JIT....................................... enabled
100+
XDebug extension.............................. disabled
101+
Difficulty multiplier............................... 1x
102+
Started at..................... 06/12/2023 13:45:37.453
103+
-------------------------------------------------------
104+
math.......................................... 0.0935 s
105+
loops......................................... 0.0121 s
106+
ifelse........................................ 0.0173 s
107+
switch........................................ 0.0172 s
108+
string........................................ 0.1842 s
109+
array......................................... 0.3212 s
110+
regex......................................... 0.1769 s
111+
is_{type}..................................... 0.0322 s
112+
hash.......................................... 0.1202 s
113+
json.......................................... 0.1586 s
114+
-----------------Additional Benchmarks-----------------
115+
io::file_read................................. 0.0129 s
116+
io::file_write................................ 0.0715 s
117+
io::file_zip.................................. 0.5335 s
118+
io::file_unzip................................ 0.1571 s
119+
rand::rand.................................... 0.0089 s
120+
rand::mt_rand................................. 0.0089 s
121+
rand::random_int.............................. 0.0679 s
122+
rand::random_bytes............................ 0.2320 s
123+
rand::openssl_random_pseudo_bytes............. 0.2953 s
124+
-------------------------------------------------------
125+
Total time.................................... 2.5214 s
126+
Peak memory usage................................ 2 MiB
41127
```
42128

43129
## Authors

0 commit comments

Comments
 (0)