Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lazy debugging added #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
crashes.json
14 changes: 12 additions & 2 deletions debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var (
)

// Debugger function.
type DebugFunction func(string, ...interface{})
type DebugFunction func(interface{}, ...interface{})

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

return func(format string, args ...interface{}) {
return func(strOrFunc interface{}, args ...interface{}) {
if !enabled {
return
}
Expand All @@ -89,6 +89,16 @@ func Debug(name string) DebugFunction {
return
}

var format, isString = strOrFunc.(string)

if !isString {
lazy, isFunc := strOrFunc.(func() string)
if !isFunc {
panic("invalid first argument type for Debug, must either be a string or lazy function")
}
format = lazy()
}

d := deltas(prevGlobal, prev, color)
fmt.Fprintf(writer, d+" \033["+color+"m"+name+"\033[0m - "+format+"\n", args...)
prevGlobal = time.Now()
Expand Down
101 changes: 97 additions & 4 deletions debug_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package debug

import "testing"
import "strings"
import "bytes"
import "time"
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"time"
)

func assertContains(t *testing.T, str, substr string) {
if !strings.Contains(str, substr) {
Expand Down Expand Up @@ -32,6 +38,21 @@ func TestDefault(t *testing.T) {
}
}

func TestDefaultLazy(t *testing.T) {
var b []byte
buf := bytes.NewBuffer(b)
SetWriter(buf)

debug := Debug("foo")
debug(func() string { return "something" })
debug(func() string { return "here" })
debug(func() string { return "whoop" })

if buf.Len() != 0 {
t.Fatalf("buffer should be empty")
}
}

func TestEnable(t *testing.T) {
var b []byte
buf := bytes.NewBuffer(b)
Expand All @@ -43,6 +64,7 @@ func TestEnable(t *testing.T) {
debug("something")
debug("here")
debug("whoop")
debug(func() string { return "lazy" })

if buf.Len() == 0 {
t.Fatalf("buffer should have output")
Expand All @@ -52,6 +74,7 @@ func TestEnable(t *testing.T) {
assertContains(t, str, "something")
assertContains(t, str, "here")
assertContains(t, str, "whoop")
assertContains(t, str, "lazy")
}

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

foo := Debug("foo")
foo("foo")
foo(func() string { return "foo lazy" })

bar := Debug("bar")
bar("bar")
bar(func() string { return "bar lazy" })

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

str := string(buf.Bytes())
assertContains(t, str, "foo")
assertContains(t, str, "foo lazy")
assertNotContains(t, str, "bar")
assertNotContains(t, str, "bar lazy")
}

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

foo := Debug("foo")
foo("foo")
foo(func() string { return "foo lazy" })

bar := Debug("bar")
bar("bar")
bar(func() string { return "bar lazy" })

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

str := string(buf.Bytes())
assertContains(t, str, "foo")
assertContains(t, str, "foo lazy")
assertContains(t, str, "bar")
assertContains(t, str, "bar lazy")
}

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

foo := Debug("foo")
foo("foo")
foo(func() string { return "foo" })

bar := Debug("bar")
bar("bar")
bar(func() string { return "bar" })

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

func BenchmarkDisabledLazy(b *testing.B) {
debug := Debug("something")
for i := 0; i < b.N; i++ {
debug(func() string { return "lazy" })
}
}

func BenchmarkNonMatch(b *testing.B) {
debug := Debug("something")
Enable("nonmatch")
for i := 0; i < b.N; i++ {
debug("stuff")
}
}

func BenchmarkLargeNonMatch(b *testing.B) {
debug := Debug("large:not:lazy")

abs, _ := filepath.Abs("./crashes.json")
file := GetFileBytes(abs)

Enable("nonmatch")
for i := 0; i < b.N; i++ {
debug(string(file))
}
}

func BenchmarkLargeLazyNonMatch(b *testing.B) {
debug := Debug("large:lazy")

abs, _ := filepath.Abs("./crashes.json")
file := GetFileBytes(abs)

Enable("nonmatch")
for i := 0; i < b.N; i++ {
debug(func() string {
return string(file)
})
}
}

func BenchmarkLargeLazyMatch(b *testing.B) {
debug := Debug("large:lazy")

abs, _ := filepath.Abs("./crashes.json")
file := GetFileBytes(abs)

Enable("large:lazy")
for i := 0; i < b.N; i++ {
debug(func() string {
return string(file)
})
}
}

func GetFileBytes(filename string) []byte {
file, err := os.Open(filename)
// if we os.Open returns an error then handle it
if err != nil {
fmt.Println(err)
}
// defer the closing of our jsonFile so that we can parse it later on
defer file.Close()
bytes, _ := ioutil.ReadAll(file)

return bytes
}
28 changes: 28 additions & 0 deletions example/multiple.lazy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

import (
"time"

. "github.com/visionmedia/go-debug"
)

var a = Debug("multiple:a")
var b = Debug("multiple:b")
var c = Debug("multiple:c")

func work(debug DebugFunction, delay time.Duration) {
for {
debug(func() string { return "doing stuff" })
time.Sleep(delay)
}
}

func main() {
q := make(chan bool)

go work(a, 1000*time.Millisecond)
go work(b, 250*time.Millisecond)
go work(c, 100*time.Millisecond)

<-q
}
2 changes: 2 additions & 0 deletions scripts/getSomeDataForPefTests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
curl -o crashes.json 'https://data.ny.gov/api/views/xe9x-a24f/rows.json?accessType=DOWNLOAD'