Commit 1ac995e0 by Scott Vivian

Add query logging function to Usage, refactor qa-db.php

parent d63ef701
...@@ -11,21 +11,22 @@ ...@@ -11,21 +11,22 @@
class Q2A_Util_Usage class Q2A_Util_Usage
{ {
public $database_usage; private $stages;
public $database_queries; private $startUsage;
public $usage_start; private $prevUsage;
public $usage_last; private $databaseUsage;
private $databaseQueryLog;
/** /**
* Initialize the counts of resource usage * Initialize the counts of resource usage
*/ */
public function __construct() public function __construct()
{ {
global $qa_database_usage, $qa_database_queries, $qa_usage_start, $qa_usage_last; $this->stages = array();
$this->databaseUsage = array('queries'=>0, 'clock'=>0);
$this->databaseQueryLog = '';
$qa_database_usage = array('queries'=>0, 'clock'=>0); $this->prevUsage = $this->startUsage = $this->getCurrent();
$qa_database_queries = '';
$qa_usage_last = $qa_usage_start = $this->getCurrent();
} }
/** /**
...@@ -33,14 +34,12 @@ class Q2A_Util_Usage ...@@ -33,14 +34,12 @@ class Q2A_Util_Usage
*/ */
public function getCurrent() public function getCurrent()
{ {
global $qa_database_usage;
$usage = array( $usage = array(
'files' => count(get_included_files()), 'files' => count(get_included_files()),
'queries' => $qa_database_usage['queries'], 'queries' => $this->databaseUsage['queries'],
'ram' => function_exists('memory_get_usage') ? memory_get_usage() : 0, 'ram' => function_exists('memory_get_usage') ? memory_get_usage() : 0,
'clock' => array_sum(explode(' ', microtime())), 'clock' => array_sum(explode(' ', microtime())),
'mysql' => $qa_database_usage['clock'], 'mysql' => $this->databaseUsage['clock'],
); );
if (function_exists('getrusage')) { if (function_exists('getrusage')) {
...@@ -61,11 +60,29 @@ class Q2A_Util_Usage ...@@ -61,11 +60,29 @@ class Q2A_Util_Usage
*/ */
public function mark($stage) public function mark($stage)
{ {
global $qa_usage_last, $qa_usage_stages;
$usage = $this->getCurrent(); $usage = $this->getCurrent();
$qa_usage_stages[$stage] = $this->delta($qa_usage_last, $usage); $this->stages[$stage] = $this->delta($this->prevUsage, $usage);
$qa_usage_last = $usage; $this->prevUsage = $usage;
}
/**
* Logs query and updates database usage stats
*/
public function logDatabaseQuery($query, $usedtime, $gotrows, $gotcolumns)
{
$this->databaseUsage['clock'] += $usedtime;
if (strlen($this->databaseQueryLog) < 1048576) { // don't keep track of big tests
$rowcolstring = '';
if (is_numeric($gotrows))
$rowcolstring .= ' - ' . $gotrows . ($gotrows == 1 ? ' row' : ' rows');
if (is_numeric($gotcolumns))
$rowcolstring .= ' - ' . $gotcolumns . ($gotcolumns == 1 ? ' column' : ' columns');
$this->databaseQueryLog .= $query . "\n\n" . sprintf('%.2f ms', $usedtime*1000) . $rowcolstring . "\n\n";
}
$this->databaseUsage['queries']++;
} }
/** /**
...@@ -73,9 +90,7 @@ class Q2A_Util_Usage ...@@ -73,9 +90,7 @@ class Q2A_Util_Usage
*/ */
public function output() public function output()
{ {
global $qa_usage_start, $qa_usage_stages, $qa_database_queries; $totaldelta = $this->delta($this->startUsage, $this->getCurrent());
$totaldelta = $this->delta($qa_usage_start, $this->getCurrent());
?> ?>
<style> <style>
.debug-table { border-collapse: collapse; box-sizing: border-box; width: 100%; margin: 20px auto; } .debug-table { border-collapse: collapse; box-sizing: border-box; width: 100%; margin: 20px auto; }
...@@ -93,7 +108,7 @@ class Q2A_Util_Usage ...@@ -93,7 +108,7 @@ class Q2A_Util_Usage
<tr> <tr>
<td colspan="2"><?php <td colspan="2"><?php
echo $this->line('Total', $totaldelta, $totaldelta) . "<br>\n"; echo $this->line('Total', $totaldelta, $totaldelta) . "<br>\n";
foreach ($qa_usage_stages as $stage => $stagedelta) foreach ($this->stages as $stage => $stagedelta)
echo '<br>' . $this->line(ucfirst($stage), $stagedelta, $totaldelta) . "\n"; echo '<br>' . $this->line(ucfirst($stage), $stagedelta, $totaldelta) . "\n";
?></td> ?></td>
</tr> </tr>
...@@ -105,7 +120,7 @@ class Q2A_Util_Usage ...@@ -105,7 +120,7 @@ class Q2A_Util_Usage
?></textarea> ?></textarea>
</td> </td>
<td class="debug-cell-queries"> <td class="debug-cell-queries">
<textarea class="debug-output" cols="40" rows="20"><?=qa_html($qa_database_queries)?></textarea> <textarea class="debug-output" cols="40" rows="20"><?=qa_html($this->databaseQueryLog)?></textarea>
</td> </td>
</tr> </tr>
</tbody> </tbody>
......
...@@ -162,34 +162,21 @@ ...@@ -162,34 +162,21 @@
if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); } if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); }
if (QA_DEBUG_PERFORMANCE) { if (QA_DEBUG_PERFORMANCE) {
global $qa_database_usage, $qa_database_queries; global $qa_usage;
// time the query
$oldtime = array_sum(explode(' ', microtime())); $oldtime = array_sum(explode(' ', microtime()));
$result = qa_db_query_execute($query); $result = qa_db_query_execute($query);
$usedtime = array_sum(explode(' ', microtime())) - $oldtime; $usedtime = array_sum(explode(' ', microtime())) - $oldtime;
if (is_array($qa_database_usage)) { // fetch counts
$qa_database_usage['clock'] += $usedtime; $gotrows = $gotcolumns = null;
if ($result instanceof mysqli_result) {
if (strlen($qa_database_queries) < 1048576) { // don't keep track of big tests $gotrows = $result->num_rows;
$gotrows = $gotcolumns = null; $gotcolumns = $result->field_count;
if ($result instanceof mysqli_result) {
$gotrows = $result->num_rows;
$gotcolumns = $result->field_count;
}
$rowcolstring = '';
if (is_numeric($gotrows))
$rowcolstring .= ' - ' . $gotrows . ($gotrows == 1 ? ' row' : ' rows');
if (is_numeric($gotcolumns))
$rowcolstring .= ' - ' . $gotcolumns . ($gotcolumns == 1 ? ' column' : ' columns');
$qa_database_queries .= $query . "\n\n" . sprintf('%.2f ms', $usedtime*1000) . $rowcolstring . "\n\n";
}
$qa_database_usage['queries']++;
} }
$qa_usage->logDatabaseQuery($query, $usedtime, $gotrows, $gotcolumns);
} }
else else
$result = qa_db_query_execute($query); $result = qa_db_query_execute($query);
......
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