| 1 |
<?php
|
| 2 |
// $Id: tester.php,v 1.3 2006/06/01 17:20:17 d_sna Exp $
|
| 3 |
|
| 4 |
/** Data storage test (depends on Drupal core files)
|
| 5 |
* It compares the SQL, gmdb and the raw serialize speed and memory requirement
|
| 6 |
* with huge record number. The record number will be MAX/STEP
|
| 7 |
* Aron Novak / aaron at szentimre dot hu /
|
| 8 |
*/
|
| 9 |
|
| 10 |
require 'includes/bootstrap.inc';
|
| 11 |
|
| 12 |
/* Settings */
|
| 13 |
define('MAX', 5000);
|
| 14 |
define('STEP', 1); // The number of records is MAX/STEP
|
| 15 |
define('TABLENAME', 'tester'); // This table will be deleted by this script!
|
| 16 |
/************/
|
| 17 |
|
| 18 |
/**
|
| 19 |
* gdbm test module.
|
| 20 |
* Need to write ./log_table.db
|
| 21 |
*/
|
| 22 |
|
| 23 |
function gdbm_create() {
|
| 24 |
$at_start = res_start();
|
| 25 |
if (!$db = dba_open("./log_table.db", "n", "gdbm")) {
|
| 26 |
die ("Cannot open database\n");
|
| 27 |
}
|
| 28 |
for ($i = 0; $i <= MAX; $i += STEP) {
|
| 29 |
dba_insert(round($i, 2), log($i), $db);
|
| 30 |
}
|
| 31 |
dba_close($db);
|
| 32 |
return res_stop($at_start);
|
| 33 |
}
|
| 34 |
|
| 35 |
function gdbm_list() {
|
| 36 |
$at_start = res_start();
|
| 37 |
if (!$db = dba_open("./log_table.db", "rd", "gdbm")) {
|
| 38 |
die ("Cannot open database\n");
|
| 39 |
}
|
| 40 |
ob_start();
|
| 41 |
for ($i = 0; $i <= MAX; $i += STEP) {
|
| 42 |
$i = round($i, 2);
|
| 43 |
print "log(" . $i . ") = " . dba_fetch($i, $db) . "\n";
|
| 44 |
}
|
| 45 |
ob_end_clean();
|
| 46 |
dba_close($db);
|
| 47 |
return res_stop($at_start);
|
| 48 |
}
|
| 49 |
|
| 50 |
function gdbm_search() {
|
| 51 |
$at_start = res_start();
|
| 52 |
$search_values = get_search_array();
|
| 53 |
if (!$db = dba_open("./log_table.db", "rd", "gdbm")) {
|
| 54 |
die ("Cannot open database\n");
|
| 55 |
}
|
| 56 |
ob_start();
|
| 57 |
$num_searches = count($search_values);
|
| 58 |
for ($i = 0; $i < $num_searches; $i++) {
|
| 59 |
print $search_values[$i] . "=>" . dba_fetch($search_values[$i], $db) . "\n";
|
| 60 |
}
|
| 61 |
ob_end_clean();
|
| 62 |
return res_stop($at_start);
|
| 63 |
}
|
| 64 |
|
| 65 |
/**
|
| 66 |
* SQL test module. Have to be a correct settings.php with database info.
|
| 67 |
*/
|
| 68 |
|
| 69 |
function sql_create() {
|
| 70 |
$at_start = res_start();
|
| 71 |
for ($i = 0; $i <= MAX; $i += STEP) {
|
| 72 |
db_query("INSERT INTO " . TABLENAME . " VALUES ('" . round($i, 2) . "','" . log($i) . "')");
|
| 73 |
// When I tried to create one huge query with $query .= it going to out of memory
|
| 74 |
}
|
| 75 |
return res_stop($at_start);
|
| 76 |
}
|
| 77 |
|
| 78 |
function sql_list() {
|
| 79 |
$at_start = res_start();
|
| 80 |
if (!$result = db_query("SELECT * FROM " . TABLENAME)) {
|
| 81 |
die ("Database problem\n");
|
| 82 |
}
|
| 83 |
ob_start();
|
| 84 |
while ($line = db_fetch_array($result)) {
|
| 85 |
print $line["number"] . "=>" . $line["val"] . "\n";
|
| 86 |
}
|
| 87 |
ob_end_clean();
|
| 88 |
return res_stop($at_start);
|
| 89 |
}
|
| 90 |
|
| 91 |
function sql_search() {
|
| 92 |
$at_start = res_start();
|
| 93 |
$search_values = get_search_array();
|
| 94 |
$query = "SELECT * FROM " . TABLENAME . " WHERE";
|
| 95 |
$num_searches = count($search_values);
|
| 96 |
for ($i = 0; $i < $num_searches; $i++) {
|
| 97 |
$query .= " number='" . $search_values[$i] . "' " . ($i == 2 ? "" : " OR ");
|
| 98 |
}
|
| 99 |
if (!$result = db_query($query)) {
|
| 100 |
die ("Database problem\n");
|
| 101 |
}
|
| 102 |
ob_start();
|
| 103 |
while ($line = db_fetch_array($result)) {
|
| 104 |
print $line["number"] . "=>" . $line["val"]. "\n";
|
| 105 |
}
|
| 106 |
ob_end_clean();
|
| 107 |
return res_stop($at_start);
|
| 108 |
}
|
| 109 |
|
| 110 |
/**
|
| 111 |
* Serialize test module. Need to write ./ser.dat.
|
| 112 |
*/
|
| 113 |
|
| 114 |
function ser_create() {
|
| 115 |
$at_start = res_start();
|
| 116 |
for($i = 0; $i <= MAX; $i += STEP) {
|
| 117 |
$data[round($i, 2)] = log($i);
|
| 118 |
}
|
| 119 |
if (!$file_s = fopen("./ser.dat", "w")) {
|
| 120 |
die ("Cannot write to the current directory!\n");
|
| 121 |
}
|
| 122 |
fwrite($file_s, serialize($data));
|
| 123 |
fclose($file_s);
|
| 124 |
return res_stop($at_start);
|
| 125 |
}
|
| 126 |
|
| 127 |
function ser_list() {
|
| 128 |
$at_start = res_start();
|
| 129 |
if (!$data = unserialize(file_get_contents("./ser.dat"))) {
|
| 130 |
die ("Cannot read from data file (./ser.dat)!\n");
|
| 131 |
}
|
| 132 |
ob_start();
|
| 133 |
for($i = 0; $i <= MAX; $i += STEP) {
|
| 134 |
$i=round($i, 2);
|
| 135 |
print $i . "=>" . $data[$i] . "\n";
|
| 136 |
}
|
| 137 |
ob_end_clean();
|
| 138 |
return res_stop($at_start);
|
| 139 |
}
|
| 140 |
|
| 141 |
function ser_search() {
|
| 142 |
$at_start = res_start();
|
| 143 |
$search_values = get_search_array();
|
| 144 |
if (!$data = unserialize(file_get_contents("./ser.dat"))) {
|
| 145 |
die ("Cannot read from data file (./ser.dat)!\n");
|
| 146 |
}
|
| 147 |
ob_start();
|
| 148 |
$num_searches = count($search_values);
|
| 149 |
for($i = 0; $i < $num_searches; $i++)
|
| 150 |
{
|
| 151 |
print $search_values[$i] . "=>" . $data[$search_values[$i]] . "\n";
|
| 152 |
}
|
| 153 |
ob_end_clean();
|
| 154 |
return res_stop($at_start);
|
| 155 |
}
|
| 156 |
|
| 157 |
/**
|
| 158 |
* Include test module. Need to write ./data.inc.
|
| 159 |
*/
|
| 160 |
|
| 161 |
function inc_create() {
|
| 162 |
$at_start = res_start();
|
| 163 |
for($i = 0; $i <= MAX; $i += STEP) {
|
| 164 |
$data[round($i, 2)] = log($i);
|
| 165 |
}
|
| 166 |
if (!$file_s = fopen("./data.inc", "w")) {
|
| 167 |
die ("Cannot write to the current directory!\n");
|
| 168 |
}
|
| 169 |
fwrite($file_s, '<?php $data = ' . var_export($data, true) . ';?>');
|
| 170 |
fclose($file_s);
|
| 171 |
return res_stop($at_start);
|
| 172 |
}
|
| 173 |
|
| 174 |
function inc_list() {
|
| 175 |
$at_start = res_start();
|
| 176 |
require './data.inc';
|
| 177 |
ob_start();
|
| 178 |
for($i = 0; $i <= MAX; $i += STEP) {
|
| 179 |
$i=round($i, 2);
|
| 180 |
print $i . "=>" . $data[$i] . "\n";
|
| 181 |
}
|
| 182 |
ob_end_clean();
|
| 183 |
return res_stop($at_start);
|
| 184 |
}
|
| 185 |
|
| 186 |
function inc_search() {
|
| 187 |
$at_start = res_start();
|
| 188 |
$search_values = get_search_array();
|
| 189 |
require './data.inc';
|
| 190 |
ob_start();
|
| 191 |
$num_searches = count($search_values);
|
| 192 |
for($i = 0; $i < $num_searches; $i++)
|
| 193 |
{
|
| 194 |
print $search_values[$i] . "=>" . $data[$search_values[$i]] . "\n";
|
| 195 |
}
|
| 196 |
ob_end_clean();
|
| 197 |
return res_stop($at_start);
|
| 198 |
}
|
| 199 |
|
| 200 |
function sql_init() { // We need to do an empty log_vals table with proper columns
|
| 201 |
db_query("DROP TABLE " . TABLENAME);
|
| 202 |
if (!$res = db_query("CREATE TABLE " . TABLENAME . " ( number double NOT NULL default '0',
|
| 203 |
val double default NULL, KEY num_idx (number) ) TYPE=MyISAM")) {
|
| 204 |
die ("Database problem\n");
|
| 205 |
}
|
| 206 |
}
|
| 207 |
|
| 208 |
function get_search_array()
|
| 209 |
{
|
| 210 |
return array (round(MAX*0.1, 1),
|
| 211 |
round(MAX*0.5, 1),
|
| 212 |
round(MAX*0.9, 1),
|
| 213 |
);
|
| 214 |
}
|
| 215 |
|
| 216 |
function res_start() {
|
| 217 |
static $timer_index;
|
| 218 |
$mem_start = memory_get_usage();
|
| 219 |
timer_start($timer_index);
|
| 220 |
return array(&$timer_index, $mem_start);
|
| 221 |
}
|
| 222 |
|
| 223 |
function res_stop($at_start) {
|
| 224 |
$res["time"] = timer_read($at_start[0]) / 1000 . " sec";
|
| 225 |
$res["mem"] = (memory_get_usage() - $at_start[1]) / 1024 . " kB";
|
| 226 |
timer_stop($at_start[0]++);
|
| 227 |
return $res;
|
| 228 |
}
|
| 229 |
|
| 230 |
if (strlen(TABLENAME) < 1) {
|
| 231 |
die ("You must edit the settings to define a proper table name for SQL testing\n");
|
| 232 |
}
|
| 233 |
|
| 234 |
drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE);
|
| 235 |
sql_init();
|
| 236 |
|
| 237 |
/*
|
| 238 |
I have these functions:
|
| 239 |
ser_create, ser_list, ser_search
|
| 240 |
sql_create, sql_list, sql_search
|
| 241 |
gdbm_create, gdbm_list, gdbm_search
|
| 242 |
The function results should be put into an assoc. array
|
| 243 |
*/
|
| 244 |
|
| 245 |
$backends = array("sql", "gdbm", "ser", "inc", );
|
| 246 |
$functions = array ("create", "list", "search", );
|
| 247 |
$n_f = count($functions);
|
| 248 |
$n_b = count($backends);
|
| 249 |
|
| 250 |
for ($i=0; $i < $n_f; $i++) {
|
| 251 |
for ($j=0; $j < $n_b; $j++) {
|
| 252 |
$function = $backends[$j] . '_' . $functions[$i];
|
| 253 |
$result[$backends[$j] . " " . $functions[$i]] = $function();
|
| 254 |
}
|
| 255 |
}
|
| 256 |
|
| 257 |
if (isset($argc) && isset($argv)) { // We're in CLI
|
| 258 |
print "The amount of records is " . MAX / STEP . "\n";
|
| 259 |
print_r($result);
|
| 260 |
} else { // Web mode
|
| 261 |
?><table><?
|
| 262 |
for ($i=0; $i < $n_f; $i++) {
|
| 263 |
for ($j=0; $j < $n_b; $j++) {
|
| 264 |
?><tr><td><?
|
| 265 |
print $backends[$j] . " " . $functions[$i];
|
| 266 |
?><td><?
|
| 267 |
print $result[$backends[$j] . " " . $functions[$i]]["time"];
|
| 268 |
?><td><?
|
| 269 |
print $result[$backends[$j] . " " . $functions[$i]]["mem"];
|
| 270 |
}
|
| 271 |
?></tr><?
|
| 272 |
}
|
| 273 |
?></table><?
|
| 274 |
}
|