This repository has been archived by the owner on Dec 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
pinba.php
457 lines (421 loc) · 13.2 KB
/
pinba.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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
<?php
/**
* A class implementing the pinba extension functionality, in pure php
*
* @see http://pinba.org/wiki/Manual:PHP_extension
* @author G. Giunta
* @copyright (C) eZ Systems AS 2011 - 2013
*/
class pinba
{
protected static $timers = array();
protected static $script_name = null;
protected static $server_name = null;
protected static $hostname = null;
protected static $doc_size = 0;
protected static $start = null;
protected static $shutdown_registered = false;
protected static $message_proto = array(
1 => array("hostname", prtbfr::TYPE_STRING),
2 => array("server_name", prtbfr::TYPE_STRING),
3 => array("script_name", prtbfr::TYPE_STRING),
4 => array("request_count", prtbfr::TYPE_UINT32),
5 => array("document_size", prtbfr::TYPE_UINT32),
6 => array("memory_peak", prtbfr::TYPE_UINT32),
7 => array("request_time", prtbfr::TYPE_UINT32),
8 => array("ru_utime", prtbfr::TYPE_UINT32),
9 => array("ru_stime", prtbfr::TYPE_UINT32),
10 => array("timer_hit_count", prtbfr::TYPE_UINT32, prtbfr::ELEMENT_REPEATED),
11 => array("timer_value", prtbfr::TYPE_UINT32, prtbfr::ELEMENT_REPEATED),
12 => array("timer_tag_count", prtbfr::TYPE_UINT32, prtbfr::ELEMENT_REPEATED),
13 => array("timer_tag_name", prtbfr::TYPE_UINT32, prtbfr::ELEMENT_REPEATED),
14 => array("timer_tag_value", prtbfr::TYPE_UINT32, prtbfr::ELEMENT_REPEATED),
15 => array("dictionary", prtbfr::TYPE_STRING, prtbfr::ELEMENT_REPEATED),
16 => array("status", prtbfr::TYPE_UINT32, prtbfr::ELEMENT_OPTIONAL)
);
/**
Creates and starts new timer.
@param array $tags an array of tags and their values in the form of "tag" => "value". Cannot contain numeric indexes for obvious reasons.
@param array $data optional array with user data, not sent to the server.
@return resource Always returns new timer resource.
*/
static function timer_start($tags, $data=null)
{
$time = microtime(true);
$timer = count(self::$timers);
self::$timers[$timer] = array(
"value" => $time,
"tags" => $tags,
"started" => true,
"data" => $data
);
return $timer;
}
/**
Stops the timer.
@param resource $timer valid timer resource.
@return bool Returns true on success and false on failure (if the timer has already been stopped).
*/
static function timer_stop($timer)
{
$time = microtime(true);
if (isset(self::$timers[$timer]))
{
if (self::$timers[$timer]["started"])
{
if ( function_exists('getrusage'))
{
/// @todo measure resource usage
}
self::$timers[$timer]["started"] = false;
self::$timers[$timer]["value"] = $time - self::$timers[$timer]["value"];
}
}
return false;
}
/**
Deletes the timer.
Available since: 0.0.6
@param resource $timer valid timer resource.
@return bool Returns true on success and false on failure.
*/
static function timer_delete($timer)
{
if (isset(self::$timers[$timer]))
{
unset(self::$timers[$timer]);
return true;
}
return false;
}
/**
Merges $tags array with the timer tags replacing existing elements.
@param resource $timer - valid timer resource
@param array $tags - an array of tags.
@return bool
*/
static function timer_tags_merge($timer, $tags)
{
if (isset(self::$timers[$timer]))
{
self::$timers[$timer]["tags"] = array_merge(self::$timers[$timer]["tags"], $tags);
return true;
}
return false;
}
/**
Replaces timer tags with the passed $tags array.
@param resource $timer - valid timer resource
@param array $tags - an array of tags.
@return bool
*/
static function timer_tags_replace($timer, $tags)
{
if (isset(self::$timers[$timer]))
{
self::$timers[$timer]["tags"] = $tags;
return true;
}
return false;
}
/**
Merges $data array with the timer user data replacing existing elements.
@param resource $timer valid timer resource
@param array $data an array of user data.
@return bool Returns true on success and false on failure.
*/
static function timer_data_merge($timer, $data)
{
if (isset(self::$timers[$timer]))
{
self::$timers[$timer]["data"] = array_merge(self::$timers[$timer]["data"], $data);
return true;
}
return false;
}
/**
Replaces timer user data with the passed $data array.
Use NULL value to reset user data in the timer.
@param resource $timer valid timer resource
@param array $data an array of user data.
@return bool Returns true on success and false on failure.
*/
static function timer_data_replace($timer, $data)
{
if (isset(self::$timers[$timer]))
{
self::$timers[$timer]["data"] = $data;
return true;
}
return false;
}
/**
Returns timer data.
@param resource $timer - valid timer resource.
@return array Output example:
array(4) {
["value"]=>
float(0.0213)
["tags"]=>
array(1) {
["foo"]=>
string(3) "bar"
}
["started"]=>
bool(true)
["data"]=>
NULL
}
@todo what to return if timer is not valid?
*/
static function timer_get_info($timer)
{
$time = microtime(true);
return static::_timer_get_info($timer, $time);
}
protected static function _timer_get_info($timer, $time)
{
if (isset(self::$timers[$timer]))
{
$timer = self::$timers[$timer];
if ( $timer["started"] )
{
$timer["value"] = $time - $timer["value"];
}
/// @todo round the timer value?
return $timer;
}
return array();
}
/**
Stops all running timers.
@return bool
@todo when shall we return false?
*/
static function timers_stop()
{
$time = microtime(true);
foreach (self::$timers as &$timer)
{
if ($timer["started"])
{
$timer["started"] = false;
$timer["value"] = $time - $timer["value"];
}
}
return false;
}
/**
Returns all request data (including timers user data).
@return array Example:
array(9) {
["mem_peak_usage"]=>
int(786432)
["req_time"]=>
float(0.001529)
["ru_utime"]=>
float(0)
["ru_stime"]=>
float(0)
["req_count"]=>
int(1)
["doc_size"]=>
int(0)
["server_name"]=>
string(7) "unknown"
["script_name"]=>
string(1) "-"
["timers"]=>
array(1) {
[0]=>
array(4) {
["value"]=>
float(4.5E-5)
["tags"]=>
array(1) {
["foo"]=>
string(3) "bar"
}
["started"]=>
bool(true)
["data"]=>
NULL
}
}
}
*/
static function get_info()
{
$time = microtime(true);
/// @todo can we get more info, such as resource usage?
$results = array(
"mem_peak_usage" => memory_get_peak_usage(true),
"req_time"=> $time - self::$start,
"ru_utime"=> 0,
"ru_stime"=> 0,
"req_count"=> 0,
"doc_size"=> self::$doc_size,
"server_name"=> (self::$server_name != null ? self::$server_name : 'unknown'),
"script_name"=> (self::$script_name != null ? self::$script_name : 'unknown')
);
foreach(self::$timers as $i => $t)
{
$results['timers'][] = static::_timer_get_info($i, $time);
}
return $results;
}
/**
Set custom script name instead of $_SERVER['SCRIPT_NAME'] used by default.
Useful for those using front controllers, when all requests are served by one PHP script.
@param string $script_name
@return bool
*/
static function script_name_set($script_name)
{
self::$script_name = $script_name;
}
/**
Set custom hostname instead of the result of gethostname() used by default.
@param string $hostname
@return bool
*/
static function hostname_set($hostname)
{
self::$hostname = $hostname;
}
/**
Useful when you need to send request data to the server immediately (for long running scripts).
You can use optional argument script_name to set custom script name.
@param string $script_name
@todo add IPv6 support (see http://pinba.org/wiki/Manual:PHP_extension)
*/
static function flush($script_name=null)
{
if (ini_get('pinba.enabled'))
{
$struct = static::get_packet_info($script_name);
$message = prtbfr::encode($struct, self::$message_proto);
$server = ini_get('pinba.server');
$port = 30002;
if (count($parts = explode(':', $server)) > 1)
{
$port = $server[1];
$server = $server[0];
}
$fp = fsockopen("udp://$server", $port, $errno, $errstr);
if ($fp)
{
fwrite($fp, $message);
fclose($fp);
}
}
}
/**
* Builds the php array structure to be sent to the pinba server
*/
static protected function get_packet_info($script_name=null)
{
$struct = static::get_info();
// massage info into correct format for pinba server
$struct["status"] = 0; /// @todo
$struct["hostname"] = self::$hostname;
if ($script_name != null)
{
$struct["server_name"] = $script_name;
}
foreach(array(
"mem_peak_usage" => "memory_peak",
"req_time" => "request_time",
"req_count" => "request_count",
"doc_size" => "document_size",
"req_time" => "request_time" ) as $old => $new)
{
$struct[$new] = $struct[$old];
}
// merge timers by tags
$tags = array();
foreach($struct["timers"] as $id => $timer)
{
$tag = md5(var_export($timer["tags"], true));
if (isset($tags[$tag]))
{
$struct["timers"][$tags[$tag]]["value"] = $struct["timers"][$tags[$tag]]["value"] + $timer["value"];
$struct["timers"][$tags[$tag]]["count"] = $struct["timers"][$tags[$tag]]["count"] + 1;
unset($struct["timers"][$id]);
}
else
{
$tags[$tag] = $id;
$struct["timers"][$id]["count"] = 1;
}
}
// build tag dictionary and index timer tags
$dict = array();
foreach($struct["timers"] as $id => $timer)
{
foreach($timer['tags'] as $tag => $value)
{
if (($tagid = array_search($tag, $dict)) === false)
{
$tagid = count($dict);
$dict[] = $tag;
}
if (($valueid = array_search($value, $dict)) === false)
{
$valueid = count($dict);
$dict[] = $value;
}
$struct["timers"][$id]['tagids'][$tagid] = $valueid;
}
}
foreach($struct["timers"] as $timer)
{
$struct["timer_hit_count"][] = $timer["count"];
$struct["timer_value"][] = $timer["value"];
$struct["timer_tag_count"][] = count($timer["tags"]);
foreach($timer["tagids"] as $key => $val)
{
$struct["timer_tag_name"][] = $key;
$struct["timer_tag_value"][] = $val;
}
}
foreach($dict as $tag)
{
$struct["dictionary"][] = $tag;
}
return $struct;
}
/**
A function not in the pinba extension api, needed to calculate total req. time
*/
static function init( $time=null )
{
if (self::$start == null || $time != null)
{
if ($time == null)
{
$time = microtime(true);
}
self::$start = $time;
}
if (self::$hostname == null)
{
self::$hostname = gethostname();
}
if (self::$script_name == null && isset($_SERVER['SCRIPT_NAME']))
{
self::$script_name = $_SERVER['SCRIPT_NAME'];
}
if (self::$server_name == null && isset($_SERVER['SERVER_NAME']))
{
self::$server_name = $_SERVER['SERVER_NAME'];
}
if ( !self::$shutdown_registered )
{
self::$shutdown_registered = true;
register_shutdown_function('pinba::flush');
}
}
}
// try to start time measurement as soon as we can
pinba::init();