-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstrument-hooks.go
More file actions
114 lines (96 loc) · 2.68 KB
/
Copy pathinstrument-hooks.go
File metadata and controls
114 lines (96 loc) · 2.68 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package testing
/*
#cgo CFLAGS: -std=c11 -I@@INSTRUMENT_HOOKS_DIR@@/includes -Wno-format -Wno-format-security -Wno-incompatible-pointer-types
#include "@@INSTRUMENT_HOOKS_DIR@@/dist/core.c"
*/
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, nameC, 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 int32, 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.int32_t(pid), 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 := C.int32_t(os.Getpid())
C.instrument_hooks_add_marker(i.hooks, C.int32_t(pid), C.MARKER_TYPE_BENCHMARK_START, C.uint64_t(startTimestamp))
C.instrument_hooks_add_marker(i.hooks, C.int32_t(pid), C.MARKER_TYPE_BENCHMARK_END, C.uint64_t(endTimestamp))
}
func (i *InstrumentHooks) SetEnvironment(sectionName, key, value string) {
if i.hooks == nil {
return
}
sectionNameC := C.CString(sectionName)
keyC := C.CString(key)
valueC := C.CString(value)
defer C.free(unsafe.Pointer(sectionNameC))
defer C.free(unsafe.Pointer(keyC))
defer C.free(unsafe.Pointer(valueC))
C.instrument_hooks_set_environment(i.hooks, sectionNameC, keyC, valueC)
}
func (i *InstrumentHooks) WriteEnvironment(pid int32) {
if i.hooks == nil {
return
}
C.instrument_hooks_write_environment(i.hooks, C.int32_t(pid))
}