1 | <?php |
---|
2 | |
---|
3 | $link = new mysqli( 'localhost', 'root', '', 'test' ); |
---|
4 | |
---|
5 | $engines = array( |
---|
6 | 'MyISAM', |
---|
7 | 'InnoDB', |
---|
8 | ); |
---|
9 | |
---|
10 | $table_sizes = array( 300, 3000, 30000, 300000 ); |
---|
11 | |
---|
12 | $percentages = array( 80, 50, 20, 5 ); |
---|
13 | |
---|
14 | $schemas = array( |
---|
15 | 'VARCHAR20' => array( |
---|
16 | 'def' => "option_id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT, option_name VARCHAR(191) DEFAULT NULL, option_value LONGTEXT NOT NULL, autoload VARCHAR(20) NOT NULL DEFAULT 'yes', PRIMARY KEY (option_id), UNIQUE KEY option_name (option_name)", |
---|
17 | 'values' => array("'no'", "'yes'"), |
---|
18 | ), |
---|
19 | 'VARCHAR20_idx' => array( |
---|
20 | 'def' => "option_id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT, option_name VARCHAR(191) DEFAULT NULL, option_value LONGTEXT NOT NULL, autoload VARCHAR(20) NOT NULL DEFAULT 'yes', PRIMARY KEY (option_id), UNIQUE KEY option_name (option_name), INDEX autoload_index (autoload)", |
---|
21 | 'values' => array("'no'", "'yes'"), |
---|
22 | ), |
---|
23 | 'ENUM_idx' => array( |
---|
24 | 'def' => "option_id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT, option_name VARCHAR(191) DEFAULT NULL, option_value LONGTEXT NOT NULL, autoload ENUM('yes', 'no') NOT NULL DEFAULT 'yes', PRIMARY KEY (option_id), UNIQUE KEY option_name (option_name), INDEX autoload_index (autoload)", |
---|
25 | 'values' => array("'no'", "'yes'"), |
---|
26 | ), |
---|
27 | 'CHAR1_idx' => array( |
---|
28 | 'def' => "option_id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT, option_name VARCHAR(191) DEFAULT NULL, option_value LONGTEXT NOT NULL, autoload CHAR(1) NOT NULL DEFAULT 'y', PRIMARY KEY (option_id), UNIQUE KEY option_name (option_name), INDEX autoload_index (autoload)", |
---|
29 | 'values' => array("'n'", "'y'"), |
---|
30 | ), |
---|
31 | 'TINYINT_idx' => array( |
---|
32 | 'def' => "option_id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT, option_name VARCHAR(191) DEFAULT NULL, option_value LONGTEXT NOT NULL, autoload TINYINT(1) NOT NULL DEFAULT 0, PRIMARY KEY (option_id), UNIQUE KEY option_name (option_name), INDEX autoload_index (autoload)", |
---|
33 | 'values' => array("0", "1"), |
---|
34 | ), |
---|
35 | 'VARCHAR20_idx2' => array( |
---|
36 | 'def' => "option_id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT, option_name VARCHAR(191) DEFAULT NULL, option_value LONGTEXT NOT NULL, autoload VARCHAR(20) NOT NULL DEFAULT 'yes', PRIMARY KEY (option_id), UNIQUE KEY option_name (option_name), INDEX autoload_index (autoload, option_name)", |
---|
37 | 'values' => array("'no'", "'yes'"), |
---|
38 | ), |
---|
39 | ); |
---|
40 | |
---|
41 | $b = str_repeat( 'x', 1000 ); |
---|
42 | |
---|
43 | $run_prep_data = FALSE; |
---|
44 | $run_tests = TRUE; |
---|
45 | |
---|
46 | foreach ($engines as $key => $engine) { |
---|
47 | foreach ($table_sizes as $key => $table_size) { |
---|
48 | foreach ($percentages as $key => $percentage) { |
---|
49 | $last_avg = FALSE; |
---|
50 | echo "{$engine} - Size {$table_size} - Percent {$percentage}\n"; |
---|
51 | foreach ($schemas as $schema_name => $schema) { |
---|
52 | $schema_val = $schema['values']; |
---|
53 | $table_name = "{$engine}_{$schema_name}_Size_{$table_size}_Percent_{$percentage}"; |
---|
54 | |
---|
55 | if ($run_prep_data == TRUE) { |
---|
56 | // Creates |
---|
57 | $setup_sql = "DROP TABLE IF EXISTS {$table_name}; "; |
---|
58 | $link->query($setup_sql) or die(__LINE__ . ': ' . $link->error . PHP_EOL); |
---|
59 | $setup_sql = "CREATE TABLE ${table_name} ({$schema['def']}) ENGINE={$engine} DEFAULT CHARSET=utf8mb4;"; |
---|
60 | $link->query($setup_sql) or die(__LINE__ . ': ' . $link->error . PHP_EOL); |
---|
61 | |
---|
62 | // Fills |
---|
63 | $sql = "INSERT INTO {$table_name} (option_name, option_value, autoload) VALUES "; |
---|
64 | $batch = 0; |
---|
65 | $rows = array(); |
---|
66 | for ($row = 0; $row < $table_size; $row++) { |
---|
67 | if ($batch == 100) { |
---|
68 | $link->query($sql . implode(', ', $rows)) or die(__LINE__ . ': ' . $link->error . PHP_EOL); |
---|
69 | $batch = 0; |
---|
70 | $rows = array(); |
---|
71 | } |
---|
72 | $a = "option_number_{$row}"; |
---|
73 | $c = ( ( $row % 100 ) < $percentage ) ? $schema_val[1] : $schema_val[0]; |
---|
74 | $rows[] = "('{$a}', '{$b}', $c)"; |
---|
75 | $batch++; |
---|
76 | } |
---|
77 | $link->query($sql . implode(', ', $rows)) or die(__LINE__ . ': ' . $link->error . PHP_EOL); |
---|
78 | $rows = array(); |
---|
79 | } |
---|
80 | |
---|
81 | if ($run_tests == TRUE) { |
---|
82 | $i = 1; // We only really are concerned with checking c=yes as in the application we only have that query. |
---|
83 | $select_val = $schema_val[$i]; |
---|
84 | $time_taken = array(); |
---|
85 | for ($t = 0; $t < 10; $t++) { |
---|
86 | $sql = "SELECT option_name, option_value FROM {$table_name} WHERE autoload = {$select_val};"; |
---|
87 | |
---|
88 | $start = microtime( true ); |
---|
89 | $res = $link->query( $sql ); |
---|
90 | $end = microtime( true ); |
---|
91 | |
---|
92 | if (!$res) { |
---|
93 | die(__LINE__ . ': ' . $link->error . PHP_EOL); |
---|
94 | } |
---|
95 | |
---|
96 | $res->free(); |
---|
97 | if ($t > 0) { |
---|
98 | $time_taken[] = ($end - $start); |
---|
99 | } |
---|
100 | } |
---|
101 | |
---|
102 | // Drop the largest and smallest |
---|
103 | sort($time_taken); |
---|
104 | array_shift($time_taken); |
---|
105 | array_pop($time_taken); |
---|
106 | |
---|
107 | $avg_time = array_sum($time_taken) / count($time_taken); |
---|
108 | |
---|
109 | // Set the baseline |
---|
110 | if ($last_avg === FALSE) { |
---|
111 | $last_avg = $avg_time; |
---|
112 | } |
---|
113 | // If the difference is significant (more than 10 ms), print it |
---|
114 | elseif (abs($last_avg - $avg_time) > 0.010) { |
---|
115 | echo "{$schema_name} {$select_val} " , number_format($avg_time, 5); |
---|
116 | |
---|
117 | if ($last_avg < $avg_time) { |
---|
118 | echo " << Slower "; |
---|
119 | } |
---|
120 | else { |
---|
121 | echo " >> Faster "; |
---|
122 | } |
---|
123 | echo number_format(100 * $last_avg / $avg_time, 0) , "%\n"; |
---|
124 | } |
---|
125 | } |
---|
126 | } |
---|
127 | echo "\n"; |
---|
128 | } |
---|
129 | } |
---|
130 | } |
---|