API接口响应速度追踪类

**

前言

**
API接口响应慢?
SLA一直提不上去?
其实这是后端程序员想进阶必须要跨过去的坎:就是把它优化掉。
那么这其中到底有没有套路呢?答案是:有的。

本文将介绍目前正在用并且十分“无脑”有效的这个套路。

**

正文

**
埋点追踪分析,找出真凶

首先呢,第一部肯定是在关键函数(有db、文件、复杂计算等操作)的前后,进行时间的记录。

这里分享一个前文跟踪的类Trace.php

<?php
class Trace{
    var $startTime = [];
    var $endTime = [];

    var $single_mode= false;
    var $trace_mode = false;

    function getTimestamp(){
        return microtime(true);
    }
    function start($type, $time=0){
        if(empty($this->trace_mode)) return 0;

        if(!$time) {
            $time = $this->getTimestamp();
        }

        if($this->single_mode) {
            $this->startTime[$type]=$time;
        } else {
            $this->startTime[$type][]=$time;
        }
    }
    function stop($type, $time=0){
        if(empty($this->trace_mode)) return 0;

        if(!$time) {
            $time = $this->getTimestamp();
        }

        if($this->single_mode) {
            $this->endTime[$type]=$time;
        } else {
            $this->endTime[$type][]=$time;
        }
    }
    function time($type) {
        if(empty($this->trace_mode)) return 0;

        if(!isset($this->endTime[$type])) return 0;

        if(is_array($this->endTime[$type])) {
            return array_sum($this->endTime[$type]) - array_sum($this->startTime[$type]);
        } else  {
            return $this->endTime[$type]-$this->startTime[$type];
        }
    }
    function log($type){
        if(empty($this->trace_mode)) return 0;

        $log['type'] = $type;
        $log['time'] = $this->time($type);;
        // 每个框架不同的记日志方式,可切换
        writeLog($log, 'trace_log');
    }


    function logAll($prefix = '', $ext = array()){
        if(empty($this->trace_mode)) return 0;

        if(!empty($ext)) {
            $log = $ext;
            $log['elapse_time'] = $this->output(false);
        } else {
            $log = $this->output(false);
        }

        if(!empty($log)) {
            $prefix = strtolower(preg_replace('/((?<=[a-z])(?=[A-Z]))/', '_', $prefix));
            // 每个框架不同的记日志方式,可切换
            writeLog($log, 'trace_' . $prefix . '_log');
        }
    }

    function output($with_endtime = true){
        $output = array();
        if(!empty($this->startTime)) {
            foreach($this->startTime as $_type => $_time) {
                $exec_time = $this->time($_type);
                if($exec_time) {
                    $output[$_type] = $exec_time;
                    if($with_endtime) {
                        if(is_array($this->endTime[$_type])) {
                            $output[$_type] .= '|' . max($this->endTime[$_type]);
                        } else {
                            $output[$_type] .= '|' . $this->endTime[$_type];
                        }
                    }
                }
            }
        }
        return $output;
    }

    function reset($type = null){
        if(empty($type)) {
            $this->startTime = null;
            $this->endTime = null;
        } else {
            unset($this->startTime[$type]);
            unset($this->endTime[$type]);
        }

        return $this;
    }
    function enableTrace($exit = true){
        $this->trace_mode = $exit;
    }
}
?>

怎么使用呢?

$Trace = loadClass('Trace');
$Trace->enableTrace(true);
$Trace->start('total');

$Trace->start('func1');
$this->run_slow_function1();
$Trace->stop('func1');

$Trace->start('func2');
$this->run_slow_function2();
$Trace->stop('func2');


$Trace->stop('total');
$Trace->logAll('api_log');
$Trace->reset();

此时去找trace_api_log_log就可以找到每一步跑的时间。根据实际可以一眼看出是哪一步跑慢了。那么这一步就是主要优化的方向了。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值