Skip to content

Commit 967f1dd

Browse files
committed
feat: add sleep benches
1 parent e647062 commit 967f1dd

File tree

4 files changed

+106
-0
lines changed

4 files changed

+106
-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: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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
11+
// usage.
12+
//
13+
// See COD-1155 for more information.
14+
static inline void busy_sleep_for(std::chrono::nanoseconds duration) {
15+
auto start = std::chrono::steady_clock::now();
16+
while ((std::chrono::steady_clock::now() - start) < duration) {
17+
// Busy-wait
18+
}
19+
}
20+
21+
static void BM_sleep_1ns(benchmark::State& state) {
22+
for (auto _ : state) {
23+
busy_sleep_for(std::chrono::nanoseconds(1));
24+
}
25+
}
26+
BENCHMARK(BM_sleep_1ns);
27+
28+
static void BM_sleep_10ns(benchmark::State& state) {
29+
for (auto _ : state) {
30+
busy_sleep_for(std::chrono::nanoseconds(10));
31+
}
32+
}
33+
BENCHMARK(BM_sleep_10ns);
34+
35+
static void BM_sleep_50ns(benchmark::State& state) {
36+
for (auto _ : state) {
37+
busy_sleep_for(std::chrono::nanoseconds(50));
38+
}
39+
}
40+
BENCHMARK(BM_sleep_50ns);
41+
42+
static void BM_sleep_100ns(benchmark::State& state) {
43+
for (auto _ : state) {
44+
busy_sleep_for(std::chrono::nanoseconds(100));
45+
}
46+
}
47+
BENCHMARK(BM_sleep_100ns);
48+
49+
static void BM_sleep_1us(benchmark::State& state) {
50+
for (auto _ : state) {
51+
busy_sleep_for(std::chrono::microseconds(1));
52+
}
53+
}
54+
BENCHMARK(BM_sleep_1us);
55+
56+
static void BM_sleep_10us(benchmark::State& state) {
57+
for (auto _ : state) {
58+
busy_sleep_for(std::chrono::microseconds(10));
59+
}
60+
}
61+
BENCHMARK(BM_sleep_10us);
62+
63+
static void BM_sleep_50us(benchmark::State& state) {
64+
for (auto _ : state) {
65+
busy_sleep_for(std::chrono::microseconds(50));
66+
}
67+
}
68+
BENCHMARK(BM_sleep_50us);
69+
70+
static void BM_sleep_100us(benchmark::State& state) {
71+
for (auto _ : state) {
72+
busy_sleep_for(std::chrono::microseconds(100));
73+
}
74+
}
75+
BENCHMARK(BM_sleep_100us);
76+
77+
static void BM_sleep_1ms(benchmark::State& state) {
78+
for (auto _ : state) {
79+
busy_sleep_for(std::chrono::milliseconds(1));
80+
}
81+
}
82+
BENCHMARK(BM_sleep_1ms);
83+
84+
static void BM_sleep_10ms(benchmark::State& state) {
85+
for (auto _ : state) {
86+
busy_sleep_for(std::chrono::milliseconds(10));
87+
}
88+
}
89+
BENCHMARK(BM_sleep_10ms);
90+
91+
static void BM_sleep_50ms(benchmark::State& state) {
92+
for (auto _ : state) {
93+
busy_sleep_for(std::chrono::milliseconds(50));
94+
}
95+
}
96+
BENCHMARK(BM_sleep_50ms);
97+
98+
static void BM_sleep_100ms(benchmark::State& state) {
99+
for (auto _ : state) {
100+
busy_sleep_for(std::chrono::milliseconds(100));
101+
}
102+
}
103+
BENCHMARK(BM_sleep_100ms);

0 commit comments

Comments
 (0)