-
Notifications
You must be signed in to change notification settings - Fork 0
/
speed.php
executable file
·150 lines (135 loc) · 3.42 KB
/
speed.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
namespace Yii2\lib;
/**
* 用于程序消费记录
* @author <github.com/cunwang>
* @link https://github.com/cunwang/yii2-speed
* @date 2018/03
* @since 1.1 简化代码
*/
class speed
{
/**
* @property json
{
"title":"Speed-xxx",
"flag":"",
"alias":[],
"option":[],
"time":[],
"error":[],
"optionSpeed":"",
"allSpeed":"",
"average":[]
}
*/
private $speedData;
/**
* initialize
* @param $name string default NULL
*/
public function __construct($name = NULL)
{
$this->speedData = [
'title' => (empty($name) ? 'Speed-' . date('Ymdhis') : $name),
'flag' => '',
'error' => [],
];
$this->setLoop('Start');
}
protected function getMt()
{
list($usec, $sec) = explode(" ", microtime());
return ((float) $usec + (float) $sec);
}
protected function getDiff($a, $b)
{
return sprintf("%.5f", (float) $b - (float) $a);
}
protected function add($name)
{
$this->speedData['alias'][] = (empty($name) ? uniqid('Alias-') : $name);
$this->speedData['option'][] = uniqid('Option-');
$this->speedData['time'][] = $this->getMt();
}
protected function getAllSpeed()
{
$data = [];
$tmp = $this->speedData['time'];
if (! empty($tmp)) {
$data['all'] = $this->getDiff($tmp[0], end($tmp));
$data['optionSpeed'] = [];
for ($i = 1; $i < count($tmp); $i++) {
$curr = $tmp[$i];
$pre = $tmp[$i -1];
$data['optionSpeed'][] = $this->getDiff($pre, $curr);
}
}
unset($tmp, $curr, $pre);
return $data;
}
public function __get($name)
{
if (empty($name)) return;
if (isset($this->speedData[$name])) {
return $this->speedData[$name];
} else {
throw new \Exception('Call unknow property: '. $name);
}
}
public function __set($name, $value)
{
if (empty($name)) return;
if (isset($this->speedData[$name])) {
$this->speedData[$name] = $value;
}
}
public function __call($name, $arguments)
{
$f = 'get' . ucfirst(strtolower($name));
if (method_exists($this, $f)) {
return call_user_func_array ([$this, $f], is_array($arguments) ? $arguments : [$arguments]);
} else {
throw new \Exception('Calling unknown method: ' . get_class($this) . "::$f()", 404);
}
}
public function s($name, $args = [])
{
$f = NULL;
$f2 = 'set' . ucfirst(strtolower($name));
do {
if (method_exists($this, $f2)) {
$f = $f2;
call_user_func_array([$this, $f], [$args]);
} else {
throw new \Exception('Calling unknown method: ' . get_class($this) . "::$f2()", 404);
}
} while ($f = NULL);
unset($f, $f2);
}
protected function setError($msg)
{
if (! empty($msg) && isset($this->speedData['error'])) {
$this->speedData['error'][] = $msg;
}
}
public function setLoop($name = NULL)
{
$this->add($name);
}
public function end()
{
$this->setLoop('End');
$allSpeed = $this->getAllSpeed();
$average = (float) ($allSpeed['all'] / count($this->speedData['option']));
$options = $allSpeed['optionSpeed'];
$this->speedData['optionSpeed'] = (count($options) > 1) ? $options : (string) $options[0];
$this->speedData['allSpeed'] = (string) $allSpeed['all'];
$this->speedData['average'] = (string) $average;
unset($allSpeed, $average, $options);
}
protected function getData($t = 'str')
{
return ($t == 'arr') ? $this->speedData : json_encode($this->speedData);
}
}