Skip to content

Commit

Permalink
add bench to test overhead
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilsk committed Jun 24, 2019
1 parent 90b18da commit 1362c6d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ GOFLAGS = -mod=vendor
TIMEOUT = 1s


.DEFAULT_GOAL = test
.DEFAULT_GOAL = test-with-coverage


.PHONY: deps
Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
[![Build][icon_build]][page_build]
[![Coverage][icon_coverage]][page_coverage]
[![Quality][icon_quality]][page_quality]
[![Documentation][icon_docs]][page_docs]

## 💡 Idea
Expand Down Expand Up @@ -82,6 +83,17 @@ func StoreIntoDatabase(ctx context.Context, data Data) error {
}
```

Output:

```
allocates at call stack: 1, detailed call stack:
call tracer_test.Handle [ca7a87c4-58d0-4fdf-857c-ef49fc3bf271]: 14.038083ms, allocates: 2
checkpoint [serialize]: 1.163587ms
checkpoint [store]: 2.436265ms
call tracer_test.FetchData: 1.192829ms, allocates: 0
call tracer_test.StoreIntoDatabase: 10.428663ms, allocates: 0
```

## 🧩 Integration

This library uses [SemVer](https://semver.org/) for versioning, and it is not
Expand All @@ -101,11 +113,13 @@ made with ❤️ for everyone
[icon_build]: https://travis-ci.org/kamilsk/tracer.svg?branch=master
[icon_coverage]: https://api.codeclimate.com/v1/badges/fb66449d1f5c64542377/test_coverage
[icon_docs]: https://godoc.org/github.com/kamilsk/tracer?status.svg
[icon_quality]: https://goreportcard.com/badge/github.com/kamilsk/tracer

[page_build]: https://travis-ci.org/kamilsk/tracer
[page_coverage]: https://codeclimate.com/github/kamilsk/tracer/test_coverage
[page_docs]: https://godoc.org/github.com/kamilsk/tracer
[page_promo]: https://github.com/kamilsk/tracer
[page_quality]: https://goreportcard.com/report/github.com/kamilsk/tracer

[dep]: https://golang.github.io/dep/
[gomod]: https://github.com/golang/go/wiki/Modules
10 changes: 4 additions & 6 deletions tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,26 +76,24 @@ type Call struct {
labels []string
caller CallerInfo
start, stop time.Time
checkpoints []*Checkpoint
checkpoints []Checkpoint
allocates int
}

func (call *Call) Checkpoint(labels ...string) *Checkpoint {
func (call *Call) Checkpoint(labels ...string) {
if call == nil {
return nil
return
}

var id string
if len(labels) > 0 {
id, labels = labels[0], labels[1:]
}
checkpoint := &Checkpoint{id: id, labels: labels, timestamp: time.Now()}
checkpoint := Checkpoint{id: id, labels: labels, timestamp: time.Now()}
if len(call.checkpoints) == cap(call.checkpoints) {
call.allocates++
}
call.checkpoints = append(call.checkpoints, checkpoint)

return checkpoint
}

func (call *Call) Stop() {
Expand Down
13 changes: 10 additions & 3 deletions tracer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,23 @@ func TestCall_Stop(t *testing.T) {
(&Call{}).Stop()
}

// BenchmarkTracing/silent-12 200000 7755 ns/op 1816 B/op 24 allocs/op
// BenchmarkTracing/full-12 200000 8880 ns/op 3944 B/op 45 allocs/op
// BenchmarkTracing/overhead-12 2000000 616 ns/op 144 B/op 9 allocs/op
// BenchmarkTracing/silent-12 200000 7478 ns/op 2320 B/op 27 allocs/op
// BenchmarkTracing/active-12 200000 9221 ns/op 4448 B/op 48 allocs/op
func BenchmarkTracing(b *testing.B) {
b.Run("overhead", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
traceRoot(context.Background())
}
})
b.Run("silent", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
traceRoot(Inject(context.Background(), make([]*Call, 0, 9)))
}
})
b.Run("full", func(b *testing.B) {
b.Run("active", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
ctx := Inject(context.Background(), make([]*Call, 0, 9))
Expand Down

0 comments on commit 1362c6d

Please sign in to comment.