Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions testing/fork.sh
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ restore_files "${CODSPEED_FILES[@]}"

apply_patch "patches/benchmark_stopbenchmark_fail.patch" 10 ".."
apply_patch "patches/benchmark_stoptimer_mitigation.patch" 10 ".."
apply_patch "patches/benchmark_benchmarkers_bloop.patch" 10 ".."


# Run pre-commit and format the code
Expand Down
40 changes: 40 additions & 0 deletions testing/patches/benchmark_benchmarkers_bloop.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
diff --git a/testing/testing/benchmark.go b/testing/testing/benchmark.go
index 4947e11..0040973 100644
--- a/testing/testing/benchmark.go
+++ b/testing/testing/benchmark.go
@@ -226,15 +226,7 @@ func (b *B) StopTimer() {
b.StopTimerWithoutMarker()

if timerOn {
- if b.startTimestamp >= endTimestamp {
- // This should never happen, unless we have a bug in the timer logic.
- panic(fmt.Sprintf("Invalid benchmark timestamps: start timestamp (%d) is greater than or equal to end timestamp (%d)", b.startTimestamp, endTimestamp))
- }
- b.startTimestamps = append(b.startTimestamps, b.startTimestamp)
- b.stopTimestamps = append(b.stopTimestamps, endTimestamp)
-
- // Reset to prevent accidental reuse
- b.startTimestamp = 0
+ b.AddBenchmarkMarkers(endTimestamp)
}
}

@@ -587,7 +579,18 @@ func (b *B) loopSlowPath() bool {
more = b.stopOrScaleBLoop()
}
if !more {
- b.StopTimerWithoutMarker()
+ // NOTE: We could move the endTimestamp capturing further up or even into the Loop() function
+ // but this will result in a huge performance degradation since the C FFI calls are expensive.
+ //
+ // The only downside of having this here, is that there's a small chance of perf sampling the
+ // benchmark framework code which already happens anyway because we only emit 1 pair of
+ // start/stop markers per benchmark to minimize overhead and allow full flamegraphs.
+ endTimestamp := capi.CurrentTimestamp()
+
+ // Edge case: The timer is stopped in b.Loop() which prevents any further calls to
+ // StopTimer() from adding the benchmark markers. We have to manually submit them here,
+ // once the benchmark loop is done.
+ b.AddBenchmarkMarkers(endTimestamp)
b.codspeed.instrument_hooks.StopBenchmark()
b.sendAccumulatedTimestamps()
23 changes: 13 additions & 10 deletions testing/testing/benchmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,15 +226,7 @@ func (b *B) StopTimer() {
b.StopTimerWithoutMarker()

if timerOn {
if b.startTimestamp >= endTimestamp {
// This should never happen, unless we have a bug in the timer logic.
panic(fmt.Sprintf("Invalid benchmark timestamps: start timestamp (%d) is greater than or equal to end timestamp (%d)", b.startTimestamp, endTimestamp))
}
b.startTimestamps = append(b.startTimestamps, b.startTimestamp)
b.stopTimestamps = append(b.stopTimestamps, endTimestamp)

// Reset to prevent accidental reuse
b.startTimestamp = 0
b.AddBenchmarkMarkers(endTimestamp)
}
}

Expand Down Expand Up @@ -587,7 +579,18 @@ func (b *B) loopSlowPath() bool {
more = b.stopOrScaleBLoop()
}
if !more {
b.StopTimerWithoutMarker()
// NOTE: We could move the endTimestamp capturing further up or even into the Loop() function
// but this will result in a huge performance degradation since the C FFI calls are expensive.
//
// The only downside of having this here, is that there's a small chance of perf sampling the
// benchmark framework code which already happens anyway because we only emit 1 pair of
// start/stop markers per benchmark to minimize overhead and allow full flamegraphs.
endTimestamp := capi.CurrentTimestamp()

// Edge case: The timer is stopped in b.Loop() which prevents any further calls to
// StopTimer() from adding the benchmark markers. We have to manually submit them here,
// once the benchmark loop is done.
b.AddBenchmarkMarkers(endTimestamp)
b.codspeed.instrument_hooks.StopBenchmark()
b.sendAccumulatedTimestamps()

Expand Down
13 changes: 13 additions & 0 deletions testing/testing/codspeed.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,16 @@ func getGitRelativePath(absPath string) string {
func ensureBenchmarkIsStopped(b *B) {
b.codspeed.instrument_hooks.StopBenchmark()
}

func (b *B) AddBenchmarkMarkers(endTimestamp uint64) {
if b.startTimestamp >= endTimestamp {
// This should never happen, unless we have a bug in the timer logic.
panic(fmt.Sprintf("Invalid benchmark timestamps: start timestamp (%d) is greater than or equal to end timestamp (%d)", b.startTimestamp, endTimestamp))
}

b.startTimestamps = append(b.startTimestamps, b.startTimestamp)
b.stopTimestamps = append(b.stopTimestamps, endTimestamp)

// Reset to prevent accidental reuse
b.startTimestamp = 0
}
Loading