关于ide:效率神器快速定位运行最慢的代码

6次阅读

共计 4853 个字符,预计需要花费 13 分钟才能阅读完成。

起源:Python 七号

天下文治,唯快不破。

编程也不例外,你的代码跑的快,你能疾速找出代码慢的起因,你的码功就高。

明天分享一个超级实用的 Python 性能剖析工具 pyinstrument,能够疾速找到代码运行最慢的局部,帮忙进步代码的性能。反对 Python 3.7+ 且可能剖析异步代码,仅需一条命令即可显示具体代码的耗时。常常写 Python 的小伙伴肯定要用一下。

https://github.com/joerick/py…

装置

pip install pyinstrument

简略的应用

在程序的开始,启动 pyinstrument 的 Profiler,完结时敞开 Profiler 并打印剖析后果如下:

from pyinstrument import Profiler

profiler = Profiler()
profiler.start()

# 这里是你要剖析的代码

profiler.stop()

profiler.print()

比方这段代码 123.py,咱们能够分明的看到是列表推导式比较慢:

from pyinstrument import Profiler

profiler = Profiler()
profiler.start()

# 这里是你要剖析的代码
a = [i for i in range(100000)]
b = (i for i in range(100000))

profiler.stop()
profiler.print()

上述剖析须要批改源代码,如果你应用命令行工具,就不须要批改源代码,只须要执行 pyinstrument xxxx.py 即可:

比方有这样一段排序的程序 c_sort.py:

import sys
import time

import numpy as np

arr = np.random.randint(0, 10, 10)

def slow_key(el):
    time.sleep(0.01)
    return el 

arr = list(arr)

for i in range(10):
    arr.sort(key=slow_key)

print(arr)

这段代码外面成心放了一句 time.sleep(0.01) 来提早性能,看看 pyinstrument 是否辨认,命令行执行 pyinstrument c_sort.py:

从后果来看,程序运行了 1.313 秒,而 sleep 就运行了 1.219 秒,很显著是瓶颈,当初咱们把它删除,再看看后果:

删除之后,性能最慢的就是 numpy 模块的初始化代码 __init__.py 了,不过这些代码不是本人写的,而且并不是特地慢,就不须要去关怀了。

剖析 Flask 代码

Web 利用也能够应用这个来找出性能瓶颈,比方 flask,只须要在申请之前记录时间,在申请之后统计工夫,只须要在 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)

如果有这样一个 API:

@app.route("/dosomething")
def do_something():
    import requests
    requests.get("http://google.com")
    return "Google says hello!"

为了测试这个 API 的瓶颈,咱们能够在 url 上加一个参数 profile 就能够:http://127.0.0.1:5000/dosomething?profile,哪一行代码执行比较慢,后果清晰可见:

剖析 Django 代码

剖析 Django 代码也非常简单,只须要在 Django 的配置文件的 MIDDLEWARE 中增加

    "pyinstrument.middleware.ProfilerMiddleware",

而后就能够在 url 上加一个参数 profile 就能够:

如果你不心愿所有人都能看到,只心愿管理员能够看到,settings.py 能够增加这样的代码:

def custom_show_pyinstrument(request):
    return request.user.is_superuser

PYINSTRUMENT_SHOW_CALLBACK = "%s.custom_show_pyinstrument" % __name__

如果不想通过 url 前面加参数的形式查看性能剖析,能够在 settings.py 文件中增加:

PYINSTRUMENT_PROFILE_DIR = 'profiles'

这样,每次拜访一次 Django 接口,就会将剖析后果以 html 文件模式保留在 我的项目目录下的 profiles 文件夹中。

剖析异步代码

简略的异步代码剖析:

async_example_simple.py:

import asyncio

from pyinstrument import Profiler

async def main():
    p = Profiler()
    with p:
        print("Hello ...")
        await asyncio.sleep(1)
        print("... World!")
    p.print()

asyncio.run(main())

简单一些的异步代码剖析:

import asyncio
import time

import pyinstrument

def do_nothing():
    pass

def busy_wait(duration):
    end_time = time.time() + duration

    while time.time() < end_time:
        do_nothing()

