This repository has been archived by the owner on Dec 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 699
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
errors: add a benchmark comparing stack trace performance (#74)
Fixes #72
- Loading branch information
1 parent
cc5fbb7
commit 1d2e603
Showing
1 changed file
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// +build go1.7 | ||
|
||
package errors | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
stderrors "errors" | ||
) | ||
|
||
func noErrors(at, depth int) error { | ||
if at >= depth { | ||
return stderrors.New("no error") | ||
} | ||
return noErrors(at+1, depth) | ||
} | ||
func yesErrors(at, depth int) error { | ||
if at >= depth { | ||
return New("ye error") | ||
} | ||
return yesErrors(at+1, depth) | ||
} | ||
|
||
func BenchmarkErrors(b *testing.B) { | ||
var toperr error | ||
type run struct { | ||
stack int | ||
std bool | ||
} | ||
runs := []run{ | ||
{10, false}, | ||
{10, true}, | ||
{100, false}, | ||
{100, true}, | ||
{1000, false}, | ||
{1000, true}, | ||
} | ||
for _, r := range runs { | ||
part := "pkg/errors" | ||
if r.std { | ||
part = "errors" | ||
} | ||
name := fmt.Sprintf("%s-stack-%d", part, r.stack) | ||
b.Run(name, func(b *testing.B) { | ||
var err error | ||
f := yesErrors | ||
if r.std { | ||
f = noErrors | ||
} | ||
b.ReportAllocs() | ||
for i := 0; i < b.N; i++ { | ||
err = f(0, r.stack) | ||
} | ||
b.StopTimer() | ||
toperr = err | ||
}) | ||
} | ||
} |