-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathbenchmark.class.php
More file actions
103 lines (92 loc) · 4.49 KB
/
Copy pathbenchmark.class.php
File metadata and controls
103 lines (92 loc) · 4.49 KB
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
<?php
//This file cannot be called directly, only included.
if (str_replace(DIRECTORY_SEPARATOR, "/", __FILE__) == $_SERVER['SCRIPT_FILENAME']) {
exit;
}
class EfrontBenchmark
{
public $times;
public $dbtimes;
public $defaultValues = array('start', 'init', 'script', 'smarty', 'end');
public function __construct($time = false) {
if (!$time) {
$time = microtime(true);
}
$this -> times = array('start' => $time);
}
public function set($label, $time = false) {
if (!$time) {
$time = microtime(true);
}
$this -> times[$label] = $time;
}
public function stop() {
$this -> times['end'] = microtime(true);
$this -> dbtimes = array('time' => $GLOBALS['db'] -> databaseTime,
'queries' => $GLOBALS['db'] -> databaseQueries);
}
public function display() {
$GLOBALS['db'] -> queries = eF_multisort($GLOBALS['db'] -> queries, 'times', 'asc');
foreach ($GLOBALS['db'] -> queries as $key=>$value) {
$GLOBALS['db'] -> queries[$key]['sql'] = htmlentities($value['sql']);
if ($value['times'] > 0.05) {
$heavyQueries[] = $value;
}
}
$str =
<div onclick = 'this.style.display=\"none\"' style = 'position:absolute;top:0px;right:0px;background-color:lightblue;border:1px solid black' >
<table>
<tr><th colspan = '100%'>Benchmarking info (click to remove)</th></tr>
<tr><td>Initialization time: </td><td>".round($this -> times['init'] - $this -> times['start'], 5)." sec</td></tr>
<tr><td>Script time: </td><td>".round($this -> times['script'] - $this -> times['init'], 5)." sec</td></tr>
<tr><td>Database time (".$this -> dbtimes['queries']." q): </td><td>".($this -> dbtimes['time'] > 100 ? 0 : round($this -> dbtimes['time'], 5))." sec (<a href = 'javascript:void(0)' onclick = 'eF_js_showDivPopup(event, \"Queries\", 2, \"queries_table\");return false;'>show queries</a>)</td></tr>
<tr><td>Smarty time: </td><td>".round($this -> times['smarty'] - $this -> times['script'], 5)." sec</td></tr>
<tr><td colspan = \"2\" class = \"horizontalSeparator\"></td></tr>
<tr><td>Total execution time: </td><td>".round($this -> times['end'] - $this -> times['start'], 5)." sec</td></tr>
<tr><td>Peak memory usage: </td><td>".round(memory_get_peak_usage(true)/1024)." KB</td></tr>";
if (sizeof($this -> defaultValues) != sizeof($this -> times)) {
$current = 'start';
foreach ($this -> times as $key => $value) {
if (!in_array($key, $this -> defaultValues)) {
$str .= "<tr><td>Time from ".$current." to ".$key.": </td><td>".round($this -> times[$key] - $this -> times[$current], 5)." sec</td></tr>";
$current = $key;
}
}
$str .= "<tr><td>Time from ".$current." to end: </td><td>".round($this -> times['end'] - $this -> times[$current], 5)." sec</td></tr>";
}
$str .=
</table>
<div style = 'display:none;' id = 'queries_table'>
<table style = 'background-color:white;width:1000px' >";
if (isset($heavyQueries)) {
$str .=
<tr><td> </td></tr>
<tr><td class = 'topTitle'>Heavy queries</td></tr>
<tr><td><re>".str_replace(array("\n", " ", "\t"), array("<br>", " ", " "), print_r($heavyQueries, true))."</pre></td></tr>";
}
$str .=
<tr><td> </td></tr>
<tr><td class = 'topTitle'>All queries</td></tr>
<tr><td><re>".str_replace(array("\n", " ", "\t"), array("<br>", " ", " "), print_r($GLOBALS['db'] -> queries, true))."</pre></td></tr>
</table>
</div>
</div>";
$fields = array("url" => htmlentities($_SERVER['REQUEST_URI']),
"init_time" => round($this -> times['init'] - $this -> times['start'], 5),
"script_time" => round($this -> times['script'] - $this -> times['init'], 5),
"database_time" => $this -> dbtimes['time'] > 100 ? 0 : round($this -> dbtimes['time'], 5),
"smarty_time" => round($this -> times['smarty'] - $this -> times['script'], 5),
"total_time" => round($this -> times['end'] - $this -> times['start'], 5),
"memory_usage" => round(memory_get_peak_usage(true)/1024),
"total_queries" => $this -> dbtimes['queries'],
"max_query" => serialize(array_slice($GLOBALS['db'] -> queries, 0, 1)),
"timestamp" => time());
/*
try {
eF_insertTableData("benchmark", $fields);
eF_deleteTableData("benchmark", "timestamp < ".(time() - 3600*24*7)); //Keep a week's data only
} catch (Exception $e) {}
*/
return $str;
}
}