当前位置:网站首页>php microtime encapsulates the tool class, calculates the running time of the interface (breakpoint)
php microtime encapsulates the tool class, calculates the running time of the interface (breakpoint)
2022-08-03 12:11:00 【listen to the years】
Introduction
There was a problem when we finished developing the relevant business procedures for the section we were responsible for and then published it online.Then, when troubleshooting, it was found that the interface timed out.So we need some way to know the execution time of each work link of this interface.Based on my ideas and experience, this article designs a tool class, through which we can know the running time of each work link.
php itself provides a function microtime, through this function and cooperate with us to break the point.We can know the time-consuming of various places when the program is running.
Sample
This sample is suitable for php-fpm mode or cli mode. If you use swoole-work, please consider the problem of dirty data (swoole-work resident memory, multiple threads share memory)
Encapsulate microtime and design a breakpoint class
Point.php
startTime = microtime(true);//initialize an empty class$this->runTimes = new \StdClass;}public function addPoint(string $pointName){//Statistical breakpoint time$this->runTimes->{$pointName} = microtime(true);}// breakpoint time calculationpublic function calculation(){//Initialize an empty class to count$data = new \StdClass;// breakpoint time calculationif(!empty($this->runTimes)){foreach((array)$this->runTimes as $k=>$v){$data->{$k} = number_format(bcsub($v , $this->startTime,4) * 1000,4) . "milliseconds";}}return $data;}}
About the use of breakpoint classes
public function index14(){$runTime = new Point();/*** @var Test3 $test3*/$test3 = app()->make(Test3::class);//breakpoint -1$runTime->addPoint("test3_run");$userinfo = $test3->userService->get(867076808);//breakpoint-2$runTime->addPoint("user_service_get");$this->info($userinfo->username);//breakpoint-3$runTime->addPoint("print");// print breakpoint execution timedd($runTime->calculation());}
Output results
wsyUMcKjfP{+"test3_run": "9.8000ms"+"user_service_get": "27.5000ms"+"print": "27.9000ms"}
Generally we often use this auxiliary class to detect the time-consuming of
restFule interface
Time-consuming execution of a class method
Time-consuming mysql connection
Time-consuming mysql CURD operation
Time-consuming redis connection
Time-consuming redis query
Time-consuming to read and write files
Time-consuming to execute business programs
According to the execution of the program, we know the execution time of each work unit, and then design the optimization plan.
If you think it's good, give it a follow and praise~
边栏推荐
猜你喜欢
随机推荐
面试突击71:GET 和 POST 有什么区别?
Matlab学习11-图像处理之图像变换
thymeleaf中的日期格式转化
基于SSM和Web实现的农作物生长监控系统
bash if conditional judgment
899. 有序队列 : 最小表示法模板题
R语言ggplot2可视化:使用ggpubr包的ggline函数可视化折线图、设置add参数为mean_se和dotplot可视化不同水平均值的折线图并为折线图添加误差线(se标准误差)和点阵图
第十五章 源代码文件 REST API 简介
R语言ggplot2可视化:使用patchwork包的plot_layout函数将多个可视化图像组合起来,ncol参数指定行的个数、byrow参数指定按照行顺序排布图
JUC(三):锁核心类AQS ing
肝完Alibaba这份面试通关宝典,我成功拿下今年第15个Offer
使用.NET简单实现一个Redis的高性能克隆版(一)
流式编程使用场景
LeetCode-48. 旋转图像
bash case usage
深度学习中数据到底要不要归一化?实测数据来说明!
LeetCode-1161. 最大层内元素和
【一起学Rust】Rust的Hello Rust详细解析
LeetCode-142. 环形链表 II
[深入浅出]三位数排序