1
1
package debug
2
2
3
- import "testing"
4
- import "strings"
5
- import "bytes"
6
- import "time"
3
+ import (
4
+ "bytes"
5
+ "fmt"
6
+ "io/ioutil"
7
+ "os"
8
+ "path/filepath"
9
+ "strings"
10
+ "testing"
11
+ "time"
12
+ )
7
13
8
14
func assertContains (t * testing.T , str , substr string ) {
9
15
if ! strings .Contains (str , substr ) {
@@ -32,6 +38,21 @@ func TestDefault(t *testing.T) {
32
38
}
33
39
}
34
40
41
+ func TestDefaultLazy (t * testing.T ) {
42
+ var b []byte
43
+ buf := bytes .NewBuffer (b )
44
+ SetWriter (buf )
45
+
46
+ debug := Debug ("foo" )
47
+ debug (func () string { return "something" })
48
+ debug (func () string { return "here" })
49
+ debug (func () string { return "whoop" })
50
+
51
+ if buf .Len () != 0 {
52
+ t .Fatalf ("buffer should be empty" )
53
+ }
54
+ }
55
+
35
56
func TestEnable (t * testing.T ) {
36
57
var b []byte
37
58
buf := bytes .NewBuffer (b )
@@ -43,6 +64,7 @@ func TestEnable(t *testing.T) {
43
64
debug ("something" )
44
65
debug ("here" )
45
66
debug ("whoop" )
67
+ debug (func () string { return "lazy" })
46
68
47
69
if buf .Len () == 0 {
48
70
t .Fatalf ("buffer should have output" )
@@ -52,6 +74,7 @@ func TestEnable(t *testing.T) {
52
74
assertContains (t , str , "something" )
53
75
assertContains (t , str , "here" )
54
76
assertContains (t , str , "whoop" )
77
+ assertContains (t , str , "lazy" )
55
78
}
56
79
57
80
func TestMultipleOneEnabled (t * testing.T ) {
@@ -63,17 +86,21 @@ func TestMultipleOneEnabled(t *testing.T) {
63
86
64
87
foo := Debug ("foo" )
65
88
foo ("foo" )
89
+ foo (func () string { return "foo lazy" })
66
90
67
91
bar := Debug ("bar" )
68
92
bar ("bar" )
93
+ bar (func () string { return "bar lazy" })
69
94
70
95
if buf .Len () == 0 {
71
96
t .Fatalf ("buffer should have output" )
72
97
}
73
98
74
99
str := string (buf .Bytes ())
75
100
assertContains (t , str , "foo" )
101
+ assertContains (t , str , "foo lazy" )
76
102
assertNotContains (t , str , "bar" )
103
+ assertNotContains (t , str , "bar lazy" )
77
104
}
78
105
79
106
func TestMultipleEnabled (t * testing.T ) {
@@ -85,17 +112,21 @@ func TestMultipleEnabled(t *testing.T) {
85
112
86
113
foo := Debug ("foo" )
87
114
foo ("foo" )
115
+ foo (func () string { return "foo lazy" })
88
116
89
117
bar := Debug ("bar" )
90
118
bar ("bar" )
119
+ bar (func () string { return "bar lazy" })
91
120
92
121
if buf .Len () == 0 {
93
122
t .Fatalf ("buffer should have output" )
94
123
}
95
124
96
125
str := string (buf .Bytes ())
97
126
assertContains (t , str , "foo" )
127
+ assertContains (t , str , "foo lazy" )
98
128
assertContains (t , str , "bar" )
129
+ assertContains (t , str , "bar lazy" )
99
130
}
100
131
101
132
func TestEnableDisable (t * testing.T ) {
@@ -108,9 +139,11 @@ func TestEnableDisable(t *testing.T) {
108
139
109
140
foo := Debug ("foo" )
110
141
foo ("foo" )
142
+ foo (func () string { return "foo" })
111
143
112
144
bar := Debug ("bar" )
113
145
bar ("bar" )
146
+ bar (func () string { return "bar" })
114
147
115
148
if buf .Len () != 0 {
116
149
t .Fatalf ("buffer should not have output" )
@@ -143,10 +176,70 @@ func BenchmarkDisabled(b *testing.B) {
143
176
}
144
177
}
145
178
179
+ func BenchmarkDisabledLazy (b * testing.B ) {
180
+ debug := Debug ("something" )
181
+ for i := 0 ; i < b .N ; i ++ {
182
+ debug (func () string { return "lazy" })
183
+ }
184
+ }
185
+
146
186
func BenchmarkNonMatch (b * testing.B ) {
147
187
debug := Debug ("something" )
148
188
Enable ("nonmatch" )
149
189
for i := 0 ; i < b .N ; i ++ {
150
190
debug ("stuff" )
151
191
}
152
192
}
193
+
194
+ func BenchmarkLargeNonMatch (b * testing.B ) {
195
+ debug := Debug ("large:not:lazy" )
196
+
197
+ abs , _ := filepath .Abs ("./crashes.json" )
198
+ file := GetFileBytes (abs )
199
+
200
+ Enable ("nonmatch" )
201
+ for i := 0 ; i < b .N ; i ++ {
202
+ debug (string (file ))
203
+ }
204
+ }
205
+
206
+ func BenchmarkLargeLazyNonMatch (b * testing.B ) {
207
+ debug := Debug ("large:lazy" )
208
+
209
+ abs , _ := filepath .Abs ("./crashes.json" )
210
+ file := GetFileBytes (abs )
211
+
212
+ Enable ("nonmatch" )
213
+ for i := 0 ; i < b .N ; i ++ {
214
+ debug (func () string {
215
+ return string (file )
216
+ })
217
+ }
218
+ }
219
+
220
+ func BenchmarkLargeLazyMatch (b * testing.B ) {
221
+ debug := Debug ("large:lazy" )
222
+
223
+ abs , _ := filepath .Abs ("./crashes.json" )
224
+ file := GetFileBytes (abs )
225
+
226
+ Enable ("large:lazy" )
227
+ for i := 0 ; i < b .N ; i ++ {
228
+ debug (func () string {
229
+ return string (file )
230
+ })
231
+ }
232
+ }
233
+
234
+ func GetFileBytes (filename string ) []byte {
235
+ file , err := os .Open (filename )
236
+ // if we os.Open returns an error then handle it
237
+ if err != nil {
238
+ fmt .Println (err )
239
+ }
240
+ // defer the closing of our jsonFile so that we can parse it later on
241
+ defer file .Close ()
242
+ bytes , _ := ioutil .ReadAll (file )
243
+
244
+ return bytes
245
+ }
0 commit comments