MqXhprof.php 2.21 KB
<?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);
}