-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLudoDBProfiling.php
More file actions
193 lines (174 loc) · 4.68 KB
/
LudoDBProfiling.php
File metadata and controls
193 lines (174 loc) · 4.68 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
/**
*
* http://localhost/LudoDBProfiling/Person/1/read/profile
* User: Alf Magne Kalleland
* Date: 18.02.13
* Time: 20:48
* @package LudoDB
* @author Alf Magne Kalleland <post@dhtmlgoodies.com>
*/
/**
* Class for profiling requests. This resources requires that you have the xhprof module enabled
* on your Apache web server. It's also easiest to use when you have the mod rewrite module enabled
* Syntax
* LudoDBProfiling/resourceToProfile/arg1/arg2/resourceServiceToProfile/profile
* i.e first argument is LudoDBProfiling and last argument should be profile. The arguments
* in between is the request you want to profile, example
*
* <code>
* http://localhost/LudoDBProfiling/Person/1/read/profile
* </code>
*
* @package LudoDB
*/
class LudoDBProfiling implements LudoDBService
{
/**
* Constructor arguments
* @var array
*/
private $arguments;
/**
* Profiling name
* @var string
*/
private $name;
/**
* Start time
* @var int
*/
private $start;
/**
* Construct new instance
*/
public function __construct()
{
$this->arguments = func_get_args();
}
/**
* Returning "profile" as only valid service method
* @return array
*/
public function getValidServices()
{
return array("profile");
}
/**
* Number of arguments has to be equal or bigger than 2 (resource + service name).
* @param String $service
* @param Array $arguments
* @return bool
*/
public function validateArguments($service, $arguments)
{
return count($arguments) >= 2;
}
/**
* Start profiling.
* @param array $data
* @return array
* @throws LudoDBException
*/
public function profile($data = array())
{
$inDevelop = LudoDBRegistry::get('DEVELOP_MODE');
if (!isset($inDevelop) || !$inDevelop) {
throw new LudoDBException("Profiling can only be executed in develop mode. Use LudoDBRegistry::set('DEVELOP_MODE', true) to activate develop mode");
}
$request = implode("/", $this->arguments);
$name = preg_replace("/[^0-9a-z]/si", "", $request);
$this->start($name);
$handler = new LudoDBRequestHandler();
$result = json_decode($handler->handle(array(
'request' => $request,
'data' => $data
)), true);
if (!$result['success']) {
throw new LudoDBException($result['message']);
}
$url = $this->end();
return array(
"result" => $url,
"request" => $request,
"data" => $data
);
}
/**
* Validate service data
* @param string $service
* @param array $data
* @return bool
*/
public function validateServiceData($service, $data)
{
return true;
}
/**
* No caching
* @param $service
* @return bool
*/
public function shouldCache($service)
{
return false;
}
/**
* Return empty string on successful profiling.
* @param String $service
* @return String
*/
public function getOnSuccessMessageFor($service)
{
return "";
}
/**
* Start profiling.
* @param $name
* @throws LudoDBException
*/
public function start($name)
{
$this->start = microtime(true);
$this->name = $name;
if (!function_exists("xhprof_enable")) {
throw new LudoDBException("xhprof is not enabled on your server");
}
$flags = defined("XHPROF_FLAGS_CPU") ? XHPROF_FLAGS_CPU : 0;
$flags += defined("XHPROF_FLAGS_MEMORY") ? XHPROF_FLAGS_MEMORY : 0;
xhprof_enable($flags);
}
/**
* Return elapsed time.
* @return mixed
*/
public function getTimeUsage()
{
return microtime(true) - $this->start;
}
/**
* End profiling
* @return null|string
*/
public function end()
{
if (function_exists("xhprof_disable")) {
$profilingData = xhprof_disable();
$profilingRuns = new XHProfRuns_Default();
$run_id = $profilingRuns->save_run($profilingData, $this->name);
return "http://" . $_SERVER['HTTP_HOST'] . "/" . $this->getPath() . "/xhprof/xhprof_html/index.php?run=$run_id&source=" . $this->name;
}
return null;
}
/**
* Return path to xhprof
* @return string
*/
private function getPath()
{
$path = explode(DIRECTORY_SEPARATOR, dirname(__FILE__));
$countTokens = count(explode("/", $_SERVER['DOCUMENT_ROOT']));
$path = array_slice($path, $countTokens);
return implode("/", $path);
}
}