public function query($sql) { if ($this->log !== false) { $backtrace = debug_backtrace(); if (isset($backtrace[1])) { $this->log->write($backtrace[1]['class'] . $backtrace[1]['type'] . $backtrace[1]['function'] . ': Sending SQL: ' . $sql, XBM_LOG_DEBUG); } else { $this->log->write('Sending SQL: ' . $sql, XBM_LOG_DEBUG); } $timer = new Timer(); } $res = parent::query($sql); if ($this->log !== false) { $elapsed = $timer->elapsed(); if (isset($backtrace[1])) { $this->log->write($backtrace[1]['class'] . $backtrace[1]['type'] . $backtrace[1]['function'] . ': Query took ' . $elapsed, XBM_LOG_DEBUG); } else { $this->log->write('Query took ' . $elapsed, XBM_LOG_DEBUG); } } return $res; }
public static function createTables(\PDO $pdo_db, string $db_prefix, string $dbtype) { $create_table_results = array(); $i = 0; define('PDO_SUCCESS', (string) '00000'); // PDO gives an error code of string 00000 if successful. Not extremely helpful. $schema_files = new \DirectoryIterator('schema/' . $dbtype); foreach ($schema_files as $schema_filename) { $table_timer = new Timer(); $table_timer->start(); // Start benchmarking if ($schema_filename->isFile() && $schema_filename->getExtension() == 'sql') { // Since we are using strict types, the Directory Iterator returns an object, and we want a string to pass to mb_substr. $simple_filename = (string) $schema_filename->getFilename(); // Routine to handle persistent database tables. If a SQL schema file starts with persist-, then it is a persistent table $persist_file = mb_substr($simple_filename, 0, 8) === 'persist-'; if ($persist_file) { $tablename = mb_substr($simple_filename, 8, -4); } else { $tablename = mb_substr($simple_filename, 0, -4); } // Slurp the SQL call from schema, and turn it into an SQL string $sql_query = file_get_contents('schema/' . $dbtype . '/' . $schema_filename); // Replace the default prefix (tki_) with the chosen table prefix from the game $sql_query = preg_replace('/tki_/', $db_prefix, $sql_query); // Remove comments from SQL $RXSQLComments = '@(--[^\\r\\n]*)|(\\#[^\\r\\n]*)|(/\\*[\\w\\W]*?(?=\\*/)\\*/)@ms'; $sql_query = $sql_query == '' ? '' : preg_replace($RXSQLComments, '', $sql_query); // FUTURE: Test handling invalid SQL to ensure it hits the error logger below AND the visible output during running $sth = $pdo_db->prepare($sql_query); $sth->execute(); if ($pdo_db->errorCode() !== PDO_SUCCESS) { $errorinfo = $pdo_db->errorInfo(); $create_table_results[$i]['result'] = $errorinfo[1] . ': ' . $errorinfo[2]; } else { $create_table_results[$i]['result'] = true; } // Db::logDbErrors($pdo_db, $execute_res, __LINE__, __FILE__); // Triggers errors because there is no DB $create_table_results[$i]['name'] = $db_prefix . $tablename; $table_timer->stop(); $create_table_results[$i]['time'] = $table_timer->elapsed(); $i++; } } return $create_table_results; }
<?php require_once "Timer.php"; $timer = new Timer(); ?> <HTML> <HEAD> <TITLE>Test Page (elapsed time: <?php echo $timer->elapsed(); ?> seconds)</TITLE> </HEAD> <BODY> This is a simple test page. Elapsed time: <?php echo $timer->elapsed(); ?> seconds. </BODY> </HTML> <!-- Final elapsed time: <?php echo $timer->elapsed(); ?> seconds. -->
function test($obj, $func) { global $count; $timer = new Timer(); $timer->start(); for ($i = 0; $i < $count; $i++) { $obj->{$func}(rand(0, $count * 10)); } return round($timer->elapsed(), 6); }