mirror of
https://github.com/e107inc/e107.git
synced 2025-08-04 21:57:51 +02:00
CLOSED issue EONE-17: benchmark handler
This commit is contained in:
115
e107_handlers/benchmark.php
Normal file
115
e107_handlers/benchmark.php
Normal file
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
/*
|
||||
* e107 website system
|
||||
*
|
||||
* Copyright (C) 2008-2010 e107 Inc (e107.org)
|
||||
* Released under the terms and conditions of the
|
||||
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt)
|
||||
*
|
||||
* Benchmark handler
|
||||
*
|
||||
* $URL$
|
||||
* $Id$
|
||||
*/
|
||||
|
||||
/**
|
||||
* @package e107
|
||||
* @subpackage shortcodes|admin|user|e107_handler|bbcodes
|
||||
* @version $Id$
|
||||
*
|
||||
* Simple, quick and efective way of testing performance of parts of your code
|
||||
* Example:
|
||||
* <code> <?php
|
||||
* require_once e_HANDLER.'benchmark.php';
|
||||
*
|
||||
* $bench = new benchmark();
|
||||
* $bench->start();
|
||||
*
|
||||
* // Do something, e.g. loop 1000000 times
|
||||
*
|
||||
* // stop timer and check your e_LOG folder
|
||||
* $bench->end()->$bench->('myevent');
|
||||
* //OR print out the result (don't forget to stop the timer if used without the above line!
|
||||
* $bench->printResult();
|
||||
* </code>
|
||||
*/
|
||||
class benchmark
|
||||
{
|
||||
protected $time_start;
|
||||
protected $time_end;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
function __construct()
|
||||
{
|
||||
$this->time_end = $this->time_start = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Start timer
|
||||
* @return benchmark
|
||||
*/
|
||||
public function start()
|
||||
{
|
||||
$this->time_start = microtime(true);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop timer
|
||||
* @return benchmark
|
||||
*/
|
||||
public function end()
|
||||
{
|
||||
$this->time_end = microtime(true);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate result
|
||||
* @return integer
|
||||
*/
|
||||
public function result()
|
||||
{
|
||||
return ($this->time_end - $this->time_start);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write result to a file in system log
|
||||
* @param string $id identifier of the current benchmark event e.g. 'thumbnail.create'
|
||||
* @param string $heading additional data to be shown in the log (header) e.g. '[Some Event]'
|
||||
* @param boolean $append overwrite or append to the log file
|
||||
* @return benchmark
|
||||
*/
|
||||
public function logResult($id, $heading = '', $append = true)
|
||||
{
|
||||
file_put_contents(e_LOG.'Benchmark_'.$id.'.log', $this->formatData($heading), ($append ? FILE_APPEND : 0));
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send result to the stdout
|
||||
*
|
||||
* @param string $heading
|
||||
* @return string
|
||||
*/
|
||||
public function printResult($heading = '')
|
||||
{
|
||||
print('<pre>'.$this->formatData($heading).'</pre>');
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Format data for loging/printing
|
||||
*
|
||||
* @param string $heading
|
||||
* @return string
|
||||
*/
|
||||
function formatData($heading)
|
||||
{
|
||||
$data = "------------- Log Start -------------\n".date('r')." ".$heading."\n";
|
||||
$data .= "Result: ".$this->result()." sec\n------------- Log End -------------\n\n";
|
||||
return $data;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user