运行和分析基准测试的python模块
pyperf的Python项目详细描述
pythonpyperf模块是一个用于编写、运行和分析基准测试的工具包。
功能
- 运行可靠基准的简单api
- 自动校准时间预算的基准。
- 生成多个工作进程。
- 计算平均值和标准差。
- 检测基准测试结果是否不稳定。
- 存储基准测试结果的json格式。
- 支持多个单位:秒、字节和整数。
用法
要run a benchmark使用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 python3importpyperfrunner=pyperf.Runner()runner.timeit(name="sort a sorted list",stmt="sorted(s, key=f)",setup="f = lambda x: x; s = list(range(1000))")
有关timeit函数和 Runner类。运行脚本并将结果转储到名为 bench.json:
$ python3 bench.py -o bench.json
要analyze benchmark results使用pyperf stats命令:
$ python3 -m pyperf stats bench.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命令测试是否存在差异 意义重大。它支持多个基准测试套件(made 多个基准)
$ python3 -m pyperf compare_to py2.json py3.json --table +-----------+---------+------------------------------+ | Benchmark | py2 | py3 | +===========+=========+==============================+ | timeit | 4.70 us | 4.22 us: 1.11x faster (-10%) | +-----------+---------+------------------------------+
pyperf system tune命令来优化系统以运行稳定的基准测试。
自动收集计算机和基准上的元数据: 使用pyperf metadata命令显示它们,或者 pyperf collect_metadata手动收集它们的命令。
--track-memory和--tracemalloc要跟踪的选项 基准的内存使用情况。
快速链接
- pyperf documentation
- pyperf project homepage at GitHub(代码,错误)
- Download latest pyperf release at the Python Cheeseshop (PyPI)
在python 3上安装pyperf的命令:
python3 -m pip install pyperf
Pyperf支持Python2.7和Python3。它是根据麻省理工学院的许可证发行的。