-
Notifications
You must be signed in to change notification settings - Fork 1
/
Amslib_Benchmark.php
117 lines (92 loc) · 2.61 KB
/
Amslib_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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
class Amslib_Benchmark
{
static protected $start = NULL;
static protected $finish = NULL;
static protected $total = NULL;
static protected $entries = array();
static protected $mode = "record";
static protected $allowedModes = array("record","log");
static public function mode($mode)
{
if(!in_array($mode,self::$allowedModes)){
return false;
}
return self::$mode = $mode;
}
static public function set($title=NULL,$data=NULL)
{
static $last = NULL;
// If no title is set, use the code location which executed the set() method
if($title === NULL) $title = Amslib_Debug::getCodeLocation(3);
$time = microtime(true);
// If is first benchmark point, set the start time to this time
if(self::$start === NULL) self::$start = $time;
// Always set the finish time to the last benchmark point
self::$finish = $time;
// Set the total to the subtraction of the last and first benchmark points
self::$total = self::$finish - self::$start;
// calculate the diff only when there was a previous benchmark point set
$diff = $last === NULL ? 0 : ($time-$last["time"]);
// Set the basic data
$e = array(
"title" => $title,
"time" => $time,
"diff" => $diff
);
// Save the data, store the last entry, add it to the list and return it to the calling method
if($data !== NULL) $e["data"] = $data;
// Retain a copy of the previous execution so you can do the differential easier
$last = $e;
switch(self::$mode){
case "record":{
self::$entries[] = $e;
}break;
case "log":{
// FIXME: refactor against log()
Amslib_Debug::log("title[$title], time[$time], diff[$diff]");
}break;
}
return $e;
}
static public function get($title=NULL,$totals=true)
{
$data = array();
if($totals){
$data["totals"] = self::totals();
}
if($title){
foreach(self::$entries as $e){
if($e["title"] == $title){
$data["entry"] = $e;
break;
}
}
}else{
$data["list"] = self::$entries;
}
if($totals) return $data;
if(isset($data["entry"])) return $data["entry"];
if(isset($data["list"])) return $data["list"];
return false;
}
static public function totals()
{
if(!self::$start) return NULL;
return array(
"start" => self::$start,
"finish" => self::$finish,
"total" => self::$total
);
}
static public function log()
{
foreach(self::get(NULL,false) as $item)
{
Amslib_Debug::log("code_location,{$item["title"]}","time[{$item["time"]}], diff[{$item["diff"]}]");
}
if($totals = self::totals()){
Amslib_Debug::log("start[{$totals["start"]}], finish[{$totals["finish"]}], total[{$totals["total"]}]");
}
}
}