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.
13 "golang.org/x/xerrors"
14 "golang.org/x/xerrors/internal"
19 func (myType) Format(s fmt.State, v rune) {
20 s.Write(bytes.Repeat([]byte("Hi! "), 10))
23 func BenchmarkErrorf(b *testing.B) {
24 err := xerrors.New("foo")
25 // pi := big.NewFloat(3.14) // Something expensive.
27 args := func(a ...interface{}) []interface{} { return a }
28 benchCases := []struct {
33 {"no_format", "msg: %v", args(err)},
34 {"with_format", "failed %d times: %v", args(5, err)},
35 {"method: mytype", "pi: %v", args("myfile.go", myType{}, err)},
36 {"method: number", "pi: %v", args("myfile.go", num, err)},
38 for _, bc := range benchCases {
39 b.Run(bc.name, func(b *testing.B) {
40 b.Run("ExpWithTrace", func(b *testing.B) {
41 for i := 0; i < b.N; i++ {
42 xerrors.Errorf(bc.format, bc.args...)
45 b.Run("ExpNoTrace", func(b *testing.B) {
46 internal.EnableTrace = false
47 defer func() { internal.EnableTrace = true }()
49 for i := 0; i < b.N; i++ {
50 xerrors.Errorf(bc.format, bc.args...)
53 b.Run("Core", func(b *testing.B) {
54 for i := 0; i < b.N; i++ {
55 fmt.Errorf(bc.format, bc.args...)