当前位置:网站首页>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~
边栏推荐
猜你喜欢
基于英雄联盟的知识图谱问答系统
GET 和 POST 有什么区别?
《数字经济全景白皮书》金融数字用户篇 重磅发布!
微信为什么使用 SQLite 保存聊天记录?
"Digital Economy Panorama White Paper" Financial Digital User Chapter released!
深度学习跟踪DLT (deep learning tracker)
【云原生 · Kubernetes】部署Kubernetes集群
html网页如何获取后台数据库的数据(html + ajax + php + mysql)
ROS中编译通过但是遇到可执行文件找不到的问题
从器件物理级提升到电路级
随机推荐
ssh 免密登录了解下
nacos应用
html网页如何获取后台数据库的数据(html + ajax + php + mysql)
bash for loop
asdn涨薪技术之apifox+Jenkins如何玩转接口自动化测试
Knowledge Graph Question Answering System Based on League of Legends
-树的高度-
项目概述、推送和存储平台准备
矩阵的计算[通俗易懂]
PC client automation testing practice based on Sikuli GUI image recognition framework
LyScript 实现对内存堆栈扫描
数据库系统原理与应用教程(074)—— MySQL 练习题:操作题 141-150(十八):综合练习
R语言使用zoo包中的rollapply函数以滚动的方式、窗口移动的方式将指定函数应用于时间序列、计算时间序列的滚动标准差(设置每个窗口不重叠)
【一起学Rust 基础篇】Rust基础——变量和数据类型
QGIS绘制演习区域示意图
使用.NET简单实现一个Redis的高性能克隆版(一)
无监督学习KMeans学习笔记和实例
ROS中编译通过但是遇到可执行文件找不到的问题
【倒计时5天】探索音画质量提升背后的秘密,千元大礼等你来拿
第四课 标识符、关键字、变量、变量的分类和作用域、常量