Xhprof.php
3.16 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
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
<?php
/**
* Created by PhpStorm.
* User: Zip
* Date: 2/22/15
* Time: 11:42 AM
*/
namespace Plugin;
use Yaf\Request_Abstract;
use Yaf\Response_Abstract;
use Yaf\Plugin_abstract;
use Hood\Debug\XHProf as hoodXHProf;
class Xhprof extends Plugin_abstract
{
private $beginTime = 0;
private $requestSlowTimeout = 500;
private $outputDir = null;
public function __construct($requestSlowTimeout = 500)
{
$this->requestSlowTimeout = $requestSlowTimeout;
}
/**
* 设置分析数据路径
* @param null $outputDir
* @return $this
*/
public function setOutputDir($outputDir = null)
{
$this->outputDir = $outputDir;
return $this;
}
/**
* (Yaf >= 2.2.9)
* 在路由之前触发
*
* @param Request_Abstract $request 当前请求对象
* @param Response_Abstract $response 当前响应对象
*
* @return mixed
*/
public function routerStartup(Request_Abstract $request, Response_Abstract $response)
{
$this->beginTime = microtime(true);
xhprof_enable();
}
/**
* (Yaf >= 2.2.9)
* 路由结束之后触发
*
* @param Request_Abstract $request 当前请求对象
* @param Response_Abstract $response 当前响应对象
*
* @return mixed
*/
public function routerShutdown(Request_Abstract $request, Response_Abstract $response)
{
}
/**
* (Yaf >= 2.2.9)
* 分发循环开始之前被触发
*
* @param Request_Abstract $request 当前请求对象
* @param Response_Abstract $response 当前响应对象
*
* @return mixed
*/
public function dispatchLoopStartup(Request_Abstract $request, Response_Abstract $response)
{
}
/**
* (Yaf >= 2.2.9)
* 分发之前触发
*
* @param Request_Abstract $request 当前请求对象
* @param Response_Abstract $response 当前响应对象
*
* @return mixed
*/
public function preDispatch(Request_Abstract $request, Response_Abstract $response)
{
}
/**
* (Yaf >= 2.2.9)
* 分发结束之后触发
*
* @param Request_Abstract $request 当前请求对象
* @param Response_Abstract $response 当前响应对象
*
* @return mixed
*/
public function postDispatch(Request_Abstract $request, Response_Abstract $response)
{
}
/**
* (Yaf >= 2.2.9)
* dispatchLoopShutdown
*
* @param Request_Abstract $request 当前请求对象
* @param Response_Abstract $response 当前响应对象
*
* @return mixed
*/
public function dispatchLoopShutdown(Request_Abstract $request, Response_Abstract $response)
{
$useTime = round((microtime(true) - $this->beginTime) * 1000);
if ($useTime > $this->requestSlowTimeout) {
$xhprofData = xhprof_disable();
$dir = $this->outputDir . $request->getRequestUri();
$xhprofRuns = new hoodXHProf\Runs($dir);
$runID = $xhprofRuns->save_run($xhprofData, "service");
}
//echo "<p><a href='http://x/index.php?run=$run_id&source=xhprof_foo' target='_blank'>Xhprof</a></p>";
}
}