This repository has been archived by the owner on Aug 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathinit.go
183 lines (166 loc) · 4.01 KB
/
init.go
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package scopeagent // import "go.undefinedlabs.com/scopeagent"
import (
"context"
"fmt"
"os"
"os/signal"
"reflect"
"runtime"
"sync"
"syscall"
"testing"
"go.undefinedlabs.com/scopeagent/agent"
"go.undefinedlabs.com/scopeagent/env"
"go.undefinedlabs.com/scopeagent/instrumentation"
"go.undefinedlabs.com/scopeagent/instrumentation/logging"
scopetesting "go.undefinedlabs.com/scopeagent/instrumentation/testing"
)
var (
runningMutex sync.RWMutex
running bool
)
// Helper function to run a `testing.M` object and gracefully stopping the agent afterwards
func Run(m *testing.M, opts ...agent.Option) int {
if getRunningFlag() {
return m.Run()
}
setRunningFlag(true)
defer setRunningFlag(false)
opts = append(opts, agent.WithTestingModeEnabled())
newAgent, err := agent.NewAgent(opts...)
if err != nil {
res := m.Run()
if env.ScopeDebug.Value {
fmt.Printf("\n%v\n", err)
}
return res
}
logging.PatchStandardLogger()
scopetesting.Init(m)
// Handle SIGINT and SIGTERM
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-sigs
if newAgent != nil {
instrumentation.Logger().Println("Terminating agent, sending partial results...")
newAgent.Stop()
}
os.Exit(1)
}()
return newAgent.Run(m)
}
// Instruments the given test, returning a `Test` object that can be used to extend the test trace
func StartTest(t *testing.T, opts ...scopetesting.Option) *scopetesting.Test {
pc, _, _, _ := runtime.Caller(1)
return scopetesting.StartTestFromCaller(t, pc, opts...)
}
// Gets the *Test from a *testing.T
func GetTest(t *testing.T) *scopetesting.Test {
return scopetesting.GetTest(t)
}
// Gets the context from a test
func GetContextFromTest(t *testing.T) context.Context {
test := GetTest(t)
if test != nil {
return test.Context()
}
return context.TODO()
}
// Sets the test code from the caller of this func
func SetTestCodeFromCaller(t *testing.T) {
SetTestCodeFromCallerSkip(t, 1)
}
// Sets the test code from the caller of this func
func SetTestCodeFromCallerSkip(t *testing.T, skip int) {
test := GetTest(t)
if test == nil {
return
}
pc, _, _, _ := runtime.Caller(skip + 1)
test.SetTestCode(pc)
}
// Sets the test code from a func
func SetTestCodeFromFunc(t *testing.T, fn interface{}) {
test := GetTest(t)
if test == nil {
return
}
value := reflect.ValueOf(fn)
pc := value.Pointer()
test.SetTestCode(pc)
}
// Gets the *Benchmark from a *testing.B
func GetBenchmark(b *testing.B) *scopetesting.Benchmark {
return scopetesting.GetBenchmark(b)
}
// Instruments the given benchmark func
//
// Example of usage:
//
// func factorial(value int) int {
// if value == 1 {
// return 1
// }
// return value * factorial(value-1)
// }
//
// func BenchmarkFactorial(b *testing.B) {
// scopeagent.StartBenchmark(b, func(b *testing.B) {
// for i := 0; i < b.N; i++ {
// _ = factorial(25)
// }
// }
// }
//
// func BenchmarkFactorialWithSubBenchmarks(b *testing.B) {
// res := 0
//
// b.Run("25", func(b *testing.B) {
// scopeagent.StartBenchmark(b, func(b *testing.B) {
// for i := 0; i < b.N; i++ {
// res = factorial(25)
// }
// })
// })
//
// b.Run("50", func(b *testing.B) {
// scopeagent.StartBenchmark(b, func(b *testing.B) {
// for i := 0; i < b.N; i++ {
// res = factorial(50)
// }
// })
// })
//
// b.Run("75", func(b *testing.B) {
// scopeagent.StartBenchmark(b, func(b *testing.B) {
// for i := 0; i < b.N; i++ {
// res = factorial(75)
// }
// })
// })
//
// b.Run("100", func(b *testing.B) {
// scopeagent.StartBenchmark(b, func(b *testing.B) {
// for i := 0; i < b.N; i++ {
// res = factorial(100)
// }
// })
// })
//
// _ = res
// }
func StartBenchmark(b *testing.B, benchFunc func(b *testing.B)) {
pc, _, _, _ := runtime.Caller(1)
scopetesting.StartBenchmark(b, pc, benchFunc)
}
func setRunningFlag(value bool) {
runningMutex.Lock()
defer runningMutex.Unlock()
running = value
}
func getRunningFlag() bool {
runningMutex.RLock()
defer runningMutex.RUnlock()
return running
}