跳转到主要内容

Python模块,用于运行和分析基准测试

项目描述

Latest release on the Python Cheeseshop (PyPI) Build status of pyperf on GitHub Actions

Python模块 pyperf 是一个用于编写、运行和分析基准测试的工具包。

功能

  • 运行可靠基准测试的简单API

  • 自动校准基准测试以适应时间预算。

  • 启动多个工作进程。

  • 计算平均值和标准差。

  • 检测基准结果是否不稳定。

  • 使用JSON格式存储基准结果。

  • 支持多个单位:秒、字节和整数。

用法

要运行基准测试,使用 pyperf timeit 命令(结果写入 bench.json

$ python3 -m pyperf timeit '[1,2]*1000' -o bench.json
.....................
Mean +- std dev: 4.22 us +- 0.08 us

或者编写一个基准测试脚本 bench.py

#!/usr/bin/env python3
import pyperf

runner = pyperf.Runner()
runner.timeit(name="sort a sorted list",
              stmt="sorted(s, key=f)",
              setup="f = lambda x: x; s = list(range(1000))")

查看 API文档 以获取有关 timeit 函数和 Runner 类的详细信息。要运行脚本并将结果输出到名为 bench.json 的文件中

$ python3 bench.py -o bench.json

要分析基准结果,使用 pyperf stats 命令

$ python3 -m pyperf stats telco.json
Total duration: 29.2 sec
Start date: 2016-10-21 03:14:19
End date: 2016-10-21 03:14:53
Raw value minimum: 177 ms
Raw value maximum: 183 ms

Number of calibration run: 1
Number of run with values: 40
Total number of run: 41

Number of warmup per run: 1
Number of value per run: 3
Loop iterations per value: 8
Total number of values: 120

Minimum:         22.1 ms
Median +- MAD:   22.5 ms +- 0.1 ms
Mean +- std dev: 22.5 ms +- 0.2 ms
Maximum:         22.9 ms

  0th percentile: 22.1 ms (-2% of the mean) -- minimum
  5th percentile: 22.3 ms (-1% of the mean)
 25th percentile: 22.4 ms (-1% of the mean) -- Q1
 50th percentile: 22.5 ms (-0% of the mean) -- median
 75th percentile: 22.7 ms (+1% of the mean) -- Q3
 95th percentile: 22.9 ms (+2% of the mean)
100th percentile: 22.9 ms (+2% of the mean) -- maximum

Number of outlier (out of 22.0 ms..23.0 ms): 0

还有

  • pyperf compare_to 命令用于测试差异是否显著。它支持多个基准测试套件(由多个基准组成)之间的比较

    $ python3 -m pyperf compare_to --table mult_list_py36.json mult_list_py37.json mult_list_py38.json
    +----------------+----------------+-----------------------+-----------------------+
    | Benchmark      | mult_list_py36 | mult_list_py37        | mult_list_py38        |
    +================+================+=======================+=======================+
    | [1]*1000       | 2.13 us        | 2.09 us: 1.02x faster | not significant       |
    +----------------+----------------+-----------------------+-----------------------+
    | [1,2]*1000     | 3.70 us        | 5.28 us: 1.42x slower | 3.18 us: 1.16x faster |
    +----------------+----------------+-----------------------+-----------------------+
    | [1,2,3]*1000   | 4.61 us        | 6.05 us: 1.31x slower | 4.17 us: 1.11x faster |
    +----------------+----------------+-----------------------+-----------------------+
    | Geometric mean | (ref)          | 1.22x slower          | 1.09x faster          |
    +----------------+----------------+-----------------------+-----------------------+
  • pyperf system tune 命令用于调整系统以运行稳定的基准测试。

  • 自动收集计算机和基准测试的元数据:使用 pyperf metadata 命令显示它们,或使用 pyperf collect_metadata 命令手动收集它们。

  • --track-memory--tracemalloc 选项用于跟踪基准测试的内存使用情况。

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解有关 安装包 的更多信息。

源分发

pyperf-2.8.0.tar.gz (225.2 kB 查看哈希值)

上传时间

构建分发

pyperf-2.8.0-py3-none-any.whl (142.5 kB 查看哈希值)

上传时间 Python 3

支持者

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面