Commit 02245a28 by Scott Vivian

Add new autoloaded classes Q2A_Util_Usage and Q2A_Util_String

Debugging functions converted to use Usage.
String class not yet in use.
parent 5db31475
<?php
/*
Question2Answer (c) Gideon Greenspan
http://www.question2answer.org/
License: GPLv2+ <http://www.question2answer.org/license.php>
File: qa-include/qa-util-debug.php
Description: Debugging stuff, currently used for tracking resource usage
*/
class Q2A_Util_Usage
{
public $database_usage;
public $database_queries;
public $usage_start;
public $usage_last;
/**
* Initialize the counts of resource usage
*/
public function __construct()
{
global $qa_database_usage, $qa_database_queries, $qa_usage_start, $qa_usage_last;
$qa_database_usage = array('queries'=>0, 'clock'=>0);
$qa_database_queries = '';
$qa_usage_last = $qa_usage_start = $this->getCurrent();
}
/**
* Return an array representing the resource usage as of now
*/
public function getCurrent()
{
global $qa_database_usage;
$usage = array(
'files' => count(get_included_files()),
'queries' => $qa_database_usage['queries'],
'ram' => function_exists('memory_get_usage') ? memory_get_usage() : 0,
'clock' => array_sum(explode(' ', microtime())),
'mysql' => $qa_database_usage['clock'],
);
if (function_exists('getrusage')) {
$rusage = getrusage();
$usage['cpu'] = $rusage["ru_utime.tv_sec"] + $rusage["ru_stime.tv_sec"]
+ ($rusage["ru_utime.tv_usec"] + $rusage["ru_stime.tv_usec"]) / 1000000;
}
else
$usage['cpu'] = 0;
$usage['other'] = $usage['clock'] - $usage['cpu'] - $usage['mysql'];
return $usage;
}
/**
* Mark the beginning of a new stage of script execution and store usages accordingly
*/
public function mark($stage)
{
global $qa_usage_last, $qa_usage_stages;
$usage = $this->getCurrent();
$qa_usage_stages[$stage] = $this->delta($qa_usage_last, $usage);
$qa_usage_last = $usage;
}
/**
* Output an (ugly) block of HTML detailing all resource usage and database queries
*/
public function output()
{
global $qa_usage_start, $qa_usage_stages, $qa_database_queries;
$totaldelta = $this->delta($qa_usage_start, $this->getCurrent());
?>
<style>
.debug-table { border-collapse: collapse; box-sizing: border-box; width: 100%; margin: 20px auto; }
.debug-table tr { background-color: #ccc; }
.debug-table td { padding: 10px; }
td.debug-cell-files { width: 30%; padding-right: 5px; }
td.debug-cell-queries { width: 70%; padding-left: 5px; }
textarea.debug-output { box-sizing: border-box; width: 100%; font: 12px monospace; color: #000; }
</style>
<table class="debug-table">
<tbody>
<tr>
<td colspan="2"><?php
echo $this->line('Total', $totaldelta, $totaldelta) . "<br>\n";
foreach ($qa_usage_stages as $stage => $stagedelta)
echo '<br>' . $this->line(ucfirst($stage), $stagedelta, $totaldelta) . "\n";
?></td>
</tr>
<tr>
<td class="debug-cell-files">
<textarea class="debug-output" cols="40" rows="20"><?php
foreach (get_included_files() as $file)
echo qa_html(implode('/', array_slice(explode('/', $file), -3)))."\n";
?></textarea>
</td>
<td class="debug-cell-queries">
<textarea class="debug-output" cols="40" rows="20"><?=qa_html($qa_database_queries)?></textarea>
</td>
</tr>
</tbody>
</table>
<?php
}
/**
* Return the difference between two resource usage arrays, as an array
*/
private function delta($oldusage, $newusage)
{
$delta = array();
foreach ($newusage as $key => $value)
$delta[$key] = max(0, $value-@$oldusage[$key]);
return $delta;
}
/**
* Return HTML to represent the resource $usage, showing appropriate proportions of $totalusage
*/
private function line($stage, $usage, $totalusage)
{
return sprintf(
"%s &ndash; <b>%.1fms</b> (%d%%) &ndash; PHP %.1fms (%d%%), MySQL %.1fms (%d%%), Other %.1fms (%d%%) &ndash; %d PHP %s, %d DB %s, %dk RAM (%d%%)",
$stage,
$usage['clock'] * 1000,
$usage['clock'] * 100 / $totalusage['clock'],
$usage['cpu'] * 1000,
$usage['cpu'] * 100 / $totalusage['clock'],
$usage['mysql'] * 1000,
$usage['mysql'] * 100 / $totalusage['clock'],
$usage['other'] * 1000,
$usage['other'] * 100 / $totalusage['clock'],
$usage['files'],
$usage['files'] == 1 ? 'file' : 'files',
$usage['queries'],
$usage['queries'] == 1 ? 'query' : 'queries',
$usage['ram'] / 1024,
$usage['ram'] ? ($usage['ram'] * 100 / $totalusage['ram']) : 0
);
}
}
......@@ -196,8 +196,8 @@
// Start performance monitoring
if (QA_DEBUG_PERFORMANCE) {
require_once 'qa-util-debug.php';
qa_usage_init();
global $qa_usage;
$qa_usage = new Q2A_Util_Usage;
}
// More for WordPress integration
......
......@@ -465,8 +465,10 @@
global $qa_template, $qa_page_error_html;
if (QA_DEBUG_PERFORMANCE)
qa_usage_mark('control');
if (QA_DEBUG_PERFORMANCE) {
global $qa_usage;
$qa_usage->mark('control');
}
$request=qa_request();
$requestlower=qa_request();
......@@ -779,6 +781,8 @@
// Below are the steps that actually execute for this file - all the above are function definitions
global $qa_usage;
qa_report_process_stage('init_page');
qa_db_connect('qa_page_db_fail_handler');
......@@ -787,27 +791,26 @@
qa_check_login_modules();
if (QA_DEBUG_PERFORMANCE)
qa_usage_mark('setup');
$qa_usage->mark('setup');
qa_check_page_clicks();
$qa_content=qa_get_request_content();
$qa_content = qa_get_request_content();
if (is_array($qa_content)) {
if (QA_DEBUG_PERFORMANCE)
qa_usage_mark('view');
$qa_usage->mark('view');
qa_output_content($qa_content);
if (QA_DEBUG_PERFORMANCE)
qa_usage_mark('theme');
$qa_usage->mark('theme');
if (qa_do_content_stats($qa_content))
if (QA_DEBUG_PERFORMANCE)
qa_usage_mark('stats');
if (qa_do_content_stats($qa_content) && QA_DEBUG_PERFORMANCE)
$qa_usage->mark('stats');
if (QA_DEBUG_PERFORMANCE)
qa_usage_output();
$qa_usage->output();
}
qa_db_disconnect();
......
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