Skip to content

Commit fcec45e

Browse files
committed
fix(testing): use correct number of rounds for b.Loop() benchmarks
1 parent ce62136 commit fcec45e

File tree

2 files changed

+13
-14
lines changed

2 files changed

+13
-14
lines changed

go-runner/src/results/raw_result.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ pub struct RawResult {
1111
pub uri: String,
1212
pub pid: u32,
1313
pub codspeed_time_per_round_ns: Vec<u64>,
14-
15-
#[serde(default)]
1614
pub codspeed_iters_per_round: Vec<u64>,
1715
}
1816

@@ -39,14 +37,11 @@ impl RawResult {
3937
.iter()
4038
.map(|t| *t as u128)
4139
.collect::<Vec<_>>();
42-
let iters_per_round = if self.codspeed_iters_per_round.is_empty() {
43-
vec![1; times_per_round_ns.len()]
44-
} else {
45-
self.codspeed_iters_per_round
46-
.iter()
47-
.map(|i| *i as u128)
48-
.collect()
49-
};
40+
let iters_per_round = self
41+
.codspeed_iters_per_round
42+
.iter()
43+
.map(|i| *i as u128)
44+
.collect();
5045

5146
WalltimeBenchmark::from_runtime_data(
5247
self.name,
@@ -68,13 +63,14 @@ mod tests {
6863
"name": "BenchmarkFibonacci20-16",
6964
"uri": "pkg/foo/fib_test.go::BenchmarkFibonacci20-16",
7065
"pid": 777767,
71-
"codspeed_time_per_round_ns": [1000, 2000, 3000]
66+
"codspeed_time_per_round_ns": [1000, 2000, 3000],
67+
"codspeed_iters_per_round": [1, 2, 3]
7268
}"#;
7369
let result: RawResult = serde_json::from_str(json_data).unwrap();
7470

7571
assert_eq!(result.name, "BenchmarkFibonacci20-16");
7672
assert_eq!(result.pid, 777767);
7773
assert_eq!(result.codspeed_time_per_round_ns.len(), 3);
78-
assert_eq!(result.codspeed_iters_per_round.len(), 0); // Default: 1 per round
74+
assert_eq!(result.codspeed_iters_per_round.len(), 3);
7975
}
8076
}

testing/testing/benchmark.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,11 @@ func (b *B) StartTimer() {
165165
// while performing steps that you don't want to measure.
166166
func (b *B) StopTimer() {
167167
if b.timerOn {
168+
// For b.N loops: This will be called in runN which sets b.N to the number of iterations.
169+
// For b.Loop() loops: loopSlowPath sets b.N to 0 to prevent b.N loops within b.Loop. However, since
170+
// we're starting/stopping the timer for each iteration in the b.Loop() loop, we can use 1 as
171+
// the number of iterations for this round.
172+
b.codspeedItersPerRound = append(b.codspeedItersPerRound, max(int64(b.N), 1))
168173
b.codspeedTimePerRoundNs = append(b.codspeedTimePerRoundNs, highPrecisionTimeSince(b.start))
169174
b.duration += highPrecisionTimeSince(b.start)
170175
// runtime.ReadMemStats(&memStats)
@@ -242,8 +247,6 @@ func (b *B) __codspeed_root_frame__runN(n int) {
242247
b.previousN = n
243248
b.previousDuration = b.duration
244249

245-
b.codspeedItersPerRound = append(b.codspeedItersPerRound, int64(n))
246-
247250
if b.loop.n > 0 && !b.loop.done && !b.failed {
248251
b.Error("benchmark function returned without B.Loop() == false (break or return in loop?)")
249252
}

0 commit comments

Comments
 (0)