[BUG] Minimum benchmark time with multiple threads

未关闭
#2,117 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@hahnjo 已经在做这个了。

开始于 2026年1月30日。

  • #2118 来自 @hahnjo —— 未关闭
  • #2256 来自 @devtejasx —— 已关闭,未合并

评估

难度
4/5
预计耗时
3-5 天
新手友好度
45/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
停滞
技术栈
cpp
领域
performance

调研方向

从处理 ThreadRange 基准测试的 --benchmark_min_time 以及 issue 中描述的 UseRealTime 行为开始。使用 --benchmark_min_time=1s 运行提供的 C++ 复现程序,并比较单线程和八线程的结果。完成标准是每个基准测试都为每个线程花费最少时间,并且线程数增加时迭代次数相应增加。

由索引模型根据 Issue 内容生成。

描述

Describe the bug
https://github.com/google/benchmark/pull/1836 fixed https://github.com/google/benchmark/issues/1834 and now the reported output makes more sense. However, the minimum benchmark time doesn't seem to be respected.

System
Arch Linux, GCC 15.2.1, benchmark 1.9.5-1

To reproduce
Consider

#include <benchmark/benchmark.h>

static void BM_MultiThreaded(benchmark::State& state) {
  int a = 0;

  for (auto _ : state) {
    for (int i = 0; i < 1000; i++) {
      benchmark::DoNotOptimize(a++);
    }
  }
}
BENCHMARK(BM_MultiThreaded)->ThreadRange(1, 8);

BENCHMARK_MAIN();

The output is somewhat expected:

---------------------------------------------------------------------
Benchmark                           Time             CPU   Iterations
---------------------------------------------------------------------
BM_MultiThreaded/threads:1        275 ns          274 ns      2585850
BM_MultiThreaded/threads:2        273 ns          272 ns      2259184
BM_MultiThreaded/threads:4        279 ns          278 ns      2449204
BM_MultiThreaded/threads:8        308 ns          307 ns      2148808

However, notice that the number of iterations stays constant - it should increase as more threads are used to run the iterations. For example:

 $ /usr/bin/time -v ./benchmark-multithreaded --benchmark_min_time=1s --benchmark_filter=threads:8
[...]
	User time (seconds): 1.72
	Elapsed (wall clock) time (h:mm:ss or m:ss): 0:00.25

With previous versions of benchmark, this could be fixed with UseRealTime() but that doesn't seem to work anymore since (probably) https://github.com/google/benchmark/pull/1836.

Expected behavior
Each benchmark should spend the minimum time (per thread).

主要语言
C++
星标
10.5k
派生
1.8k
平均合并
1 天 17 小时
30 天内合并 PR
30

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

google/benchmark 的其他 Issue

查看 google/benchmark 的全部 Issue

相似的 Issue

更多 C++ Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。