Skip to content
This repository was archived by the owner on Dec 1, 2021. It is now read-only.

Commit 1d2e603

Browse files
kardianosdavecheney
authored andcommitted
errors: add a benchmark comparing stack trace performance (#74)
Fixes #72
1 parent cc5fbb7 commit 1d2e603

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

bench_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// +build go1.7
2+
3+
package errors
4+
5+
import (
6+
"fmt"
7+
"testing"
8+
9+
stderrors "errors"
10+
)
11+
12+
func noErrors(at, depth int) error {
13+
if at >= depth {
14+
return stderrors.New("no error")
15+
}
16+
return noErrors(at+1, depth)
17+
}
18+
func yesErrors(at, depth int) error {
19+
if at >= depth {
20+
return New("ye error")
21+
}
22+
return yesErrors(at+1, depth)
23+
}
24+
25+
func BenchmarkErrors(b *testing.B) {
26+
var toperr error
27+
type run struct {
28+
stack int
29+
std bool
30+
}
31+
runs := []run{
32+
{10, false},
33+
{10, true},
34+
{100, false},
35+
{100, true},
36+
{1000, false},
37+
{1000, true},
38+
}
39+
for _, r := range runs {
40+
part := "pkg/errors"
41+
if r.std {
42+
part = "errors"
43+
}
44+
name := fmt.Sprintf("%s-stack-%d", part, r.stack)
45+
b.Run(name, func(b *testing.B) {
46+
var err error
47+
f := yesErrors
48+
if r.std {
49+
f = noErrors
50+
}
51+
b.ReportAllocs()
52+
for i := 0; i < b.N; i++ {
53+
err = f(0, r.stack)
54+
}
55+
b.StopTimer()
56+
toperr = err
57+
})
58+
}
59+
}

0 commit comments

Comments
 (0)