OSDN Git Service

new repo
[bytom/vapor.git] / vendor / github.com / pkg / errors / bench_test.go
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
19 func yesErrors(at, depth int) error {
20         if at >= depth {
21                 return New("ye error")
22         }
23         return yesErrors(at+1, depth)
24 }
25
26 // GlobalE is an exported global to store the result of benchmark results,
27 // preventing the compiler from optimising the benchmark functions away.
28 var GlobalE error
29
30 func BenchmarkErrors(b *testing.B) {
31         type run struct {
32                 stack int
33                 std   bool
34         }
35         runs := []run{
36                 {10, false},
37                 {10, true},
38                 {100, false},
39                 {100, true},
40                 {1000, false},
41                 {1000, true},
42         }
43         for _, r := range runs {
44                 part := "pkg/errors"
45                 if r.std {
46                         part = "errors"
47                 }
48                 name := fmt.Sprintf("%s-stack-%d", part, r.stack)
49                 b.Run(name, func(b *testing.B) {
50                         var err error
51                         f := yesErrors
52                         if r.std {
53                                 f = noErrors
54                         }
55                         b.ReportAllocs()
56                         for i := 0; i < b.N; i++ {
57                                 err = f(0, r.stack)
58                         }
59                         b.StopTimer()
60                         GlobalE = err
61                 })
62         }
63 }