当前位置:网站首页>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~
边栏推荐
- 码率vs.分辨率,哪一个更重要?
- 【Verilog】HDLBits题解——Circuits/Sequential Logic/Latches and Flip-Flops
- 基于Sikuli GUI图像识别框架的PC客户端自动化测试实践
- YOLOv5训练数据提示No labels found、with_suffix使用、yolov5训练时出现WARNING: Ignoring corrupted image and/or label
- opencv学习—VideoCapture 类基础知识「建议收藏」
- 深度学习中数据到底要不要归一化?实测数据来说明!
- Redis发布订阅和数据类型
- 进程内存
- 第3章 搭建短视频App基础架构
- Explain the virtual machine in detail!JD.com produced HotSpot VM source code analysis notes (with complete source code)
猜你喜欢

899. 有序队列

fastposter v2.9.0 程序员必备海报生成器

基于Sikuli GUI图像识别框架的PC客户端自动化测试实践

4500 words sum up, a software test engineer need to master the skill books

OFDM 十六讲 4 -What is a Cyclic Prefix in OFDM

广州番禺:暑期防溺水,安全不放假

基于SSM和Web实现的农作物生长监控系统

TiKV & TiFlash 加速复杂业务查询丨TiFlash 应用实践

分享一款实用的太阳能充电电路(室内光照可用)

Matlab学习11-图像处理之图像变换
随机推荐
零信任架构分析【扬帆】
利用ChangeStream实现Amazon DocumentDB表级别容灾复制
微信为什么使用 SQLite 保存聊天记录?
3年软件测试经验,不懂自动化基础...不知道我这种测试人员是不是要被淘汰了?
fastposter v2.9.0 程序员必备海报生成器
JUC(三):锁核心类AQS ing
解决oracle安装在linux中jdk的冲突
flink流批一体有啥条件,数据源是从mysql批量分片读取,为啥设置成批量模式就不行
用C语言解决A+B问题,A-B问题,A*B问题
数据库系统原理与应用教程(073)—— MySQL 练习题:操作题 131-140(十七):综合练习
详解虚拟机!京东大佬出品HotSpot VM源码剖析笔记(附完整源码)
别再用if-else了,分享一下我使用“策略模式”的项目经验...
项目概述、推送和存储平台准备
FE主导打造一个运营活动平台
从零开始Blazor Server(6)--基于策略的权限验证
距LiveVideoStackCon 2022 上海站开幕还有3天!
thymeleaf中的日期格式转化
零拷贝、MMAP、堆外内存,傻傻搞不明白...
Mysql重启后innodb和myisam插入的主键id变化总结
Redis发布订阅和数据类型