Python:用pyinstrument做性能分析
导引
在计算密集型计算或一些Web应用中,我们常常需要对代码做性能分析。在Python中,最原始的方法即是使用time包中的time函数(该函数以秒为计时单位):
from time import sleep, time
def func1():
sleep(0.001)
def func2():
sleep(2)
begin1 = time()
func1()
end1 = time()
begin2 = time()
func2()
end2 = time()
print("func1 consume: %f, func2 consume:%f, func3 consume: %f"\
% (end1-begin1, end2-begin2, end2-begin1))
控制台输出如下:
func1 consume: 0.001271, func2 consume:2.000421, func3 consume: 2.001692
pyinstrument基本用法
但是一旦函数多了起来,这种方式显然过于繁杂。类似C语言中的cProfile,在Python中,也有专门的性能分析工具pyinstrument(该库非内置,需要使用conda/pip安装),我们在复杂的项目中可以使用它来代替简陋的time.time()。
首先来看一下基本的使用,它的使用框架如下:
from pyinstrument import Profiler
from time import sleep
def func1():
sleep(0.1)
def func2():
sleep(2)
profiler = Profiler()
profiler.start()
# 这里是你要分析的代码,我们这里分析func1和func2两个函数
func1()
func2()
profiler.stop()
profiler.print()
可以看到,该工具也将其成功分析出了个函数的运行时间,并为我们标红指出了运行2s的func2函数是性能瓶颈:

如果我们进一步调低函数的运行时间:
def func3():
sleep(0.0001)
profiler = Profiler()
profiler.start()
func3()
profiler.stop()
profiler.print()
此时会显示“No samples were recorded”,如下:

这是因为你的代码运行时间小于了1ms,如果你仍然想分析这段代码,你可以选择将间隔值调到比默认的0.001(1ms)小,比如这样:
profiler = Profiler(interval=0.0001)
此时你会发现,func3也能被检测出来了:

此外,如果你要在浏览器中查看分析结果,可以使用profiler.open_in_browser()代替profiler.print()的控制台打印输出:

也可以使用profiler.output_html()将profile以html形式输出。
分析Flask中的web响应性能
我们也可以对Flask应用进行性能分析,具体的用法如下:
from flask import Flask, g, make_response, request
app = Flask(__name__)
@app.before_request
def before_request():
if "profile" in request.args:
g.profiler = Profiler()
g.profiler.start()
@app.after_request
def after_request(response):
if not hasattr(g, "profiler"):
return response
g.profiler.stop()
output_html = g.profiler.output_html()
return make_response(output_html)
这样程序会检测每个request中的?profile参数,如果检测到则会开始分析。在运行了profiler的request结束后,它会生成一个html输出替代掉真实的response并返回。
参考
- [1] https://docs.python.org/3/library/time.html?highlight=time#time.time
- [2] https://pyinstrument.readthedocs.io/en/latest/guide.html
Python:用pyinstrument做性能分析的更多相关文章
- chrome使用Timeline做性能分析
使用Timeline做性能分析 Timeline面板记录和分析了web应用运行时的所有活动情况,这是研究和查找性能问题的最佳途径.###Timeline面板概览 Timeline面板主要有三个部分构成 ...
- Python 优化第一步: 性能分析实践 使用cporfile+gprof2dot可视化
拿来主义: python -m cProfile -o profile.pstats to_profile.py gprof2dot -f pstats profile.pstats |dot -Tp ...
- python面试总结3(性能分析优化,GIl常考题)
python性能分析和优化,GIL常考题 什么是Cpython GIL Cpython解释器的内存管理并不是线程安全的 保护多线程情况下对python对象访问 Cpython使用简单的锁机制避免多个线 ...
- python 数据较大 性能分析
前提:若有一个几百M的文件需要解析,某个函数需要运行很多次(几千次),需要考虑性能问题 性能分析模块:cProfile 使用方法:cProfile.run("func()"),其中 ...
- golang 使用pprof和go-torch做性能分析
软件开发过程中,项目上线并不是终点.上线后,还要对程序的取样分析运行情况,并重构现有的功能,让程序执行更高效更稳写. golang的工具包内自带pprof功能,使找出程序中占内存和CPU较多的部分功能 ...
- 使用xdebug对php做性能分析调优
作为PHP程序员我们或多或少都了解或使用过xdebug.此文章记录安装和配置xdebug,以及如何使用它来分析php程序. 我的机器环境: mac, php 安装 xdebug 推荐使用 pecl 安 ...
- python pstats ,profile 性能分析
#! /usr/bin/env python # encoding=utf8 import pstats import profile def func1(): for i in range(1000 ...
- 使用JProfiler做性能分析过程
供自己记录一下,也分享给大家使用JProfiler的过程(感谢教我使用这个工具的大佬),整个博客比较粗糙,希望对大家有帮助 1.首先安装好JProfiler,打开eclipse,右键你所要分析的项目, ...
- 用cProfile做性能分析【转】
原文地址: https://www.cnblogs.com/kaituorensheng/p/4453953.html
随机推荐
- HashCode补充
public class Demo4 { public static void main(String[] args) { String str1 = "hello"; Strin ...
- ValueStack与ContentMap (ActionContext.getContext().getValueStack().set())
在方法 <action name="zilei" class="dtreeAction" method="zilei"> & ...
- 微信小程序开发提升效率
http://www.ifanr.com/minapp/790017 微信小程序的 API 实现需要兼顾方方面面,所以仍然使用 callback 写法. 众所周知,Callback-Hell(回调地狱 ...
- Docker的资源控制管理
Docker的资源控制管理 1.CPU控制 2.对内存使用进行限制 3.对磁盘I/O配额控制的限制 1.CPU控制: cgroups,是一个非常强大的linux内核工具,他不仅可以限制被namespa ...
- 2022寒假集训day2
day1:学习seach和回溯,初步了解. day2:深度优化搜索 T1 洛谷P157:https://www.luogu.com.cn/problem/P1157 题目描述 排列与组合是常用的数学方 ...
- Winds10 安装JDK8.0教程
首先下载一个jdk,可以通过这个链接下载:https://pan.baidu.com/s/1aP6SdL8UQK_C2GvALLb6Wg也可以去官网下载:https://www.oracle.com/ ...
- Spring Boot数据访问之数据源自动配置
Spring Boot提供自动配置的数据访问,首先体验下,Spring Boot使用2.5.5版本: 1)导入坐标: 2.5.25版本支持8.0.26mysql数据库驱动.spring-boot-st ...
- 『无为则无心』Python面向对象 — 60、魔法属性
目录 1.魔法属性__name__ 2.魔法属性__bases__ 3.魔法属性__mro__ 4.魔法属性__doc__ 5.魔法属性__module__ 和__class__ 6.魔法属性__di ...
- 技术小白的也能独立完成数据分析,这款BI系统你值得拥有
是否有很多小白跟我一样,不会编程代码,又觉得excel操作太繁琐了,一直苦苦不知道要怎么做数据分析.前段时间我使用了一款bi系统,简直太方便了!拖拉拽就能制作分析图表.点击就能应用智能分析功能,如果这 ...
- Centos7使用kubeadm安装1.23.1版本的k8s集群
系统环境 #cat /etc/redhat-release CentOS Linux release 7.6.1810 (Core) #Linux内核一定要大约等于3.10,也就是centos版本要大 ...