/
benchmark.php
executable file
·129 lines (90 loc) · 2.56 KB
/
benchmark.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env php
<?php
/**
* Copyright (C) 2012, Oleg Efimov and other contributors
*
* See license text in LICENSE file
*/
$conn = null;
$cfg = "";
$f = fopen('php://stdin', 'r');
while ($line = fgets($f))
$cfg .= $line;
fclose($f);
$cfg = json_decode($cfg, true);
//
// Benchmarking functions
//
function do_benchmark_selects()
{
global $conn, $cfg;
usleep(intval($cfg['delay_before_select'])*1000); // micro seconds
$start = microtime(true);
$rows = array();
$r = mysql_query($cfg['select_query'], $conn);
while ($row = mysql_fetch_array($r)) {
$rows[] = $row;
}
$finish = microtime(true);
return round(count($rows)/($finish - $start), 0);
}
function do_benchmark_inserts()
{
global $conn, $cfg;
$start = microtime(true);
for ($i = 0; $i < $cfg['insert_rows_count']; $i++) {
mysql_query($cfg['insert_query'], $conn);
}
$finish = microtime(true);
return round($cfg['insert_rows_count']/($finish - $start), 0);
}
function do_benchmark_reconnects()
{
global $conn, $cfg;
$start = microtime(true);
for ($i = 0; $i < $cfg['reconnect_count']; $i++) {
mysql_close($conn);
$conn = mysql_connect("{$cfg['host']}:{$cfg['port']}", $cfg['user'], $cfg['password']);
mysql_select_db($cfg['database'], $conn);
}
$finish = microtime(true);
return round($cfg['reconnect_count']/($finish - $start), 0);
}
function do_benchmark_escapes()
{
global $conn, $cfg;
$escaped_string = "";
$start = microtime(true);
for ($i = 0; $i < $cfg['escapes_count']; $i++) {
$escaped_string = mysql_real_escape_string($cfg['string_to_escape'], $conn);
}
$finish = microtime(true);
return round($cfg['escapes_count']/($finish - $start), 0);
}
function do_benchmark_init()
{
global $conn, $cfg;
$start = microtime(true);
$conn = mysql_connect("{$cfg['host']}:{$cfg['port']}", $cfg['user'], $cfg['password']);
mysql_select_db($cfg['database'], $conn);
mysql_query("DROP TABLE IF EXISTS " . $cfg['test_table']);
mysql_query($cfg['create_table_query']);
$finish = microtime(true);
return round($finish - $start, 3);
}
//
// Do benchmarks
//
$benchmarks = array(
"init",
"escapes",
"reconnects",
"inserts",
"selects",
);
$results = array();
foreach ($benchmarks as $benchmark) {
$results[$benchmark] = call_user_func("do_benchmark_" . $benchmark);
}
mysql_close($conn);
echo json_encode($results);