Skip to content

Commit 1039b1d

Browse files
committed
feat(testing): add benchmark markers
1 parent 87a627e commit 1039b1d

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

testing/capi/instrument-hooks.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,17 @@ typedef struct instruments_root_InstrumentHooks__547 InstrumentHooks;
88
import "C"
99
import (
1010
"runtime"
11+
"time"
1112
"unsafe"
1213
)
1314

15+
const (
16+
MarkerTypeSampleStart = 0
17+
MarkerTypeSampleEnd = 1
18+
MarkerTypeBenchmarkStart = 2
19+
MarkerTypeBenchmarkEnd = 3
20+
)
21+
1422
// This will be set in the go-runner
1523
var integrationVersion = "dev"
1624

@@ -79,3 +87,16 @@ func (i *InstrumentHooks) IsInstrumented() bool {
7987
}
8088
return bool(C.instrument_hooks_is_instrumented(i.hooks))
8189
}
90+
91+
func CurrentTimestamp() uint64 {
92+
return uint64(C.instrument_hooks_current_timestamp())
93+
}
94+
95+
func (i *InstrumentHooks) AddBenchmarkTimestamps(startTimestamp, endTimestamp uint64) {
96+
if i.hooks == nil {
97+
return
98+
}
99+
pid := uint32(C.getpid())
100+
C.instrument_hooks_add_marker(i.hooks, C.uint32_t(pid), C.uint8_t(MarkerTypeBenchmarkStart), C.uint64_t(startTimestamp))
101+
C.instrument_hooks_add_marker(i.hooks, C.uint32_t(pid), C.uint8_t(MarkerTypeBenchmarkEnd), C.uint64_t(endTimestamp))
102+
}

testing/testing/benchmark.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ type codspeed struct {
9090

9191
codspeedTimePerRoundNs []time.Duration
9292
codspeedItersPerRound []int64
93+
startTimestamp uint64
9394
}
9495

9596
// B is a type passed to [Benchmark] functions to manage benchmark
@@ -155,6 +156,7 @@ func (b *B) StartTimer() {
155156
// runtime.ReadMemStats(&memStats)
156157
// b.startAllocs = memStats.Mallocs
157158
// b.startBytes = memStats.TotalAlloc
159+
b.codspeed.startTimestamp = capi.CurrentTimestamp()
158160
b.start = highPrecisionTimeNow()
159161
b.timerOn = true
160162
// b.loop.i &^= loopPoisonTimer
@@ -165,6 +167,9 @@ func (b *B) StartTimer() {
165167
// while performing steps that you don't want to measure.
166168
func (b *B) StopTimer() {
167169
if b.timerOn {
170+
endTimestamp := capi.CurrentTimestamp()
171+
b.codspeed.instrument_hooks.AddBenchmarkTimestamps(b.codspeed.startTimestamp, endTimestamp)
172+
168173
// For b.N loops: This will be called in runN which sets b.N to the number of iterations.
169174
// For b.Loop() loops: loopSlowPath sets b.N to 0 to prevent b.N loops within b.Loop. However, since
170175
// we're starting/stopping the timer for each iteration in the b.Loop() loop, we can use 1 as

0 commit comments

Comments
 (0)