Skip to content

Commit f4b479f

Browse files
committed
lazy debugging added
1 parent bfacf9d commit f4b479f

File tree

5 files changed

+140
-6
lines changed

5 files changed

+140
-6
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
crashes.json

debug.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ var (
2020
)
2121

2222
// Debugger function.
23-
type DebugFunction func(string, ...interface{})
23+
type DebugFunction func(interface{}, ...interface{})
2424

2525
// Terminal colors used at random.
2626
var colors []string = []string{
@@ -80,7 +80,7 @@ func Debug(name string) DebugFunction {
8080
color := colors[rand.Intn(len(colors))]
8181
prev := time.Now()
8282

83-
return func(format string, args ...interface{}) {
83+
return func(strOrFunc interface{}, args ...interface{}) {
8484
if !enabled {
8585
return
8686
}
@@ -89,6 +89,16 @@ func Debug(name string) DebugFunction {
8989
return
9090
}
9191

92+
var format, isString = strOrFunc.(string)
93+
94+
if !isString {
95+
lazy, isFunc := strOrFunc.(func() string)
96+
if !isFunc {
97+
panic("invalid first argument type for Debug, must either be a string or lazy function")
98+
}
99+
format = lazy()
100+
}
101+
92102
d := deltas(prevGlobal, prev, color)
93103
fmt.Fprintf(writer, d+" \033["+color+"m"+name+"\033[0m - "+format+"\n", args...)
94104
prevGlobal = time.Now()

debug_test.go

+97-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
package debug
22

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+
)
713

814
func assertContains(t *testing.T, str, substr string) {
915
if !strings.Contains(str, substr) {
@@ -32,6 +38,21 @@ func TestDefault(t *testing.T) {
3238
}
3339
}
3440

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+
3556
func TestEnable(t *testing.T) {
3657
var b []byte
3758
buf := bytes.NewBuffer(b)
@@ -43,6 +64,7 @@ func TestEnable(t *testing.T) {
4364
debug("something")
4465
debug("here")
4566
debug("whoop")
67+
debug(func() string { return "lazy" })
4668

4769
if buf.Len() == 0 {
4870
t.Fatalf("buffer should have output")
@@ -52,6 +74,7 @@ func TestEnable(t *testing.T) {
5274
assertContains(t, str, "something")
5375
assertContains(t, str, "here")
5476
assertContains(t, str, "whoop")
77+
assertContains(t, str, "lazy")
5578
}
5679

5780
func TestMultipleOneEnabled(t *testing.T) {
@@ -63,17 +86,21 @@ func TestMultipleOneEnabled(t *testing.T) {
6386

6487
foo := Debug("foo")
6588
foo("foo")
89+
foo(func() string { return "foo lazy" })
6690

6791
bar := Debug("bar")
6892
bar("bar")
93+
bar(func() string { return "bar lazy" })
6994

7095
if buf.Len() == 0 {
7196
t.Fatalf("buffer should have output")
7297
}
7398

7499
str := string(buf.Bytes())
75100
assertContains(t, str, "foo")
101+
assertContains(t, str, "foo lazy")
76102
assertNotContains(t, str, "bar")
103+
assertNotContains(t, str, "bar lazy")
77104
}
78105

79106
func TestMultipleEnabled(t *testing.T) {
@@ -85,17 +112,21 @@ func TestMultipleEnabled(t *testing.T) {
85112

86113
foo := Debug("foo")
87114
foo("foo")
115+
foo(func() string { return "foo lazy" })
88116

89117
bar := Debug("bar")
90118
bar("bar")
119+
bar(func() string { return "bar lazy" })
91120

92121
if buf.Len() == 0 {
93122
t.Fatalf("buffer should have output")
94123
}
95124

96125
str := string(buf.Bytes())
97126
assertContains(t, str, "foo")
127+
assertContains(t, str, "foo lazy")
98128
assertContains(t, str, "bar")
129+
assertContains(t, str, "bar lazy")
99130
}
100131

101132
func TestEnableDisable(t *testing.T) {
@@ -108,9 +139,11 @@ func TestEnableDisable(t *testing.T) {
108139

109140
foo := Debug("foo")
110141
foo("foo")
142+
foo(func() string { return "foo" })
111143

112144
bar := Debug("bar")
113145
bar("bar")
146+
bar(func() string { return "bar" })
114147

115148
if buf.Len() != 0 {
116149
t.Fatalf("buffer should not have output")
@@ -143,10 +176,70 @@ func BenchmarkDisabled(b *testing.B) {
143176
}
144177
}
145178

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+
146186
func BenchmarkNonMatch(b *testing.B) {
147187
debug := Debug("something")
148188
Enable("nonmatch")
149189
for i := 0; i < b.N; i++ {
150190
debug("stuff")
151191
}
152192
}
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+
}

example/multiple.lazy.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package main
2+
3+
import (
4+
"time"
5+
6+
. "github.com/visionmedia/go-debug"
7+
)
8+
9+
var a = Debug("multiple:a")
10+
var b = Debug("multiple:b")
11+
var c = Debug("multiple:c")
12+
13+
func work(debug DebugFunction, delay time.Duration) {
14+
for {
15+
debug(func() string { return "doing stuff" })
16+
time.Sleep(delay)
17+
}
18+
}
19+
20+
func main() {
21+
q := make(chan bool)
22+
23+
go work(a, 1000*time.Millisecond)
24+
go work(b, 250*time.Millisecond)
25+
go work(c, 100*time.Millisecond)
26+
27+
<-q
28+
}

scripts/getSomeDataForPefTests.sh

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/sh
2+
curl -o crashes.json 'https://data.ny.gov/api/views/xe9x-a24f/rows.json?accessType=DOWNLOAD'

0 commit comments

Comments
 (0)