From 1362c6d002e6c65da1d78da3e1c3cf8b4a180636 Mon Sep 17 00:00:00 2001 From: Kamil Samigullin Date: Mon, 24 Jun 2019 16:43:16 +0300 Subject: [PATCH] add bench to test overhead --- Makefile | 2 +- README.md | 14 ++++++++++++++ tracer.go | 10 ++++------ tracer_test.go | 13 ++++++++++--- 4 files changed, 29 insertions(+), 10 deletions(-) diff --git a/Makefile b/Makefile index 63af7f4..fbcda2b 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ GOFLAGS = -mod=vendor TIMEOUT = 1s -.DEFAULT_GOAL = test +.DEFAULT_GOAL = test-with-coverage .PHONY: deps diff --git a/README.md b/README.md index 8dbf01c..1c1bc5e 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 @@ -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 diff --git a/tracer.go b/tracer.go index fb6aafd..da417fa 100644 --- a/tracer.go +++ b/tracer.go @@ -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() { diff --git a/tracer_test.go b/tracer_test.go index ca03f24..5acfd1e 100644 --- a/tracer_test.go +++ b/tracer_test.go @@ -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))