@@ -2,7 +2,6 @@ package testing
22
33import (
44 "flag"
5- "os"
65 "reflect"
76 "runtime"
87 "testing"
@@ -14,7 +13,9 @@ import (
1413)
1514
1615var (
17- parallel int
16+ parallel int
17+ tRunPatched bool
18+ bRunPatched bool
1819)
1920
2021// Initialize the testing instrumentation
@@ -54,7 +55,7 @@ func Init(m *testing.M) {
5455 benchmarks = append (benchmarks , testing.InternalBenchmark {
5556 Name : benchmark .Name ,
5657 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 {
5859 funcValue (b )
5960 } else {
6061 startBenchmark (b , funcPointer , funcValue )
@@ -64,36 +65,50 @@ func Init(m *testing.M) {
6465 }
6566 * intBenchmarks = benchmarks
6667 }
68+ }
6769
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+ }
8579
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
98113 }
99114}
0 commit comments