Skip to content

Commit

Permalink
logger::default_stats(): call get_performance_info()
Browse files Browse the repository at this point in the history
This is suboptimal, the same call may have already been made in request_shutdown()
  • Loading branch information
srdjan-catalyst committed Mar 4, 2022
1 parent c729d48 commit 6718d90
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
30 changes: 27 additions & 3 deletions classes/logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,36 @@ private static function default_log_stream(): ?string {
* @return array
*/
public static function default_stats(): array {
global $CFG, $PERF, $DB, $PAGE;
global $CFG, $PERF;

// XXX This is lame, same call is in request_shutdown()
$perf = get_performance_info();
list($db_reads, $db_writes) = explode('/', $perf['dbqueries']);
list($cache_hits, $cache_misses, $cache_sets) = explode(' / ', $perf['cachesused']);

$stats = [
'db_reads: '.$DB->perf_get_reads(),
'db_writes: '.$DB->perf_get_writes(),
"db_reads: $db_reads",
"db_writes: $db_writes",
];

if ($cache_hits > 0 || $cache_misses > 0 || $cache_sets > 0 ) {
// otherwise perfdebug is not set, and stats are not recorded
$stats[] = "cache_hits: $cache_hits";
$stats[] = "cache_misses: $cache_misses";
$stats[] = "cache_sets: $cache_sets";
}

if (isset($PERF->sessionlock['gain'])) {
list($msec, $sec) = explode(' ', $PERF->starttime);
$stats[] = 'sess_lock_acquired_in: '.($PERF->sessionlock['gain'] - ((float) $sec + (float) $msec));
$stats[] = 'sess_lock_type: '.(
defined('READ_ONLY_SESSION') && !empty($CFG->enable_read_only_sessions) && READ_ONLY_SESSION ? 'readonly' : 'write'
);

} else {
$stats[] = 'sess_lock_type: none';
}

if (function_exists('memory_get_peak_usage')) {
$stats[] = 'memory_peak: '.memory_get_peak_usage();
}
Expand Down
2 changes: 1 addition & 1 deletion tests/logger_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function test_log() {
}

public function test_default_stats() {
$cnt = function_exists('memory_get_peak_usage') ? 3 : 2;
$cnt = function_exists('memory_get_peak_usage') ? 4 : 3;

$stats = logger::default_stats();
$this->assertEquals($cnt, count($stats));
Expand Down

0 comments on commit 6718d90

Please sign in to comment.