<?php
/**
* Copyright (C) 2010 Kai Dorschner
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @author Kai Dorschner <the-hide@address.com>
* @copyright Copyright 2010, Kai Dorschner
* @license http://www.gnu.org/licenses/gpl.html GPLv3
* @package mocovi
* @subpackage helpers
*/
/**
* A little stopwatch.
*
* You can utilise this class and it's methods as an ordinary stopwatch.
* Start, break, stop and show results. Furthermore you can comment all your
* interactions.
* It is able to stop in milliseconds. I thought it is usefull to look how long
* several programs need to be executed.
* For it is static, you can use it everywhere since it's included
*
* @package mocovi
* @subpackage helpers
*/
class Timer
{
/**
* Timestamp of first time {@link start()} was hit.
*
* @var integer
* @access private
*/
private static $begin;
/**
* Enables or disables dumping of the time values.
*/
public static $dump = false;
/**
* Flag if file has been dumped.
*/
private static $dumped = false; // Do not change
/**
* Textfile where the values should be saved in.
*/
public static $dumpFile = 'parsetimes.txt';
/**
* Textfile where the values should be saved in.
*/
public static $currentPage = '';
/**
* Timestamps of stopping the count by hitting {@link stop()}.
*
* @var array
* @access private
*/
private static $end = array();
/**
* Comments for each {@link stop()} are saves here.
*
* The indexes are redundant with those in {@link $end}.
*
* @access private
*/
private static $comments = array();
protected function __construct()
{
throw new Exception(get_class(self).' cannot be instantiated. It\'s a static class.');
}
/**
* Starts or resets the stopwatch.
*
* @access public
*/
public static function start()
{
self::$begin = time() + microtime(true);
}
/**
* Stops respectivly breaks the stopwatch.
*
* @param string $comment Comments can be written for each break
* @access public
*/
public static function stop($comment = '')
{
if(!self::$begin) self::start();
$end = time() + microtime(true);
$key = self::arrayInsert(self::$end, $end);
if(strlen($comment) > 0) self::$comments[$key] = $comment;
}
/**
* Returns an array of all corresponding comments in ascending order.
*
* @return array All corresponding comments in one array.
* @access public
*/
public static function getComments()
{
return (array)self::$comments;
}
public static function getDuration($roundvalue = 4)
{
$times = self::getValues($roundvalue);
return $times[count($times) - 1];
}
public static function getDumpedDuration($roundvalue = 4)
{
if($handle = fopen(self::$dumpFile, 'r'))
{
$values = explode("\n", fread($handle, filesize(self::$dumpFile)));
fclose($handle);
}
return array_sum($values) / count($values);
}
/**
* Returns an array of all stopwatch values in ascending order.
*
* @return array All stopwatch values in one array.
* @param integer $roundvalue Defines how precise the rounding is realized.
* @access public
*/
public static function getValues($roundvalue = 4)
{
$timesarray = array();
if(!self::$end) self::stop();
foreach(self::$end as $endtime)
$timesarray[] = round($endtime - self::$begin, $roundvalue);
if(self::$dump) self::dumpTime();
return $timesarray;
}
private static function dumpTime()
{
if(self::$dumped == false)
{
self::$dumped = true;
if($handle = fopen(self::$dumpFile, 'a'))
{
fwrite($handle, self::$currentPage.' '.self::getDuration().' '.date('c')."\n");
fclose($handle);
}
}
}
/**
* Returns the formated stopwatch values as string.
*
* @return string The formated time values.
* @param integer $roundvalue Defines how precise the rounding is realized.
* @access public
*/
public static function getValuesFormated($roundvalue = 4)
{
$return = "Duration:\n";
$i = 0;
foreach(self::getValues($roundvalue) as $value)
{
if(!isset($cache))
$cache = $value;
$return .= $value.' sek'.(isset(self::$comments[$i]) ? ' "'.self::$comments[$i++].'"' : '').(($value - $cache) > 0 ? ' (+'.round($value - $cache, $roundvalue).')' : '')."\n";
$cache = $value;
}
$return .= "==========================\n";
return $return .= $value.' sek';
}
/**
* Inserts a value into an array.
*
* This function has got the same behaviour like the native array_push();
* function of PHP, but this function here returns the key where the value
* was inserted.
*
* @return integer The key where the last value was inserted.
* @param array &$array The concerning array.
* @param mixed $value The value which is inserted.
* @access protected
*/
protected static function arrayInsert(Array &$array, $value)
{
$array[$key = count($array)] = $value;
return $key;
}
}