bench_test.go 906 B

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