Merge branch 'maint/7.0'
[ninja.git] / application / hooks / benchmark_log.php
blobc62032460016672a799b56201c35c376ca57a870
1 <?php
3 /**
4 * Set $config['log_benchmark_to_file'] = '/tmp/ninja_benchmark.log' in
5 * ninja/application/config/custom/config.php
6 * to log benchmarking data.
7 */
8 class benchmark_log {
9 private $filename;
11 /**
12 * @param $filename string
14 function __construct($filename) {
15 $this->filename = $filename;
16 Event::add('system.display', array($this, 'log_to_file'));
19 /**
20 * Collects benchmarking information and prints to file
22 function log_to_file() {
23 $benchmark = Benchmark::get(SYSTEM_BENCHMARK.'_total_execution');
24 $memory = function_exists('memory_get_usage') ? (memory_get_usage() / 1024 / 1024) : 0;
25 $sqltime=0.0;
26 $sqlrows=0;
27 foreach (Database::$benchmarks as $key => $value){
28 $sqltime += $value["time"];
29 $sqlrows += $value["rows"];
32 $output = array(
33 'timestamp' => time(),
34 // we want to be able to pipe to cut -d ' ', hence: no spaces in username
35 'user' => Auth::instance()->get_user() ? str_replace(' ', '_', Auth::instance()->get_user()->username) : '[not_logged_in]',
36 'url' => url::current(true),
37 'execution_time' => $benchmark['time'].'s',
38 'memory_usage' => number_format($memory, 2).'MB',
39 'num_sql' => count(Database::$benchmarks),
40 'sqltime' => $sqltime,
41 'sqlrows' => $sqlrows
45 file_put_contents($this->filename, implode(' ', $output)."\n", FILE_APPEND);
49 if($log_benchmark_to_file = Kohana::config('config.log_benchmark_to_file')) {
50 new benchmark_log($log_benchmark_to_file);