当前位置: 首页 > news >正文

PHP和Python脚本的性能监测方案

目录

1. 说明

2. PHP脚本性能监测方案

2.1 安装xdebug

2.2 配置xdebug.ini 

2.3 命令行与VS Code中使用

- 命令行

- VS Code

2.4 QCacheGrind 浏览

3. Python脚本性能监测方案

3.1 命令行

4. 工具

5.参考


1. 说明

      获取我们的脚本程序运行时的指标,对分析与解决性能瓶颈问题是非常重要的一环,以下介绍在PHP与Python下的实践方案:安装性能监测插件 + 产生监测日志 + 用QCacheGrind工具分析
 

2. PHP脚本性能监测方案

2.1 安装xdebug
# wsl2下的ubuntu20 LTS为例apt install php7.4-xdebug

2.2 配置xdebug.ini 

tips: 路径可通过查php -i|grep xdebug.ini获取

zend_extension=xdebug.so# 指定日志输出路径
xdebug.output_dir = "/xdebug_logs"
xdebug.profiler_append = 0xdebug.mode = profile
xdebug.start_with_request = trigger
xdebug.log_level = 7
2.3 命令行与VS Code中使用
- 命令行
php -dxdebug.mode=profile myscript
- VS Code

切记:vscode运行时连接WSL,选择对应的ubuntu目录,不然你的运行环境将不一致

普通的launch.json

{"name": "Debug current script in console","type": "php","request": "launch","program": "${file}","cwd": "${fileDirname}","runtimeArgs": ["-d xdebug.mode=profile"],"externalConsole": false,"port": 9003
}

以TP6为例的launch.json

{"name": "Run PHP Profile","type": "php","request": "launch","cwd": "${workspaceRoot}","program": "${workspaceRoot}/think","args": ["myaction","0"],"runtimeArgs": ["-d xdebug.mode=profile"],"externalConsole": false,"port": 9003
}

截图: 

2.4 QCacheGrind 浏览

 生成的日志保存在 \\wsl.localhost\Ubuntu-20.04\xdebug_logs

 

3. Python脚本性能监测方案

python内部已经提供了cProfile性能监测模块,我们用它就好,只需要把它出来的文件转换一下

3.1 命令行
# 产生日志到./tmp/profile_output.prof
python -m cProfile -o ./tmp/profile_output.prof ./class_test2.py# 转换格式
python -m pyprof2calltree -i profile_output.prof -o callgrind.cprof

4. 工具

工具说明
QCacheGrind

windows工具,能可视化查看callgrind格式的性能数据

下载:https://sourceforge.net/projects/qcachegrindwin/

PHP
XDebug官网: Xdebug - Debugger and Profiler Tool for PHP
Python
cProfile内置标准模块, 性能分析模块
例子: python -m cProfile -o ./tmp/profile_output.prof ./class_test2.py
pyprof2calltree把cprofile产生的性能日志转换为QCacheGrind格式,
例子: python -m pyprof2calltree -i profile_output.prof -o callgrind.cprof

5.参考

- Xdebug: Documentation » Profiling

- The Python Profilers — Python 3.13.0 documentation

- https://gist.github.com/Susensio/efd9422e14556dff4122434c3603aff3

http://www.lryc.cn/news/482597.html

相关文章:

  • C语言实现数据结构之堆
  • 战略共赢 软硬兼备|云途半导体与知从科技达成战略合作
  • python:用 sklearn 构建 K-Means 聚类模型
  • elementUI中2个日期组件实现开始时间、结束时间(禁用日期面板、控制开始时间不能超过结束时间的时分秒)实现方案
  • Oracle 聚集因子factor clustering
  • 【大数据学习 | kafka高级部分】kafka的快速读写
  • 云技术基础
  • 字节序(Byte Order)
  • 融云:社交泛娱乐出海机会尚存,跨境电商异军突起
  • django博客项目实现站内搜索功能
  • 蓝桥杯c++算法学习【1】之枚举与模拟(卡片、回文日期、赢球票、既约分数:::非常典型的比刷例题!!!)
  • Android 延时操作的常用方法
  • AI驱动的轻量级笔记应用Blinko
  • 一文搞懂 UML 类图
  • Zabbix 7 最新版本安装 Rocky Linux 8
  • 使用HTML、CSS和JavaScript创建动态雪人和雪花效果
  • redis bind 127.0.0.1和bind 10.34.56.78的区别
  • 基于点云的 3D 目标检测模型 PointPillars 部署 tensorRT
  • centos查看硬盘资源使用情况命令大全
  • Solon MVC 的 @Mapping 用法说明
  • uni-app表单⑪
  • PyQt5 加载UI界面与资源文件
  • 【MySQL】数据库知识突破:数据类型全解析与详解
  • 使用Golang实现开发中常用的【实例设计模式】
  • 【Java学习】电脑基础操作和编程环境配置
  • AVL树解析
  • 栈和队列(Java)
  • C#设计原则
  • easyfs 简易文件系统
  • 【架构论文-1】面向服务架构(SOA)