Commit 5bebdc73 authored by Penny Leach's avatar Penny Leach
Browse files

basic performance profiling. needs enabling in config.php (see notes

in config-dist.php)
parent 203120e9
......@@ -83,6 +83,11 @@ $cfg->log_environ_targets = LOG_TARGET_SCREEN | LOG_TARGET_ERRORLOG;
// but probably only warnings are useful on a live site.
$cfg->log_backtrace_levels = LOG_LEVEL_WARN | LOG_LEVEL_ENVIRON;
// capture performance information and print it
// $cfg->perftofoot = true; // needs a call to mahara_performance_info (smarty callback) - see default theme's footer.tpl
// $cfg->perftolog = true;
// if neither are set, performance info wont be captured.
// mail handling
// if you want mahara to use smtp servers to send mail, enter one or more here
// blank means mahara will use the default PHP method.
......
......@@ -48,6 +48,8 @@ if (!is_readable($CFG->docroot . 'config.php')) {
exit;
}
init_performance_info();
require('config.php');
$CFG = (object)array_merge((array)$cfg, (array)$CFG);
......@@ -192,4 +194,31 @@ if (defined('JSON')) {
}
}
/*
* Initializes our performance info early.
*
* Pairs up with get_performance_info() which is actually
* in lib/mahara.php. This function is here so that we can
* call it before all the libs are pulled in.
*
* @uses $PERF
*/
function init_performance_info() {
global $PERF;
$PERF = new StdClass;
$PERF->dbqueries = 0;
$PERF->logwrites = 0;
if (function_exists('microtime')) {
$PERF->starttime = microtime();
}
if (function_exists('memory_get_usage')) {
$PERF->startmemory = memory_get_usage();
}
if (function_exists('posix_times')) {
$PERF->startposixtimes = posix_times();
}
}
?>
......@@ -569,5 +569,9 @@ $string['youraccounthasbeensuspendedtext'] = 'Your account has been suspended';
$string['youraccounthasbeenunsuspended'] = 'Your account has been unsuspended';
$string['youraccounthasbeenunsuspendedtext'] = 'Your account has been unsuspended'; // @todo: more info?
// size of stuff
$string['sizemb'] = 'MB';
$string['sizekb'] = 'KB';
$string['sizegb'] = 'GB';
$string['sizeb'] = 'b';
?>
<?php
/**
* This program is part of Mahara
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* @package mahara
* @subpackage lang
* @author Penny Leach <penny@catalyst.net.nz>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL
* @copyright (C) 2006,2007 Catalyst IT Ltd http://catalyst.net.nz
*
*/
defined('INTERNAL') || die();
$string['memoryused'] = 'Memory';
$string['timeused'] = 'Execution time';
$string['seconds'] = 'seconds';
$string['included'] = 'Included files';
$string['dbqueries'] = 'DB queries';
$string['ticks'] = 'ticks';
$string['sys'] = 'sys';
$string['user'] = 'user';
$string['cuser'] = 'cuser';
$string['csys'] = 'csys';
$string['serverload'] = 'Server load';
?>
......@@ -51,6 +51,7 @@ function execute_sql($command) {
try {
$result = $db->Execute($command);
increment_perf_db();
}
catch (ADODB_Exception $e) {
log_debug($e->getMessage() . "Command was: $command");
......@@ -365,8 +366,10 @@ function get_recordset_sql($sql, $values=null, $limitfrom=null, $limitnum=null)
if (!empty($values) && is_array($values) && count($values) > 0) {
$stmt = $db->Prepare($sql);
$rs = $db->Execute($stmt, $values);
increment_perf_db();
} else {
$rs = $db->Execute($sql);
increment_perf_db();
}
}
}
......@@ -772,6 +775,7 @@ function set_field_select($table, $newfield, $newvalue, $select, $values) {
$sql = 'UPDATE '. get_config('dbprefix') . $table .' SET '. $newfield .' = ? ' . $select;
try {
$stmt = $db->Prepare($sql);
increment_perf_db();
return $db->Execute($stmt, $values);
}
catch (ADODB_Exception $e) {
......@@ -807,6 +811,7 @@ function delete_records($table, $field1=null, $value1=null, $field2=null, $value
$sql = 'DELETE FROM '. get_config('dbprefix') . $table . ' ' . $select;
try {
$stmt = $db->Prepare($sql);
increment_perf_db();
return $db->Execute($stmt,$values);
}
catch (ADODB_Exception $e) {
......@@ -837,8 +842,10 @@ function delete_records_sql($sql, $values=null) {
$result = false;
if (!empty($values) && is_array($values) && count($values) > 0) {
$stmt = $db->Prepare($sql);
increment_perf_db();
$result = $db->Execute($stmt, $values);
} else {
increment_perf_db();
$result = $db->Execute($sql);
}
}
......@@ -918,6 +925,7 @@ function insert_record($table, $dataobject, $primarykey=false, $returnpk=false)
// Run the SQL statement
try {
$stmt = $db->Prepare($insertSQL);
increment_perf_db();
$rs = $db->Execute($stmt,$ddd);
}
catch (ADODB_Exception $e) {
......@@ -944,6 +952,7 @@ function insert_record($table, $dataobject, $primarykey=false, $returnpk=false)
// try to get the primary key based on id
try {
$oidsql = 'SELECT '. $primarykey .' FROM '. get_config('dbprefix') . $table .' WHERE oid = '. $id;
increment_perf_db();
$rs = $db->Execute($oidsql);
if ($rs->RecordCount() == 1) {
return (integer)$rs->fields[0];
......@@ -1072,6 +1081,7 @@ function update_record($table, $dataobject, $where=null) {
$sql = 'UPDATE '. get_config('dbprefix') . $table .' SET '. $update .' WHERE ' . $whereclause;
try {
$stmt = $db->Prepare($sql);
increment_perf_db();
$rs = $db->Execute($stmt,array_merge($values, $wherevalues));
return true;
}
......@@ -1168,6 +1178,7 @@ function where_values_prepared($value1=null, $value2=null, $value3=null, $value4
function column_type($table, $column) {
global $db;
increment_perf_db();
if(!$rs = $db->Execute('SELECT ' . $column.' FROM ' . get_config('dbprefix') . $table . ' WHERE 1=2')) {
return false;
}
......@@ -1269,6 +1280,7 @@ function db_format_tsfield($field, $as = null) {
function configure_dbconnection() {
global $db;
increment_perf_db();
$db->Execute("SET NAMES 'utf8'");
// more later..
......@@ -1358,4 +1370,12 @@ function create_sql_exception_message($e, $sql, $values) {
return $message;
}
function increment_perf_db() {
if (!get_config('perftolog') && !get_config('perftofoot')) {
return true;
}
global $PERF;
$PERF->dbqueries++;
}
?>
......@@ -1448,4 +1448,112 @@ function get_mahara_install_subdirectory() {
return substr($wwwroot, strpos($wwwroot, '/'));
}
/**
*** get_performance_info() pairs up with init_performance_info()
*** loaded in init.php. Returns an array with 'html' and 'txt'
*** values ready for use, and each of the individual stats provided
*** separately as well.
***
**/
function get_performance_info() {
if (!get_config('perftofoot') && !get_config('perftolog')) {
return array();
}
global $PERF;
$info = array();
$info['realtime'] = microtime_diff($PERF->starttime, microtime());
if (function_exists('memory_get_usage')) {
$info['memory_total'] = memory_get_usage();
$info['memory_growth'] = memory_get_usage() - $PERF->startmemory;
}
$inc = get_included_files();
$info['includecount'] = count($inc);
if (!empty($PERF->dbqueries)) {
$info['dbqueries'] = $PERF->dbqueries;
}
if (function_exists('posix_times')) {
$ptimes = posix_times();
if (is_array($ptimes)) {
foreach ($ptimes as $key => $val) {
$info[$key] = $ptimes[$key] - $PERF->startposixtimes[$key];
}
}
}
// Grab the load average for the last minute
// /proc will only work under some linux configurations
// while uptime is there under MacOSX/Darwin and other unices
if (is_readable('/proc/loadavg') && $loadavg = @file('/proc/loadavg')) {
list($server_load) = explode(' ', $loadavg[0]);
unset($loadavg);
} else if ( function_exists('is_executable') && is_executable('/usr/bin/uptime') && $loadavg = `/usr/bin/uptime` ) {
if (preg_match('/load averages?: (\d+[\.,:]\d+)/', $loadavg, $matches)) {
$server_load = $matches[1];
} else {
log_debug('PERF: Could not parse uptime output!');
}
}
if (!empty($server_load)) {
$info['serverload'] = $server_load;
}
return $info;
}
/**
* microtime_diff
*
* @param string $a ?
* @param string $b ?
* @return string
* @todo Finish documenting this function
*/
function microtime_diff($a, $b) {
list($a_dec, $a_sec) = explode(' ', $a);
list($b_dec, $b_sec) = explode(' ', $b);
return $b_sec - $a_sec + $b_dec - $a_dec;
}
/**
* Converts bytes into display form
*
* @param string $size ?
* @return string
* @staticvar string $gb Localized string for size in gigabytes
* @staticvar string $mb Localized string for size in megabytes
* @staticvar string $kb Localized string for size in kilobytes
* @staticvar string $b Localized string for size in bytes
* @todo Finish documenting this function. Verify return type.
*/
function display_size($size) {
static $gb, $mb, $kb, $b;
if (empty($gb)) {
$gb = get_string('sizegb');
$mb = get_string('sizemb');
$kb = get_string('sizekb');
$b = get_string('sizeb');
}
if ($size >= 1073741824) {
$size = round($size / 1073741824 * 10) / 10 . $gb;
} else if ($size >= 1048576) {
$size = round($size / 1048576 * 10) / 10 . $mb;
} else if ($size >= 1024) {
$size = round($size / 1024 * 10) / 10 . $kb;
} else {
$size = $size .' '. $b;
}
return $size;
}
?>
<?php
/**
* Smarty plugin
* @package Smarty
* @subpackage plugins
*/
/**
* Smarty {mahara_performance_info} function plugin
*
* Type: function<br>
* Name: mahara_performance_info<br>
* Date: June 22, 2006<br>
* Purpose: Fetch internationalized strings
* @author Penny Leach <penny@catalyst.net.nz>
* @version 1.0
* @param array
* @param Smarty
* @return html to display in the footer.
*/
function smarty_function_mahara_performance_info($params, &$smarty) {
if (!get_config('perftofoot') && !get_config('perftolog')) {
return;
}
$info = get_performance_info();
$smarty = smarty();
foreach ($info as $key => $value) {
$smarty->assign('perf_' . $key, $value);
}
// extras
$smarty->assign('perf_memory_total_display', display_size($info['memory_total']));
$smarty->assign('perf_memory_growth_display', display_size($info['memory_growth']));
if (get_config('perftolog')) {
$logstring = 'PERF: ' . strip_querystring(get_script_path()). ': ';
$logstring .= ' memory_total: '.$info['memory_total'].'B (' . display_size($info['memory_total']).') memory_growth: '.$info['memory_growth'].'B ('.display_size($info['memory_growth']).')';
$logstring .= ' time: '.$info['realtime'].'s';
$logstring .= ' includecount: '.$info['includecount'];
$logstring .= ' dbqueries: '.$info['dbqueries'];
$logstring .= ' ticks: ' . $info['ticks'] . ' user: ' . $info['utime'] . ' sys: ' . $info['stime'] .' cuser: ' . $info['cutime'] . ' csys: ' . $info['cstime'];
$logstring .= ' serverload: ' . $info['serverload'];
log_debug($logstring);
}
if (get_config('perftofoot')) {
return $smarty->fetch('performancefooter.tpl');
}
}
?>
......@@ -1720,3 +1720,8 @@ a.pieform-calendar-toggle img {
color: #547C22;
font-size: 13px;
}
.performanceinfo {
font-size:9px;
text-align:center;
margin-top:4px;
}
......@@ -11,6 +11,7 @@
<div id="bottom-corners">
<div class="footer-left"><img src="{theme_path location='images/footer_corner_botleft.gif'}" border="0" alt=""></div><div class="footer-right"><img src="{theme_path location='images/footer_corner_botright.gif'}" border="0" alt=""></div>
</div>
{mahara_performance_info}
</div>
</body>
</html>
<div class="performanceinfo">
{if $perf_memory_total}
<span class="memoryused">{str tag="memoryused" section="performance"}: {$perf_memory_total_display}</span><br />
{/if}
{if $perf_realtime}
<span class="timeused">{str tag="timeused" section="performance"}: {$perf_realtime} {str tag="seconds" section="performance"}</span><br />
{/if}
{if $perf_includecount}
<span class="included">{str tag="included" section="performance"}: {$perf_includecount}</span><br />
{/if}
{if $perf_dbqueries}
<span class="dbqueries">{str tag="dbqueries" section="performance"}: {$perf_dbqueries}</span><br />
{/if}
{if $perf_ticks}
<span class="posixtimes">{str tag="ticks" section="performance"}: {$perf_ticks} {str tag="user" section="performance"}: {$perf_user}
{str tag="sys" section="performance"}: {$perf_sys} {str tag="cuser" section="performance"}: {$perf_cutime}
{str tag="csys" section="performance"}: {$perf_cstime}</span><br />
{/if}
{if $perf_serverload}
<span class="serverload">{str tag="serverload" section="performance"}: {$perf_serverload}</span><br />
{/if}
</div>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment