基准测试#

设置#

首先安装 Archery 实用程序以运行基准测试套件。

运行基准测试套件#

可以使用 benchmark run 子命令运行基准测试套件。

# Run benchmarks in the current git workspace
archery benchmark run
# Storing the results in a file
archery benchmark run --output=run.json

有时,需要传递自定义 CMake 标志,例如。

export CC=clang-8 CXX=clang++8
archery benchmark run --cmake-extras="-DARROW_SIMD_LEVEL=NONE"

此外,可以指定完整的 CMake 构建目录。

archery benchmark run $HOME/arrow/cpp/release-build

比较#

基准测试的一个目标是检测性能回归。 为此,archery 通过 benchmark diff 子命令实现了一个基准测试比较工具。

在默认调用中,它将比较当前源(在 git 中称为当前工作区)与本地主分支

archery --quiet benchmark diff --benchmark-filter=FloatParsing
-----------------------------------------------------------------------------------
Non-regressions: (1)
-----------------------------------------------------------------------------------
               benchmark            baseline           contender  change % counters
 FloatParsing<FloatType>  105.983M items/sec  105.983M items/sec       0.0       {}

------------------------------------------------------------------------------------
Regressions: (1)
------------------------------------------------------------------------------------
                benchmark            baseline           contender  change % counters
 FloatParsing<DoubleType>  209.941M items/sec  109.941M items/sec   -47.632       {}

有关更多信息,请调用 archery benchmark diff --help 命令以获取多个调用示例。

高效迭代#

由于构建时间长和运行时间长,使用基准测试开发进行迭代可能是一个乏味的过程。 archery benchmark diff 可以使用多种技巧来减少这种开销。

首先,基准测试命令支持比较现有的构建目录。这可以与 --preserve 标志配对,以避免从零开始重建源。

# First invocation clone and checkouts in a temporary directory. The
# directory is preserved with --preserve
archery benchmark diff --preserve

# Modify C++ sources

# Re-run benchmark in the previously created build directory.
archery benchmark diff /tmp/arrow-bench*/{WORKSPACE,master}/build

其次,可以将基准测试运行结果保存在 json 文件中。 这也避免了重建源,但也避免了执行(有时)繁重的基准测试。 这种技术可以用作一种简陋的缓存。

# Run the benchmarks on a given commit and save the result
archery benchmark run --output=run-head-1.json HEAD~1
# Compare the previous captured result with HEAD
archery benchmark diff HEAD run-head-1.json

第三,基准测试命令支持过滤套件(--suite-filter)和基准测试(--benchmark-filter),这两个选项都支持正则表达式。

# Taking over a previous run, but only filtering for benchmarks matching
# `Kernel` and suite matching `compute-aggregate`.
archery benchmark diff                                       \
  --suite-filter=compute-aggregate --benchmark-filter=Kernel \
  /tmp/arrow-bench*/{WORKSPACE,master}/build

代替在比较时重新运行基准测试,可以为竞争者和/或基线指定一个 JSON 文件(由 archery benchmark run 生成)。

archery benchmark run --output=baseline.json $HOME/arrow/cpp/release-build
git checkout some-feature
archery benchmark run --output=contender.json $HOME/arrow/cpp/release-build
archery benchmark diff contender.json baseline.json

回归检测#

编写基准测试#

  1. 基准测试命令将(默认情况下)使用正则表达式 ^Regression 过滤基准测试。 这样,默认情况下不会运行所有基准测试。 因此,如果您希望自动验证您的基准测试是否存在回归,则名称必须匹配。

  2. 基准测试命令将使用 --benchmark_repetitions=K 选项来获得统计显着性。 因此,基准测试不应覆盖 (C++) 基准测试的参数定义中的重复次数。

  3. 由于 #2,基准测试应运行得足够快。 通常,当输入不适合内存 (L2/L3) 时,基准测试将受到内存限制而不是 CPU 限制。 在这种情况下,可以缩小输入。

  4. 默认情况下,google 的基准测试库将使用 cputime 指标,它是进程的所有线程在 CPU 上专用的运行时总和。 与 realtime(即挂钟时间,例如 end_time - start_time 之间的差)相比。 在单线程模型中,cputime 是首选,因为它受上下文切换的影响较小。 在多线程场景中,cputime 将给出不正确的结果,因为它会被线程数膨胀并且可能与 realtime 相差甚远。 因此,如果基准测试是多线程的,则最好使用 SetRealtime(),请参阅此例子

脚本#

archery 是作为具有命令行前端的 python 库编写的。 可以导入该库以自动执行某些任务。

由于构建输出,命令行界面的某些调用可能非常冗长。 可以使用 --quiet 选项控制/避免这种情况,或者可以使用 --output=<file>,例如。

archery benchmark diff --benchmark-filter=Kernel --output=compare.json ...