async def say(what, when, profile=False):
    if profile:
        p = pyinstrument.Profiler()
        p.start()

    busy_wait(0.1)
    sleep_start = time.time()
    await asyncio.sleep(when)
    print(f"slept for {time.time() - sleep_start:.3f} seconds")
    busy_wait(0.1)

    print(what)
    if profile:
        p.stop()
        p.print(show_all=True)

loop = asyncio.get_event_loop()

loop.create_task(say("first hello", 2, profile=True))
loop.create_task(say("second hello", 1, profile=True))
loop.create_task(say("third hello", 3, profile=True))

loop.run_forever()
loop.close()

工作原理

Pyinstrument 每 1ms 中断一次程序,并在该点记录整个堆栈。它应用 C 扩展名和 PyEval_SetProfile 来做到这一点,但只每 1 毫秒读取一次读数。你可能感觉报告的样本数量有点少,但别放心,它不会升高准确性。默认距离 1ms 是记录堆栈帧的上限,但如果在单个函数调用中破费了很长时间,则会在该调用完结时进行记录。如此无效地将这些样本“打包”并在最初记录。

Pyinstrument 是一个统计分析器,并不跟踪,它不会跟踪您的程序进行的每个函数调用。相同,它每 1 毫秒记录一次调用堆栈。与其余分析器相比,统计分析器的开销比跟踪分析器低得多。

比如说,我想弄清楚为什么 Django 中的 Web 申请很慢。如果我应用 cProfile,我可能会失去这个:

151940 function calls (147672 primitive calls) in 1.696 seconds

   Ordered by: cumulative time

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    1.696    1.696 profile:0(<code object <module> at 0x1053d6a30, file "./manage.py", line 2>)
        1    0.001    0.001    1.693    1.693 manage.py:2(<module>)
        1    0.000    0.000    1.586    1.586 __init__.py:394(execute_from_command_line)
        1    0.000    0.000    1.586    1.586 __init__.py:350(execute)
        1    0.000    0.000    1.142    1.142 __init__.py:254(fetch_command)
       43    0.013    0.000    1.124    0.026 __init__.py:1(<module>)
      388    0.008    0.000    1.062    0.003 re.py:226(_compile)
      158    0.005    0.000    1.048    0.007 sre_compile.py:496(compile)
        1    0.001    0.001    1.042    1.042 __init__.py:78(get_commands)
      153    0.001    0.000    1.036    0.007 re.py:188(compile)
  106/102    0.001    0.000    1.030    0.010 __init__.py:52(__getattr__)
        1    0.000    0.000    1.029    1.029 __init__.py:31(_setup)
        1    0.000    0.000    1.021    1.021 __init__.py:57(_configure_logging)
        2    0.002    0.001    1.011    0.505 log.py:1(<module>)

看完是不是还是一脸懵逼,通常很难了解您本人的代码如何与这些跟踪相关联。Pyinstrument 记录整个堆栈,因而跟踪低廉的调用要容易得多。它还默认暗藏库框架,让您专一于影响性能的应用程序 / 模块:

  _     ._   __/__   _ _  _  _ _/_   Recorded: 14:53:35  Samples:  131
 /_//_/// /_\ / //_// / //_'/ //    Duration: 3.131     CPU time: 0.195
/   _/                    v3.0.0b3

Program: examples/django_example/manage.py runserver --nothreading --noreload

3.131 <module>  manage.py:2
└─ 3.118 execute_from_command_line  django/core/management/__init__.py:378
      [473 frames hidden]  django, socketserver, selectors, wsgi...
         2.836 select  selectors.py:365
         0.126 _get_response  django/core/handlers/base.py:96
         └─ 0.126 hello_world  django_example/views.py:4

最初的话

本文分享了 pyinstrument 的用法,有了这个性能剖析神器,当前优化代码能够节俭很多工夫了,这样的效率神器很值得分享,毕竟人生苦短,能多点工夫干点有意思的不香么?

开源前哨 日常分享热门、乏味和实用的开源我的项目。参加保护 10 万 + Star 的开源技术资源库,包含:Python、Java、C/C++、Go、JS、CSS、Node.js、PHP、.NET 等。

正文完
 0