MqXhprof.php
2.21 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
<?php
error_reporting(E_ALL);
ini_set('display_errors',true);
/**
* 读取配置信息
*/
$Config = new Lua();
$Config->include(__DIR__.'/Config.lua');
require_once __DIR__ . '/xhprof.io/xhprof/classes/data.php';
while (true){
echo "Start a new checking\n";
if ($logfiles = glob($Config->performance_dir.'/xhprof*')){
try{
if (empty($pdo)){
throw new \Exception;
}
$pdo->query('show processlist');
}catch(\Exception $e){
$pdo = new PDO("mysql:dbname=xhprof.io;host={$Config->mysql_host};port={$Config->mysql_port};charset=utf8", $Config->mysql_user, $Config->mysql_pass);
$xhprof_data_obj = new \ay\xhprof\Data($pdo);
}
foreach ($logfiles as $logfile){
echo "Getting logfile: {$logfile}\n";
$logs = file($logfile);
echo "Log number: ".count($logs)."\n";
unlink($logfile);
foreach ($logs as $k=>$log){
echo "Writing log #".($k+1)."\n";
if (!is_array($log = json_decode($log,true))){
continue;
}
if (empty($log['data']['context'])){
continue;
}
if (!is_array($xhprof_data = json_decode($log['data']['context'],true))){
continue;
}
if (isset($log['data']['_SERVER']) && is_array($__SERVER = json_decode($log['data']['_SERVER'],true))){
$_SERVER['HTTP_HOST'] = $__SERVER['HTTP_HOST'];
$_SERVER['REQUEST_URI'] = $__SERVER['REQUEST_URI'];
$_SERVER['REQUEST_METHOD'] = $__SERVER['REQUEST_METHOD'];
@$_SERVER['HTTPS'] = $__SERVER['HTTPS'];
}else{
$_SERVER['HTTP_HOST'] = $_SERVER['REQUEST_URI'] = $_SERVER['REQUEST_METHOD'] = '';
}
$request_id = $xhprof_data_obj->save($xhprof_data);
echo "Write log #".($k+1)." result: ".($request_id ? 'true' : 'false')."\n";
}
}
echo "Finish one loop\n";
}else{
echo "Not exists any logfile\n";
sleep(10);
}
usleep(100000);
}