Skip to content

Instantly share code, notes, and snippets.

@atsu666
Created December 20, 2018 13:07
Show Gist options
  • Save atsu666/bc1fa4ed5c9be3df5ffda26fcab189d7 to your computer and use it in GitHub Desktop.
Save atsu666/bc1fa4ed5c9be3df5ffda26fcab189d7 to your computer and use it in GitHub Desktop.
extension/acms/Hook.php
public function beforeShutdown()
{
if (1
&& is_int(strpos(MIME_TYPE, 'html'))
&& !is_ajax()
) {
global $bench;
global $bench_build;
global $bench_rewrite_path;
global $bench_boot;
global $bench_global_vars;
global $bench_template;
global $bench_slow_query;
$Tpl = new \Template('<hr />'
. '<div class="acms-admin-benchmark-container">'
. '<div class="acmsBenchmark">'
. '<h1>グローバル変数</h1>'
. '<table class="acmsBenchmarkTable">'
. '<thead>'
. '<tr class="head">'
. '<th>global vars</th><th>values</th>'
. '</tr>'
. '</thead>'
. '<tbody><!-- BEGIN global_vars:loop -->'
. '<tr>'
. '<th>{key}</th><td>{value}</td>'
. '</tr><!-- END global_vars:loop -->'
. '</tbody>'
. '</table>'
. '</div>'
. '<div class="acmsBenchmark">'
. '<h1>モジュール処理時間</h1>'
. '<table class="acmsBenchmarkTable">'
. '<thead>'
. '<tr>'
. '<th>module</th><th>identifier</th><th>run time</th>'
. '</tr>'
. '</thead>'
. '<tbody><!-- BEGIN module:loop -->'
. '<tr>'
. '<th>{module}</th><td>{identifier}</td><td>{run_time}</td>'
. '</tr><!-- END module:loop -->'
. '</tbody>'
. '</table>'
. '</div>'
. '<!-- BEGIN query:veil --><div class="acmsBenchmark">'
. '<h1>Slow Query</h1>'
. '<table class="acmsBenchmarkTable">'
. '<thead>'
. '<tr>'
. '<th>time</th><th>query</th>'
. '</tr>'
. '<tbody><!-- BEGIN query:loop -->'
. '<tr>'
. '<td>{time}</td><td>{query}</td>'
. '</tr><!-- END query:loop -->'
. '</tbody>'
. '</table>'
. '</div><!-- END query:veil -->'
. '<div class="acmsBenchmark">'
. '<h1>テンプレート</h1>'
. '<table class="acmsBenchmarkTable">'
. '<thead>'
. '<tr>'
. '<th>template</th>'
. '</tr>'
. '<tbody><!-- BEGIN template:loop -->'
. '<tr>'
. '<td>{template}</td>'
. '</tr><!-- END template:loop -->'
. '</tbody>'
. '</table>'
. '</div>'
. '<div class="acmsBenchmark">'
. '<h1>メモリー</h1>'
. '<table class="acmsBenchmarkTable">'
. '<thead>'
. '<tr>'
. '<th>key</th><th>value</th>'
. '</tr>'
. '<tbody>'
. '<tr>'
. '<td>memory_limit</td><td>' . ini_get('memory_limit') . '</td>'
. '</tr>'
. '<tr>'
. '<td>memory_get_usage</td><td>' . byteConvert(memory_get_usage()) . '</td>'
. '</tr>'
. '<tr>'
. '<td>memory_get_peak_usage</td><td>' . byteConvert(memory_get_peak_usage()) . '</td>'
. '</tr>'
. '</tbody>'
. '</table>'
. '</div>'
. '</div>');
if (!empty($bench)) {
foreach ($bench as $title => $all) {
$sort = array();
foreach ($all as $row) {
$sort[] = $row['sort_key'];
}
array_multisort($sort, SORT_DESC, SORT_NUMERIC, $all);
array_push($all, array(
'module' => 'MODULE TOTAL TIME',
'run_time' => array_sum($sort),
), array(
'module' => 'BOOT TOTAL TIME',
'run_time' => $bench_boot,
), array(
'module' => 'BUILD TOTAL TIME',
'run_time' => $bench_build,
), array(
'module' => 'REWRITE PATH TOTAL TIME',
'run_time' => $bench_rewrite_path,
), array(
'module' => 'PROCCESS TIME',
'run_time' => sprintf('%0.6f', microtime(true) - START_TIME),
));
foreach ($all as $row) {
$Tpl->add($title . ':loop', $row);
}
}
}
if (!empty($bench_global_vars)) {
foreach ($bench_global_vars as $key => $val) {
$Tpl->add('global_vars:loop', array(
'key' => $key,
'value' => $val,
));
}
}
if (!empty($bench_template)) {
foreach ($bench_template as $val) {
$Tpl->add('template:loop', array(
'template' => $val,
));
}
}
if (!empty($bench_slow_query)) {
foreach ($bench_slow_query as $val) {
$Tpl->add('query:loop', array(
'time' => $val['time'],
'query' => $val['query'],
));
}
}
if (!empty($bench) || !empty($bench_global_vars)) {
file_put_contents('./stdout.log', $Tpl->get());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment