1
|
|
|
<?php
|
2
|
|
|
namespace Mcknubb\Log;
|
3
|
|
|
/**
|
4
|
|
|
* Tlog
|
5
|
|
|
* Print table for CLog
|
6
|
|
|
*
|
7
|
|
|
*/
|
8
|
|
|
class TLog
|
9
|
|
|
{
|
10
|
|
|
/**
|
11
|
|
|
* Print all timestamp to a table.
|
12
|
|
|
*
|
13
|
|
|
* @return string with a html-table to display all timestamps.
|
14
|
|
|
*
|
15
|
|
|
*/
|
16
|
1 |
|
public function renderTimestampAsTable($timestamps, $memoryPeak, $pageLoadTime) {
|
17
|
1 |
|
$prev = $first = $timestamps[0]['when'];
|
18
|
1 |
|
$last = $timestamps[count($timestamps) - 1]['when'];
|
19
|
1 |
|
if($last === $first) {
|
20
|
1 |
|
$last = microtime(true);
|
21
|
|
|
}
|
22
|
1 |
|
$html = "<div class='log' style='background-color:#fff; color:#000; margin-top:50px; padding:20px;'><table class=table><h2>Timestamps</h2><tr><th>Domain</th><th>Where</th><th>When (sec)</th><th>Duration (sec)</th><th>Percent</th><th>Memory (MB)</th><th>Memory peak (MB)</th><th>Comment</th></tr>";
|
23
|
1 |
|
$right = ' style="text-align: right;"';
|
24
|
1 |
|
$total = array('domain' => array(), 'where' => array());
|
25
|
1 |
|
foreach($timestamps as $val) {
|
26
|
1 |
|
$when = $val['when'] - $first;
|
27
|
1 |
|
$duration = isset($val['duration']) ? round($val['duration'], 3) : null;
|
28
|
1 |
|
$percent = round(($when) / ($last - $first) * 100);
|
29
|
1 |
|
$memory = round($val['memory'] / 1024 / 1024, 2);
|
30
|
1 |
|
$peak = isset($val['memory-peak']) ? round($val['memory-peak'] / 1024 / 1024, 2) : NULL;
|
31
|
1 |
|
$when = round($when, 3);
|
32
|
1 |
|
$html .= "<tr><td>{$val['domain']}</td><td>{$val['where']}</td><td{$right}>{$when}</td><td{$right}>{$duration}</td><td{$right}>{$percent}</td><td{$right}>{$memory}</td><td{$right}>{$peak}</td><td>{$val['comment']}</td></tr>";
|
33
|
1 |
|
$prev = $val['when'];
|
34
|
1 |
|
@$total['domain'][$val['domain']] += $duration;
|
35
|
1 |
|
@$total['where'][$val['where']] += $duration;
|
36
|
|
|
}
|
37
|
1 |
|
$html .= "</table><table class=table><h2>Duration per domain</h2><tr><th>Domain</th><th>Duration</th><th>Percent</th></tr>";
|
38
|
1 |
|
arsort($total['domain']);
|
39
|
1 |
View Code Duplication |
foreach($total['domain'] as $key => $val) {
|
|
|
|
|
40
|
1 |
|
$percent = round($val / ($last - $first) * 100, 1);
|
41
|
1 |
|
$html .= "<tr><td>{$key}</td><td>{$val}</td><td>{$percent}</td></tr>";
|
42
|
|
|
}
|
43
|
1 |
|
$html .= "</table><table class=table><h2>Duration per area</h2><tr><th>Area</th><th>Duration</th><th>Percent</th></tr>";
|
44
|
1 |
|
arsort($total['where']);
|
45
|
1 |
View Code Duplication |
foreach($total['where'] as $key => $val) {
|
|
|
|
|
46
|
1 |
|
$percent = round($val / ($last - $first) * 100, 1);
|
47
|
1 |
|
$html .= "<tr><td>{$key}</td><td>{$val}</td><td>{$percent}</td></tr>";
|
48
|
|
|
}
|
49
|
1 |
|
$html .= "</table>";
|
50
|
1 |
|
$html .= "<p>Peek memory consumption was {$memoryPeak} MB.</p>";
|
51
|
1 |
|
$html .= "<p>Page was loaded in {$pageLoadTime} secs.</p>";
|
52
|
1 |
|
$html .= "</div>";
|
53
|
1 |
|
return $html;
|
54
|
|
|
}
|
55
|
|
|
|
56
|
|
|
|
57
|
|
|
/**
|
58
|
|
|
*
|
59
|
|
|
* @return String with information that no timestamps have been logged.
|
60
|
|
|
*/
|
61
|
1 |
|
public function nothingLogged() {
|
62
|
1 |
|
$html = "<p>No timestamps have been logged. Log is empty.</p>";
|
63
|
1 |
|
return $html;
|
64
|
|
|
}
|
65
|
|
|
|
66
|
|
|
/*
|
67
|
|
|
* Test if class is loaded
|
68
|
|
|
*/
|
69
|
|
|
public function saySomething($word) {
|
70
|
|
|
echo $word;
|
71
|
|
|
}
|
72
|
|
|
}
|
73
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.