-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstrument-hooks.go
More file actions
97 lines (81 loc) · 2.39 KB
/
Copy pathinstrument-hooks.go
File metadata and controls
97 lines (81 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package testing
/*
#cgo CFLAGS: -I@@INSTRUMENT_HOOKS_DIR@@/includes -Wno-format -Wno-format-security
#include "@@INSTRUMENT_HOOKS_DIR@@/dist/core.c"
#define MARKER_TYPE_BENCHMARK_START c_MARKER_TYPE_BENCHMARK_START__247
#define MARKER_TYPE_BENCHMARK_END c_MARKER_TYPE_BENCHMARK_END__248
typedef struct instruments_root_InstrumentHooks__547 InstrumentHooks;
*/
import "C"
import (
"os"
"runtime"
"unsafe"
)
var integrationVersion = "@@GO_RUNNER_VERSION@@"
type InstrumentHooks struct {
hooks *C.InstrumentHooks
}
func NewInstrumentHooks() *InstrumentHooks {
inst := &InstrumentHooks{
hooks: C.instrument_hooks_init(),
}
runtime.SetFinalizer(inst, (*InstrumentHooks).cleanup)
inst.SetIntegration("codspeed-go", integrationVersion)
return inst
}
func (i *InstrumentHooks) Close() {
if i.hooks != nil {
C.instrument_hooks_deinit(i.hooks)
i.hooks = nil
runtime.SetFinalizer(i, nil)
}
}
func (i *InstrumentHooks) cleanup() {
i.Close()
}
func (i *InstrumentHooks) SetIntegration(name, version string) {
if i.hooks == nil {
return
}
nameC := C.CString(name)
versionC := C.CString(version)
defer C.free(unsafe.Pointer(nameC))
defer C.free(unsafe.Pointer(versionC))
C.instrument_hooks_set_integration(i.hooks, (*C.uint8_t)(unsafe.Pointer(nameC)), (*C.uint8_t)(unsafe.Pointer(versionC)))
}
func (i *InstrumentHooks) StartBenchmark() {
if i.hooks != nil {
C.instrument_hooks_start_benchmark(i.hooks)
}
}
func (i *InstrumentHooks) StopBenchmark() {
if i.hooks != nil {
C.instrument_hooks_stop_benchmark(i.hooks)
}
}
func (i *InstrumentHooks) SetExecutedBenchmark(pid uint32, name string) {
if i.hooks == nil {
return
}
nameC := C.CString(name)
defer C.free(unsafe.Pointer(nameC))
C.instrument_hooks_set_executed_benchmark(i.hooks, C.uint(pid), (*C.uint8_t)(unsafe.Pointer(nameC)))
}
func (i *InstrumentHooks) IsInstrumented() bool {
if i.hooks == nil {
return false
}
return bool(C.instrument_hooks_is_instrumented(i.hooks))
}
func CurrentTimestamp() uint64 {
return uint64(C.instrument_hooks_current_timestamp())
}
func (i *InstrumentHooks) AddBenchmarkTimestamps(startTimestamp, endTimestamp uint64) {
if i.hooks == nil {
return
}
pid := uint32(os.Getpid())
C.instrument_hooks_add_marker(i.hooks, C.uint32_t(pid), C.MARKER_TYPE_BENCHMARK_START, C.uint64_t(startTimestamp))
C.instrument_hooks_add_marker(i.hooks, C.uint32_t(pid), C.MARKER_TYPE_BENCHMARK_END, C.uint64_t(endTimestamp))
}