Archived
1
0
Fork 0
This repository has been archived on 2026-05-10. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
fiktivkod/library/Fiktiv/Performance.php
H Hautakoski c0189183f9 cleanup
2010-09-19 16:33:23 +02:00

70 lines
1.5 KiB
PHP

<?php
class Fiktiv_Performance
{
/**
* @var float
*/
protected static $_start = null;
protected static $_result = null;
/**
* @return void
*/
public static function begin()
{
self::$_start = microtime(true);
}
/**
* @return float
*/
public static function end()
{
self::$_result = microtime(true) - self::$_start;
return self::result();
}
public static function result()
{
return self::$_result;
}
public static function benchmark($function, $params = array(), $iterations = 1000, $times = 10)
{
$result = array(
'avg' => null,
'total' => null,
'max' => null,
'min' => null,
'results' => array()
);
for($x=0; $x < $times; $x++) {
self::begin();
for($y=0; $y < $iterations; $y++) {
call_user_func_array($function, $params);
}
$result['results'][] = self::end();
$result['total'] += self::result();
if ($result['min'] > self::result() || empty($result['min'])) {
$result['min'] = self::result();
}
if ($result['max'] < self::result()) {
$result['max'] = self::result();
}
}
$result['avg'] = array_sum($result['results']) / count($result['results']);
return $result;
}
}