-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathmysql-benchmark
executable file
·63 lines (49 loc) · 1.75 KB
/
mysql-benchmark
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
#!/usr/bin/php
<?php
$db_params = array(
'host' => 'localhost',
'username' => 'root',
'password' => 'admin',
);
$link = mysql_connect(
$db_params['host'],
$db_params['username'],
$db_params['password']);
if (!$link) {
die("Can't connect to mysql");
}
mysql_query('CREATE DATABASE benchmark');
mysql_select_db('benchmark', $link);
mysql_query("CREATE TABLE `benchmark`.`benchtable` "
. "(`id` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,"
. " `token` VARCHAR( 16 ) NULL DEFAULT NULL "
. ") ENGINE = INNODB");
$n_rows = 1000;
echo "Inserting $n_rows rows ...\n";
$start_time = microtime(true);
for($i=1; $i < $n_rows + 1 ; $i++) {
$token = base_convert(sha1($i),7, 36);
mysql_query("INSERT INTO benchtable (id, token) VALUES (".$i.", '".$token."')");
}
$end_time = microtime(true);
$insert_time = $end_time - $start_time;
echo "Inserted $n_rows rows in " . $insert_time . " seconds \n";
echo "Approx. " . $n_rows / $insert_time . " inserts per second\n\n";
echo "Selecting $n_rows rows ...\n";
$start_time = microtime(true);
$result = mysql_query("select * FROM benchtable");
$end_time = microtime(true);
$select_time = $end_time - $start_time;
echo "Selected $n_rows rows in " . $select_time . " seconds \n";
echo "Approx. " . $n_rows / $select_time . " selects per second\n\n";
echo "Updating $n_rows rows ...\n";
$start_time = microtime(true);
while($row = mysql_fetch_assoc($result)) {
$token = md5($row['token']);
mysql_query("UPDATE benchtable SET token='$token' WHERE id=" . $row['id']);
}
$end_time = microtime(true);
$update_time = $end_time - $start_time;
echo "Updated $n_rows rows in " . $update_time . " seconds \n";
echo "Approx. " . $n_rows / $update_time . " updates per second\n";
mysql_query('DROP DATABASE benchmark');