-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsrc.php
More file actions
75 lines (65 loc) · 1.81 KB
/
src.php
File metadata and controls
75 lines (65 loc) · 1.81 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
<?php
ini_set('date.timezone','Asia/Tokyo');
class SimpleProfiler
{
public $log = [];
public $fp = null;
public $init_time = null;
public $opens = [];
public $indent = 0;
public function __construct($log_file)
{
$this->fp = fopen($log_file,'a');
$this->init_time = ceil(microtime(true)*1000);
$txt = "\n----START------( ".(date('Y-m-d H:i:s'))." )------\n";
$txt .= "label : S-E間のms / Initからのms \n";
fwrite($this->fp,$txt);
}
public function __destruct()
{
fwrite($this->fp,"----END------( ".(date('Y-m-d H:i:s'))." )------\n\n");
$aggregate = [];
foreach ( $this->log as $l ) {
if ( $l[2] == 0 ) continue;
if ( !isset($aggregate[$l[0]])) {
$aggregate[$l[0]] = [
'count' => 0,
'time' => 0
];
}
$aggregate[$l[0]]['count']++;
$aggregate[$l[0]]['time'] += $l[2];
}
foreach ( $aggregate as $sig => $a ) {
fwrite($this->fp,"{$sig} : {$a['count']}回 / 合計 {$a['time']}ms / 平均 ".($a['time']/$a['count'])."ms\n");
}
}
private function setLog($sig,$time,$fromStart = 0)
{
$fromInit = ceil(microtime(true)*1000) - $this->init_time;
$this->log[] = [$sig,$time,$fromStart,$fromInit,$this->renderIndent()];
$format = ($fromStart==0?'[S]':'[E]').$this->renderIndent().$sig." : ".$fromStart." / ".$fromInit ."\n";
fwrite($this->fp,$format);
}
public function start ($sig)
{
$this->opens[$sig] = $time = ceil(microtime(true)*1000);
$this->indent++;
$this->setLog($sig, $time);
}
public function end ($sig)
{
$fromStart = ceil(microtime(true)*1000) - $this->opens[$sig];
unset($this->opens[$sig]);
$this->setLog($sig,ceil(microtime(true)*1000),$fromStart);
$this->indent--;
}
private function renderIndent()
{
$ind = '';
for ($i = 0; $i < $this->indent; $i++) {
$ind .= ' ';
}
return $ind;
}
}