代码拉取完成,页面将自动刷新
// +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)
}
// GlobalE is an exported global to store the result of benchmark results,
// preventing the compiler from optimising the benchmark functions away.
var GlobalE interface{}
func BenchmarkErrors(b *testing.B) {
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()
GlobalE = err
})
}
}
func BenchmarkStackFormatting(b *testing.B) {
type run struct {
stack int
format string
}
runs := []run{
{10, "%s"},
{10, "%v"},
{10, "%+v"},
{30, "%s"},
{30, "%v"},
{30, "%+v"},
{60, "%s"},
{60, "%v"},
{60, "%+v"},
}
var stackStr string
for _, r := range runs {
name := fmt.Sprintf("%s-stack-%d", r.format, r.stack)
b.Run(name, func(b *testing.B) {
err := yesErrors(0, r.stack)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
stackStr = fmt.Sprintf(r.format, err)
}
b.StopTimer()
})
}
for _, r := range runs {
name := fmt.Sprintf("%s-stacktrace-%d", r.format, r.stack)
b.Run(name, func(b *testing.B) {
err := yesErrors(0, r.stack)
st := err.(*fundamental).stack.StackTrace()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
stackStr = fmt.Sprintf(r.format, st)
}
b.StopTimer()
})
}
GlobalE = stackStr
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。