github.com/qioalice/ekago/v3@v3.3.2-0.20221202205325-5c262d586ee4/ekaerr/bench_test.go (about) 1 package ekaerr_test 2 3 import ( 4 "errors" 5 "fmt" 6 "testing" 7 "unsafe" 8 9 "github.com/qioalice/ekago/v3/ekaerr" 10 "github.com/qioalice/ekago/v3/ekalog" 11 "github.com/qioalice/ekago/v3/internal/ekaletter" 12 ) 13 14 //go:noinline 15 func wrapStdErrors(layer int) error { 16 17 if layer == 0 { 18 //goland:noinspection GoErrorStringFormat 19 return errors.New("Something goes wrong") 20 } 21 22 err := wrapStdErrors(layer - 1) 23 if layer&1 == 0 { 24 return fmt.Errorf("wrap: %w, add message", err) 25 } else { 26 return err 27 } 28 } 29 30 //go:noinline 31 func wrapEkagoErr(layer int) *ekaerr.Error { 32 33 if layer == 0 { 34 return ekaerr.IllegalArgument.New("Something goes wrong") 35 } 36 37 err := wrapEkagoErr(layer - 1) 38 if layer&1 == 0 { 39 return err.AddMessage("Add message").Throw() 40 } else { 41 return err.Throw() 42 } 43 } 44 45 //go:noinline 46 func wrapEkagoErrLightweight(layer int) *ekaerr.Error { 47 48 if layer == 0 { 49 return ekaerr.IllegalArgument.LightNew("Something goes wrong") 50 } 51 52 err := wrapEkagoErrLightweight(layer - 1) 53 if layer&1 == 0 { 54 return err.AddMessage("Add message").WithInt("layer", layer+1).Throw() 55 } else { 56 return err.Throw() 57 } 58 } 59 60 func TestFoo(t *testing.T) { 61 62 err := wrapEkagoErr(16) 63 l := ekaletter.BridgeErrorGetLetter(unsafe.Pointer(err)) 64 65 fmt.Printf("Messages len = %d (%d), Fields len = %d (%d), Stacktrace len = %d (%d)\n", 66 len(l.Messages), cap(l.Messages), 67 len(l.Fields), cap(l.Fields), 68 len(l.StackTrace), cap(l.StackTrace), 69 ) 70 71 ekalog.ReplaceEncoder(new(ekalog.CI_JSONEncoder).SetOneDepthLevel(true)) 72 ekalog.Errore("", err) 73 } 74 75 var cases = []struct { 76 layers int 77 }{ 78 {1}, 79 {16}, 80 {32}, 81 {64}, 82 {128}, 83 } 84 85 func BenchmarkWrap(b *testing.B) { 86 for _, tc := range cases { 87 b.Run(fmt.Sprintf("std errors %v layers", tc.layers), func(b *testing.B) { 88 b.ReportAllocs() 89 for n := 0; n < b.N; n++ { 90 _ = wrapStdErrors(tc.layers) 91 } 92 }) 93 94 b.Run(fmt.Sprintf("ekaerr %v layers", tc.layers), func(b *testing.B) { 95 b.ReportAllocs() 96 for n := 0; n < b.N; n++ { 97 err := wrapEkagoErr(tc.layers) 98 ekaerr.ReleaseError(err) 99 } 100 }) 101 102 b.Run(fmt.Sprintf("ekaerr LIGHT %v layers", tc.layers), func(b *testing.B) { 103 b.ReportAllocs() 104 for n := 0; n < b.N; n++ { 105 err := wrapEkagoErrLightweight(tc.layers) 106 ekaerr.ReleaseError(err) 107 } 108 }) 109 } 110 }