-
Notifications
You must be signed in to change notification settings - Fork 0
/
Benchmark.php
60 lines (48 loc) · 1.59 KB
/
Benchmark.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php defined('DICROOT') or die('Äîñòóï çàïðåù¸í');
/*
* Êëàññ äëÿ èçìåðåíèÿ ïðîèçâîäèòåëüíîñòè
* TODO: ñðåäíåå âðåìÿ âûïîëíåíèÿ, ïàìÿòü ïîòðåáëÿåìàÿ øåëë-ñêðèïòàìè
*/
class Benchmark
{
public static $points = array();
public static function start($name)
{
static $counter = 0;
$token = base_convert($counter++, 10, 32);
Benchmark::$points[$token] = array(
'name' => (string) $name,
'start_time' => microtime(true),
'start_memory' => memory_get_usage(),
'end_time' => FALSE,
'end_memory' => FALSE
);
return $token;
}
public static function stop($token)
{
Benchmark::$points[$token]['end_time'] = microtime(true);
Benchmark::$points[$token]['end_memory'] = memory_get_usage();
$point = Benchmark::$points[$token];
return array(
$point['name'],
$point['end_time'] - $point['start_time'],
$point['end_memory'] - $point['start_memory']
);
}
public static function statistics()
{
$stat = array();
if (Benchmark::$points) {
foreach (Benchmark::$points as $token => $vals) {
if ($vals['end_time'] === FALSE OR $vals['end_memory'] === FALSE) Benchmark::stop($token);
$stat[$vals['name']] = array(
'time' => $vals['end_time'] - $vals['start_time'],
'mem' => $vals['end_memory'] - $vals['start_memory'],
'token' => $token
);
}
}
return $stat;
}
}