Browse Source

More $config['debug'] improvements to time-related stuff

pull/40/head
Michael Foster 11 years ago
parent
commit
343803afb9
  1. 2
      inc/cache.php
  2. 21
      inc/database.php
  3. 32
      inc/functions.php
  4. 6
      inc/template.php
  5. 6
      mod.php

2
inc/cache.php

@ -66,7 +66,7 @@ class Cache {
return $data; return $data;
} }
public static function set($key, $value, $expires = false) { public static function set($key, $value, $expires = false) {
global $config; global $config, $debug;
$key = $config['cache']['prefix'] . $key; $key = $config['cache']['prefix'] . $key;

21
inc/database.php

@ -28,13 +28,13 @@ class PreparedQueryDebug {
$return = call_user_func_array(array($this->query, $function), $args); $return = call_user_func_array(array($this->query, $function), $args);
if ($config['debug'] && $function == 'execute') { if ($config['debug'] && $function == 'execute') {
$time = round((microtime(true) - $start) * 1000, 2) . 'ms'; $time = microtime(true) - $start;
$debug['sql'][] = array( $debug['sql'][] = array(
'query' => $this->query->queryString, 'query' => $this->query->queryString,
'rows' => $this->query->rowCount(), 'rows' => $this->query->rowCount(),
'time' => '~' . $time 'time' => '~' . round($time * 1000, 2) . 'ms'
); );
$debug['time']['db_queries'] += $time;
} }
return $return; return $return;
@ -42,10 +42,14 @@ class PreparedQueryDebug {
} }
function sql_open() { function sql_open() {
global $pdo, $config; global $pdo, $config, $debug;
if ($pdo) if ($pdo)
return true; return true;
if ($config['debug'])
$start = microtime(true);
if (isset($config['db']['server'][0]) && $config['db']['server'][0] == ':') if (isset($config['db']['server'][0]) && $config['db']['server'][0] == ':')
$unix_socket = substr($config['db']['server'], 1); $unix_socket = substr($config['db']['server'], 1);
else else
@ -64,6 +68,10 @@ function sql_open() {
if ($config['db']['persistent']) if ($config['db']['persistent'])
$options[PDO::ATTR_PERSISTENT] = true; $options[PDO::ATTR_PERSISTENT] = true;
$pdo = new PDO($dsn, $config['db']['user'], $config['db']['password'], $options); $pdo = new PDO($dsn, $config['db']['user'], $config['db']['password'], $options);
if ($config['debug'])
$debug['time']['db_connect'] = '~' . round((microtime(true) - $start) * 1000, 2) . 'ms';
if (mysql_version() >= 50503) if (mysql_version() >= 50503)
query('SET NAMES utf8mb4') or error(db_error()); query('SET NAMES utf8mb4') or error(db_error());
else else
@ -117,12 +125,13 @@ function query($query) {
$query = $pdo->query($query); $query = $pdo->query($query);
if (!$query) if (!$query)
return false; return false;
$time = round((microtime(true) - $start) * 1000, 2) . 'ms'; $time = microtime(true) - $start;
$debug['sql'][] = array( $debug['sql'][] = array(
'query' => $query->queryString, 'query' => $query->queryString,
'rows' => $query->rowCount(), 'rows' => $query->rowCount(),
'time' => '~' . $time 'time' => '~' . round($time * 1000, 2) . 'ms'
); );
$debug['time']['db_queries'] += $time;
return $query; return $query;
} }

32
inc/functions.php

@ -81,14 +81,6 @@ function loadConfig() {
$__version = file_exists('.installed') ? trim(file_get_contents('.installed')) : false; $__version = file_exists('.installed') ? trim(file_get_contents('.installed')) : false;
$config['version'] = $__version; $config['version'] = $__version;
if ($config['debug']) {
if (!isset($debug)) {
$debug = array('sql' => array(), 'exec' => array(), 'purge' => array(), 'cached' => array(), 'write' => array());
$debug['start'] = $microtime_start;
$debug['start_debug'] = microtime(true);;
}
}
date_default_timezone_set($config['timezone']); date_default_timezone_set($config['timezone']);
if (!isset($config['global_message'])) if (!isset($config['global_message']))
@ -199,6 +191,25 @@ function loadConfig() {
if ($config['cache']['enabled']) if ($config['cache']['enabled'])
require_once 'inc/cache.php'; require_once 'inc/cache.php';
event('load-config'); event('load-config');
if ($config['debug']) {
if (!isset($debug)) {
$debug = array(
'sql' => array(),
'exec' => array(),
'purge' => array(),
'cached' => array(),
'write' => array(),
'time' => array(
'db_queries' => 0,
'exec' => 0,
),
'start' => $microtime_start,
'start_debug' => microtime(true)
);
$debug['start'] = $microtime_start;
}
}
} }
function basic_error_function_because_the_other_isnt_loaded_yet($message, $priority = true) { function basic_error_function_because_the_other_isnt_loaded_yet($message, $priority = true) {
@ -2056,12 +2067,13 @@ function shell_exec_error($command, $suppress_stdout = false) {
$return = preg_replace('/TB_SUCCESS$/', '', $return); $return = preg_replace('/TB_SUCCESS$/', '', $return);
if ($config['debug']) { if ($config['debug']) {
$time = round((microtime(true) - $start) * 1000, 2) . 'ms'; $time = microtime(true) - $start;
$debug['exec'][] = array( $debug['exec'][] = array(
'command' => $command, 'command' => $command,
'time' => '~' . $time, 'time' => '~' . round($time * 1000, 2) . 'ms',
'response' => $return ? $return : null 'response' => $return ? $return : null
); );
$debug['time']['exec'] += $time;
} }
return $return === 'TB_SUCCESS' ? false : $return; return $return === 'TB_SUCCESS' ? false : $return;

6
inc/template.php

@ -46,8 +46,8 @@ function Element($templateFile, array $options) {
if (isset($options['body']) && $config['debug']) { if (isset($options['body']) && $config['debug']) {
if (isset($debug['start'])) { if (isset($debug['start'])) {
$debug['time'] = '~' . round((microtime(true) - $debug['start']) * 1000, 2) . 'ms'; $debug['time']['total'] = '~' . round((microtime(true) - $debug['start']) * 1000, 2) . 'ms';
$debug['time (initialization)'] = '~' . round(($debug['start_debug'] - $debug['start']) * 1000, 2) . 'ms'; $debug['time']['init'] = '~' . round(($debug['start_debug'] - $debug['start']) * 1000, 2) . 'ms';
unset($debug['start']); unset($debug['start']);
unset($debug['start_debug']); unset($debug['start_debug']);
} }
@ -55,6 +55,8 @@ function Element($templateFile, array $options) {
$debug['build_pages'] = $build_pages; $debug['build_pages'] = $build_pages;
$debug['included'] = get_included_files(); $debug['included'] = get_included_files();
$debug['memory'] = round(memory_get_usage(true) / (1024 * 1024), 2) . ' MiB'; $debug['memory'] = round(memory_get_usage(true) / (1024 * 1024), 2) . ' MiB';
$debug['time']['db_queries'] = '~' . round($debug['time']['db_queries'] * 1000, 2) . 'ms';
$debug['time']['exec'] = '~' . round($debug['time']['exec'] * 1000, 2) . 'ms';
$options['body'] .= $options['body'] .=
'<h3>Debug</h3><pre style="white-space: pre-wrap;font-size: 10px;">' . '<h3>Debug</h3><pre style="white-space: pre-wrap;font-size: 10px;">' .
str_replace("\n", '<br/>', utf8tohtml(print_r($debug, true))) . str_replace("\n", '<br/>', utf8tohtml(print_r($debug, true))) .

6
mod.php

@ -8,6 +8,9 @@ require 'inc/functions.php';
require 'inc/mod/pages.php'; require 'inc/mod/pages.php';
require 'inc/mod/auth.php'; require 'inc/mod/auth.php';
if ($config['debug'])
$parse_start_time = microtime(true);
// Fix for magic quotes // Fix for magic quotes
if (get_magic_quotes_gpc()) { if (get_magic_quotes_gpc()) {
function strip_array($var) { function strip_array($var) {
@ -156,8 +159,9 @@ foreach ($pages as $uri => $handler) {
$debug['mod_page'] = array( $debug['mod_page'] = array(
'req' => $query, 'req' => $query,
'match' => $uri, 'match' => $uri,
'handler' => $handler 'handler' => $handler,
); );
$debug['time']['parse_mod_req'] = '~' . round((microtime(true) - $parse_start_time) * 1000, 2) . 'ms';
} }
if (is_string($handler)) { if (is_string($handler)) {

Loading…
Cancel
Save