|
@@ -5,34 +5,23 @@ import (
|
|
"testing"
|
|
"testing"
|
|
)
|
|
)
|
|
|
|
|
|
-// 针对于 New、Wrap 两种构建错误的方式进行性能测试对比,结果如下(堆栈深度为10、100、1000):
|
|
|
|
|
|
+// 针对于 New、Wrap 两种构建错误的方式进行性能测试对比,结果如下(堆栈深度为10、100、1000),golang版本1.22:
|
|
// New
|
|
// New
|
|
// fserr
|
|
// fserr
|
|
-// 3029 ns/op
|
|
|
|
-// 3788 ns/op
|
|
|
|
-// 14480 ns/op
|
|
|
|
-//
|
|
|
|
-// pkgerr
|
|
|
|
-// 2982 ns/op
|
|
|
|
-// 4434 ns/op
|
|
|
|
-// 15177 ns/op
|
|
|
|
-//
|
|
|
|
|
|
+// 801 ns/op
|
|
|
|
+// 1650 ns/op
|
|
|
|
+// 6262 ns/op
|
|
//
|
|
//
|
|
// Wrap
|
|
// Wrap
|
|
// fserr
|
|
// fserr
|
|
-// 41.08 ns/op
|
|
|
|
-// 483.5 ns/op
|
|
|
|
-// 4722 ns/op
|
|
|
|
-//
|
|
|
|
-// pkgerr
|
|
|
|
-// 38.52 ns/op
|
|
|
|
-// 585.9 ns/op
|
|
|
|
-// 5148 ns/op
|
|
|
|
|
|
+// 826.2 ns/op
|
|
|
|
+// 1623 ns/op
|
|
|
|
+// 6201 ns/op
|
|
//
|
|
//
|
|
-// WithCode性能如下:
|
|
|
|
-// 4657 ns/op
|
|
|
|
-// 5143 ns/op
|
|
|
|
-// 14912 ns/op
|
|
|
|
|
|
+// WithCode
|
|
|
|
+// 2077 ns/op
|
|
|
|
+// 2765 ns/op
|
|
|
|
+// 7628 ns/op
|
|
|
|
|
|
func yesErrors(at, depth int, how func() error) error {
|
|
func yesErrors(at, depth int, how func() error) error {
|
|
if at >= depth {
|
|
if at >= depth {
|
|
@@ -45,6 +34,48 @@ func yesErrors(at, depth int, how func() error) error {
|
|
// preventing the compiler from optimising the benchmark functions away.
|
|
// preventing the compiler from optimising the benchmark functions away.
|
|
var GlobalE interface{}
|
|
var GlobalE interface{}
|
|
|
|
|
|
|
|
+func BenchmarkNew(b *testing.B) {
|
|
|
|
+ for _, r := range []int{
|
|
|
|
+ 10,
|
|
|
|
+ 100,
|
|
|
|
+ 1000,
|
|
|
|
+ } {
|
|
|
|
+ name := fmt.Sprintf("fserr-stack-%d", r)
|
|
|
|
+ b.Run(name, func(b *testing.B) {
|
|
|
|
+ var err error
|
|
|
|
+ b.ReportAllocs()
|
|
|
|
+ for i := 0; i < b.N; i++ {
|
|
|
|
+ err = yesErrors(0, r, func() error {
|
|
|
|
+ return New("success")
|
|
|
|
+ })
|
|
|
|
+ }
|
|
|
|
+ b.StopTimer()
|
|
|
|
+ GlobalE = err
|
|
|
|
+ })
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func BenchmarkWrap(b *testing.B) {
|
|
|
|
+ for _, r := range []int{
|
|
|
|
+ 10,
|
|
|
|
+ 100,
|
|
|
|
+ 1000,
|
|
|
|
+ } {
|
|
|
|
+ name := fmt.Sprintf("fserr-stack-%d", r)
|
|
|
|
+ b.Run(name, func(b *testing.B) {
|
|
|
|
+ var err error
|
|
|
|
+ b.ReportAllocs()
|
|
|
|
+ for i := 0; i < b.N; i++ {
|
|
|
|
+ err = yesErrors(0, r, func() error {
|
|
|
|
+ return Wrap(New("origin"), "success")
|
|
|
|
+ })
|
|
|
|
+ }
|
|
|
|
+ b.StopTimer()
|
|
|
|
+ GlobalE = err
|
|
|
|
+ })
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
func BenchmarkWithCode(b *testing.B) {
|
|
func BenchmarkWithCode(b *testing.B) {
|
|
NewOK(ErrDb, "success")
|
|
NewOK(ErrDb, "success")
|
|
for _, r := range []int{
|
|
for _, r := range []int{
|