当前位置:网站首页>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~
边栏推荐
猜你喜欢
随机推荐
缓存--伪共享问题
深度学习跟踪DLT (deep learning tracker)
苹果发布 AI 生成模型 GAUDI,文字生成 3D 场景
R语言绘制时间序列的自相关函数图:使用acf函数可视化时间序列数据的自相关系数图
学习软件测试需要掌握哪些知识点呢?
【Verilog】HDLBits题解——Verification: Reading Simulations
特征降维学习笔记(pca和lda)(1)
dataset数据集有哪些_数据集类型
C language advanced article: memory function
技术总监需要会些什么?也太难了!
日常开发写代码原则
LyScript 实现对内存堆栈扫描
字符串本地化和消息字典(二)
YOLOv5训练数据提示No labels found、with_suffix使用、yolov5训练时出现WARNING: Ignoring corrupted image and/or label
数据库系统原理与应用教程(075)—— MySQL 练习题:操作题 151-159(十九):综合练习
Mysql重启后innodb和myisam插入的主键id变化总结
【Verilog】HDLBits题解——Verification: Writing Testbenches
想学自动化测试网课哪个好?过了人告诉你:适合自己的才是最重要
Filebeat 如何保持文件状态?
肝完Alibaba这份面试通关宝典,我成功拿下今年第15个Offer