Between records program execution, the interface call time to a log file or database

1, to obtain millisecond

private function getMillisecond() {
    list($s1, $s2) = explode(' ', microtime());
    return (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000);
}

2, access to the interface time prior to the call, after call

$t1 = $this->getMillisecond(true);
$result = json_decode(Http::doPostJson($url,$data),true);
$t2 = $this->getMillisecond(true);
$this->miclog($t1,$t2,__METHOD__,$result,$data['funid']);

3, logging

private function miclog($t1,$t2,$name,$result = [],$funid = ''){
    $lasttime = ($t2 - $t1).'ms';
    $content = date('Y-m-d H:i:s').'   '.$lasttime.'   '.$name.'   '.$funid;
    file_put_contents('/tmp/ssyv5/jg_micro.log',$content.PHP_EOL,FILE_APPEND);


    $length_time = $t2 - $t1;
    if (200 < $length_time){
        file_put_contents('/tmp/ssyv5/jg_micro_200.log',$content.PHP_EOL,FILE_APPEND);
    }elseif (100 < $length_time){
        file_put_contents('/tmp/ssyv5/jg_micro_100.log',$content.PHP_EOL,FILE_APPEND);
    }

    if (!$result){
        file_put_contents('/tmp/ssyv5/jg_micro_die.log',$content.PHP_EOL,FILE_APPEND);
    }

    if ($length_time >= 5000){
        file_put_contents('/tmp/ssyv5/jg_micro_overtime.log',$content.PHP_EOL,FILE_APPEND);
        $this->addEditJgLog($funid,$length_time);
    }
}

4, database records

protected function addEditJgLog($funid,$length_time) {
    // 记录到数据库
    $jg_slow_log = M('jg_slow_log');
    $where['funid'] = $funid;
    $date = date('Y-m-d');
    $where['date'] = $date;
    $exist = $jg_slow_log->where($where)->find();
    if (!$exist) { // 添加
        $add_data = [
            'funid' => $funid,
            'date'  => $date,
            'times' => 1,
            'min_micro'  => $length_time,
            'max_micro'  => $length_time,
            'update_time' => date('Y-m-d H:i:s'),
            'create_time' => date('Y-m-d H:i:s'),
        ];
        $jg_slow_log->add($add_data);
    } else { // 修改
        $max_micro = (int)$exist['max_micro'];
        $min_micro = (int)$exist['min_micro'];
        $times = ++$exist['times']; // 先加1,再赋值给变量
        if ((int)$length_time > $max_micro) {
            $max_micro = $length_time;
        }
        if ((int)$length_time < $min_micro) {
            $min_micro = $length_time;
        }
        $edit_data = [
            'min_micro'  => $min_micro,
            'max_micro'  => $max_micro,
            'times'      => $times,
            'update_time' => date('Y-m-d H:i:s'),
        ];
        $jg_slow_log->where(['id'=>$exist['id']])->save($edit_data);
    }
}

Guess you like

Origin www.cnblogs.com/jiqing9006/p/10949137.html