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