1

I want to measure how many times C++ function will be executed per N seconds, is there a way to do that via google benchmark? Maybe using some lambda for ComputeStatistics function?

If there is no way to do it via google benchmark: are there any other ways of measuring throughput?

blonded04
  • 147
  • 9
  • "Will be" as in theoretically? – tadman Jul 18 '23 at 15:17
  • 1
    The main point of the Benchmark library is to microbenchmark functions by calling in a loop. (You can make it `__attribute__((noinline,noipa))` to defeat any inlining and cross-iteration optimization.) Any tutorial or guide on using Benchmark should show you how to do this. To measure throughput, make the input to each call independent of the previous call. To measure latency, make one or more of the inputs to the next call dependent on the previous. – Peter Cordes Jul 18 '23 at 15:19

2 Answers2

3

The throughput is reported in units of items/s https://github.com/google/benchmark/blob/main/docs/user_guide.md#output-formats

Benchmark                Time(ns)    CPU(ns) Iterations
-------------------------------------------------------
BM_SetInsert/1024/1         28928      29349      23853  133.097kB/s   33.2742k items/s
BM_SetInsert/1024/8         32065      32913      21375  949.487kB/s   237.372k items/s
BM_SetInsert/1024/10        33157      33648      21431  1.13369MB/s   290.225k items/s

So to directly answer your question

I want to measure how many times C++ function will be executed per N seconds

you would take this items/s and multiply by N seconds, that would give you approximately the number of items processed in N seconds.

Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • As I found out: you have to add custom items counter to `Benchmark::State` by yourself which is hard enough for some interesting examples you may want to benchmark, and also adds some performance overhead (adds tiny, but anyway ads). – blonded04 Jul 21 '23 at 10:54
0

Actually, the easiest way is to use ->MinTime(seconds) for each of your benchmarks you want to calculate throughput for, and then simply taking Iterations from each of the benchmarks.

blonded04
  • 147
  • 9