1
1
package testing
2
2
3
3
import (
4
- "os"
5
4
"reflect"
6
5
"runtime"
7
6
"testing"
@@ -11,6 +10,11 @@ import (
11
10
"go.undefinedlabs.com/scopeagent/reflection"
12
11
)
13
12
13
+ var (
14
+ tRunPatched bool
15
+ bRunPatched bool
16
+ )
17
+
14
18
// Initialize the testing instrumentation
15
19
func Init (m * testing.M ) {
16
20
if tPointer , err := reflection .GetFieldPointerOf (m , "tests" ); err == nil {
@@ -41,7 +45,7 @@ func Init(m *testing.M) {
41
45
benchmarks = append (benchmarks , testing.InternalBenchmark {
42
46
Name : benchmark .Name ,
43
47
F : func (b * testing.B ) { // Indirection of the original benchmark
44
- if envDMPatch , set := os . LookupEnv ( "SCOPE_DISABLE_MONKEY_PATCHING" ); ! set || envDMPatch == "" {
48
+ if bRunPatched {
45
49
funcValue (b )
46
50
} else {
47
51
startBenchmark (b , funcPointer , funcValue )
@@ -51,36 +55,50 @@ func Init(m *testing.M) {
51
55
}
52
56
* intBenchmarks = benchmarks
53
57
}
58
+ }
54
59
55
- if envDMPatch , set := os .LookupEnv ("SCOPE_DISABLE_MONKEY_PATCHING" ); ! set || envDMPatch == "" {
56
- // We monkey patch the `testing.T.Run()` func to auto instrument sub tests
57
- var t * testing.T
58
- tType := reflect .TypeOf (t )
59
- if tRunMethod , ok := tType .MethodByName ("Run" ); ok {
60
- _ , err := mpatch .PatchMethodByReflect (tRunMethod , func (t * testing.T , name string , f func (t * testing.T )) bool {
61
- pc , _ , _ , _ := runtime .Caller (1 )
62
- gT := FromTestingT (t )
63
- return gT .Run (name , func (childT * testing.T ) {
64
- addAutoInstrumentedTest (childT )
65
- childTest := StartTestFromCaller (childT , pc )
66
- defer childTest .end ()
67
- f (childT )
68
- })
69
- })
70
- logOnError (err )
71
- }
60
+ func PatchTRun () {
61
+ // We monkey patch the `testing.T.Run()` func to auto instrument sub tests
62
+ var t * testing.T
63
+ var tRunMethod reflect.Method
64
+ var ok bool
65
+ tType := reflect .TypeOf (t )
66
+ if tRunMethod , ok = tType .MethodByName ("Run" ); ! ok {
67
+ return
68
+ }
72
69
73
- // We monkey patch the `testing.B.Run()` func to auto instrument sub benchmark
74
- var b * testing.B
75
- bType := reflect .TypeOf (b )
76
- if bRunMethod , ok := bType .MethodByName ("Run" ); ok {
77
- _ , err := mpatch .PatchMethodByReflect (bRunMethod , func (b * testing.B , name string , f func (b * testing.B )) bool {
78
- pc , _ , _ , _ := runtime .Caller (1 )
79
- return FromTestingB (b ).Run (name , func (b * testing.B ) {
80
- StartBenchmark (b , pc , f )
81
- })
82
- })
83
- logOnError (err )
84
- }
70
+ _ , err := mpatch .PatchMethodByReflect (tRunMethod , func (t * testing.T , name string , f func (t * testing.T )) bool {
71
+ pc , _ , _ , _ := runtime .Caller (1 )
72
+ gT := FromTestingT (t )
73
+ return gT .Run (name , func (childT * testing.T ) {
74
+ addAutoInstrumentedTest (childT )
75
+ childTest := StartTestFromCaller (childT , pc )
76
+ defer childTest .end ()
77
+ f (childT )
78
+ })
79
+ })
80
+ if ! logOnError (err ) {
81
+ tRunPatched = true
82
+ }
83
+ }
84
+
85
+ func PatchBRun () {
86
+ // We monkey patch the `testing.B.Run()` func to auto instrument sub benchmark
87
+ var b * testing.B
88
+ var bRunMethod reflect.Method
89
+ var ok bool
90
+ bType := reflect .TypeOf (b )
91
+ if bRunMethod , ok = bType .MethodByName ("Run" ); ! ok {
92
+ return
93
+ }
94
+
95
+ _ , err := mpatch .PatchMethodByReflect (bRunMethod , func (b * testing.B , name string , f func (b * testing.B )) bool {
96
+ pc , _ , _ , _ := runtime .Caller (1 )
97
+ return FromTestingB (b ).Run (name , func (b * testing.B ) {
98
+ StartBenchmark (b , pc , f )
99
+ })
100
+ })
101
+ if ! logOnError (err ) {
102
+ bRunPatched = true
85
103
}
86
104
}
0 commit comments