Skip to content

Commit 8718b17

Browse files
committed
feat: add sleep benches
1 parent e647062 commit 8718b17

File tree

4 files changed

+105
-0
lines changed

4 files changed

+105
-0
lines changed

core/instrument-hooks

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit a9ae7a4d897dbb694cb0355c9994141a7c4a1ab9
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../google_benchmark_cmake/sleep_bench.hpp

examples/google_benchmark_cmake/main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <cstring>
44

55
#include "fixture_bench.hpp"
6+
#include "sleep_bench.hpp"
67
#include "template_bench.hpp"
78

89
template <class... Args>
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#pragma once
2+
3+
#include <benchmark/benchmark.h>
4+
5+
#include <chrono>
6+
#include <thread>
7+
8+
// Note: `std::this_thread::sleep_for` and other methods rely on the kernel's
9+
// scheduling and may not be precise for very short durations (<1ms). By using
10+
// a busy-wait loop, we can achieve more precise timing at the cost of CPU usage.
11+
//
12+
// See COD-1155 for more information.
13+
static inline void busy_sleep_for(std::chrono::nanoseconds duration) {
14+
auto start = std::chrono::steady_clock::now();
15+
while ((std::chrono::steady_clock::now() - start) < duration) {
16+
// Busy-wait
17+
}
18+
}
19+
20+
static void BM_sleep_1ns(benchmark::State& state) {
21+
for (auto _ : state) {
22+
busy_sleep_for(std::chrono::nanoseconds(1));
23+
}
24+
}
25+
BENCHMARK(BM_sleep_1ns);
26+
27+
static void BM_sleep_10ns(benchmark::State& state) {
28+
for (auto _ : state) {
29+
busy_sleep_for(std::chrono::nanoseconds(10));
30+
}
31+
}
32+
BENCHMARK(BM_sleep_10ns);
33+
34+
static void BM_sleep_50ns(benchmark::State& state) {
35+
for (auto _ : state) {
36+
busy_sleep_for(std::chrono::nanoseconds(50));
37+
}
38+
}
39+
BENCHMARK(BM_sleep_50ns);
40+
41+
static void BM_sleep_100ns(benchmark::State& state) {
42+
for (auto _ : state) {
43+
busy_sleep_for(std::chrono::nanoseconds(100));
44+
}
45+
}
46+
BENCHMARK(BM_sleep_100ns);
47+
48+
static void BM_sleep_1us(benchmark::State& state) {
49+
for (auto _ : state) {
50+
busy_sleep_for(std::chrono::microseconds(1));
51+
}
52+
}
53+
BENCHMARK(BM_sleep_1us);
54+
55+
static void BM_sleep_10us(benchmark::State& state) {
56+
for (auto _ : state) {
57+
busy_sleep_for(std::chrono::microseconds(10));
58+
}
59+
}
60+
BENCHMARK(BM_sleep_10us);
61+
62+
static void BM_sleep_50us(benchmark::State& state) {
63+
for (auto _ : state) {
64+
busy_sleep_for(std::chrono::microseconds(50));
65+
}
66+
}
67+
BENCHMARK(BM_sleep_50us);
68+
69+
static void BM_sleep_100us(benchmark::State& state) {
70+
for (auto _ : state) {
71+
busy_sleep_for(std::chrono::microseconds(100));
72+
}
73+
}
74+
BENCHMARK(BM_sleep_100us);
75+
76+
static void BM_sleep_1ms(benchmark::State& state) {
77+
for (auto _ : state) {
78+
busy_sleep_for(std::chrono::milliseconds(1));
79+
}
80+
}
81+
BENCHMARK(BM_sleep_1ms);
82+
83+
static void BM_sleep_10ms(benchmark::State& state) {
84+
for (auto _ : state) {
85+
busy_sleep_for(std::chrono::milliseconds(10));
86+
}
87+
}
88+
BENCHMARK(BM_sleep_10ms);
89+
90+
static void BM_sleep_50ms(benchmark::State& state) {
91+
for (auto _ : state) {
92+
busy_sleep_for(std::chrono::milliseconds(50));
93+
}
94+
}
95+
BENCHMARK(BM_sleep_50ms);
96+
97+
static void BM_sleep_100ms(benchmark::State& state) {
98+
for (auto _ : state) {
99+
busy_sleep_for(std::chrono::milliseconds(100));
100+
}
101+
}
102+
BENCHMARK(BM_sleep_100ms);

0 commit comments

Comments
 (0)