bench_test.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Copyright 2018 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package errors
  5. import (
  6. "fmt"
  7. "testing"
  8. )
  9. func BenchmarkWrap(b *testing.B) {
  10. err := New("foo")
  11. args := func(a ...interface{}) []interface{} { return a }
  12. benchCases := []struct {
  13. name string
  14. format string
  15. args []interface{}
  16. msg string
  17. err error
  18. }{
  19. {"wrap", "msg: %w", args(err), "wrap", err},
  20. }
  21. for _, bc := range benchCases {
  22. b.Run(bc.name, func(b *testing.B) {
  23. b.Run("WrapTrace", func(b *testing.B) {
  24. b.ReportAllocs()
  25. for i := 0; i < b.N; i++ {
  26. _ = Wrap(bc.err, bc.msg)
  27. }
  28. })
  29. b.Run("WrapNoTrace", func(b *testing.B) {
  30. b.ReportAllocs()
  31. DisableTrace()
  32. defer enableTrace()
  33. for i := 0; i < b.N; i++ {
  34. _ = Wrap(bc.err, bc.msg)
  35. }
  36. })
  37. b.Run("Core", func(b *testing.B) {
  38. b.ReportAllocs()
  39. for i := 0; i < b.N; i++ {
  40. _ = fmt.Errorf(bc.format, bc.args...)
  41. }
  42. })
  43. })
  44. }
  45. }