├── .gitignore
├── LICENSE
├── README.md
└── benchmark.php
/.gitignore:
--------------------------------------------------------------------------------
1 | /nbproject/private/
2 | /nbproject/
3 | /_doc/
4 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2014 odan
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy of
6 | this software and associated documentation files (the "Software"), to deal in
7 | the Software without restriction, including without limitation the rights to
8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 | the Software, and to permit persons to whom the Software is furnished to do so,
10 | subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | benchmark-php
2 | =============
3 |
4 | This is a PHP benchmark script to compare the runtime speed of PHP and MySQL. This project is inspired by www.php-benchmark-script.com (Alessandro Torrisi) an www.webdesign-informatik.de. In my point of view benchmark-php is more correct and comparable for different servers.
5 |
6 | Setup
7 |
8 | Upload benchmark.php an execute it:
9 | http://www.example.com/benchmark.php
10 |
11 |
12 | MySQL Setup (optional)
13 |
14 | - Open benchmark.php
15 | - Edit this lines
16 |
17 | ```php
18 | $arr_cfg['db.host'] = 'hostname';
19 | $arr_cfg['db.user'] = 'username';
20 | $arr_cfg['db.pw'] = 'password';
21 | $arr_cfg['db.name'] = 'database';
22 | ```
23 |
24 | - upload and run the script
25 |
--------------------------------------------------------------------------------
/benchmark.php:
--------------------------------------------------------------------------------
1 | \n
\n";
35 | echo "";
47 |
48 | echo array_to_html($arr_benchmark);
49 |
50 | echo "\n";
51 | exit;
52 |
53 | // -----------------------------------------------------------------------------
54 | // Benchmark functions
55 | // -----------------------------------------------------------------------------
56 |
57 | function test_benchmark($arr_cfg)
58 | {
59 |
60 | $time_start = microtime(true);
61 |
62 | $arr_return = array();
63 | $arr_return['version'] = '1.1';
64 | $arr_return['sysinfo']['time'] = date("Y-m-d H:i:s");
65 | $arr_return['sysinfo']['php_version'] = PHP_VERSION;
66 | $arr_return['sysinfo']['platform'] = PHP_OS;
67 | $arr_return['sysinfo']['server_name'] = $_SERVER['SERVER_NAME'];
68 | $arr_return['sysinfo']['server_addr'] = $_SERVER['SERVER_ADDR'];
69 |
70 | test_math($arr_return);
71 |
72 | test_string($arr_return);
73 |
74 | test_loops($arr_return);
75 |
76 | test_ifelse($arr_return);
77 |
78 | if (isset($arr_cfg['db.host'])) {
79 | test_mysql($arr_return, $arr_cfg);
80 | }
81 |
82 | $arr_return['total'] = timer_diff($time_start);
83 |
84 | return $arr_return;
85 | }
86 |
87 | function test_math(&$arr_return, $count = 99999)
88 | {
89 | $time_start = microtime(true);
90 |
91 | $mathFunctions = array("abs", "acos", "asin", "atan", "bindec", "floor", "exp", "sin", "tan", "pi", "is_finite", "is_nan", "sqrt");
92 | for ($i = 0; $i < $count; $i++) {
93 | foreach ($mathFunctions as $function) {
94 | $r = call_user_func_array($function, array($i));
95 | }
96 | }
97 |
98 | $arr_return['benchmark']['math'] = timer_diff($time_start);
99 | }
100 |
101 | function test_string(&$arr_return, $count = 99999)
102 | {
103 | $time_start = microtime(true);
104 | $stringFunctions = array("addslashes", "chunk_split", "metaphone", "strip_tags", "md5", "sha1", "strtoupper", "strtolower", "strrev", "strlen", "soundex", "ord");
105 |
106 | $string = 'the quick brown fox jumps over the lazy dog';
107 | for ($i = 0; $i < $count; $i++) {
108 | foreach ($stringFunctions as $function) {
109 | $r = call_user_func_array($function, array($string));
110 | }
111 | }
112 | $arr_return['benchmark']['string'] = timer_diff($time_start);
113 | }
114 |
115 | function test_loops(&$arr_return, $count = 999999)
116 | {
117 | $time_start = microtime(true);
118 | for ($i = 0; $i < $count; ++$i)
119 | ;
120 | $i = 0;
121 | while ($i < $count) {
122 | ++$i;
123 | }
124 |
125 | $arr_return['benchmark']['loops'] = timer_diff($time_start);
126 | }
127 |
128 | function test_ifelse(&$arr_return, $count = 999999)
129 | {
130 | $time_start = microtime(true);
131 | for ($i = 0; $i < $count; $i++) {
132 | if ($i == -1) {
133 |
134 | } elseif ($i == -2) {
135 |
136 | } else if ($i == -3) {
137 |
138 | }
139 | }
140 | $arr_return['benchmark']['ifelse'] = timer_diff($time_start);
141 | }
142 |
143 | function test_mysql(&$arr_return, $arr_cfg)
144 | {
145 |
146 | $time_start = microtime(true);
147 |
148 | $link = mysqli_connect($arr_cfg['db.host'], $arr_cfg['db.user'], $arr_cfg['db.pw']);
149 | $arr_return['benchmark']['mysql']['connect'] = timer_diff($time_start);
150 |
151 | // //$arr_return['sysinfo']['mysql_version'] = '';
152 |
153 | mysqli_select_db($link, $arr_cfg['db.name']);
154 | $arr_return['benchmark']['mysql']['select_db'] = timer_diff($time_start);
155 |
156 | $result = mysqli_query($link, 'SELECT VERSION() as version;');
157 | $arr_row = mysqli_fetch_assoc($result);
158 | $arr_return['sysinfo']['mysql_version'] = $arr_row['version'];
159 | $arr_return['benchmark']['mysql']['query_version'] = timer_diff($time_start);
160 |
161 | $query = "SELECT BENCHMARK(1000000,ENCODE('hello',RAND());";
162 | $result = mysqli_query($link, $query);
163 | $arr_return['benchmark']['mysql']['query_benchmark'] = timer_diff($time_start);
164 |
165 | mysqli_close($link);
166 |
167 | $arr_return['benchmark']['mysql']['total'] = timer_diff($time_start);
168 |
169 | return $arr_return;
170 | }
171 |
172 | function timer_diff($time_start)
173 | {
174 | return number_format(microtime(true) - $time_start, 3);
175 | }
176 |
177 | function array_to_html($my_array)
178 | {
179 | $strReturn = '';
180 | if (is_array($my_array)) {
181 | $strReturn .= '';
182 | foreach ($my_array as $k => $v) {
183 | $strReturn .= "\n";
184 | $strReturn .= '' . htmlentities($k) . " | ";
185 | $strReturn .= array_to_html($v);
186 | $strReturn .= " |
";
187 | }
188 | $strReturn .= "\n
";
189 | } else {
190 | $strReturn = htmlentities($my_array);
191 | }
192 | return $strReturn;
193 | }
194 |
--------------------------------------------------------------------------------