File tree Expand file tree Collapse file tree 1 file changed +59
-0
lines changed
Expand file tree Collapse file tree 1 file changed +59
-0
lines changed Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments