|
| 1 | +package benchmark |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "runtime/debug" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/joomcode/errorx" |
| 10 | +) |
| 11 | + |
| 12 | +var errorSink error |
| 13 | + |
| 14 | +func BenchmarkSimpleError10(b *testing.B) { |
| 15 | + for n := 0; n < b.N; n++ { |
| 16 | + errorSink = function0(10, createSimpleError) |
| 17 | + } |
| 18 | + consumeResult(errorSink) |
| 19 | +} |
| 20 | + |
| 21 | +func BenchmarkErrorxError10(b *testing.B) { |
| 22 | + for n := 0; n < b.N; n++ { |
| 23 | + errorSink = function0(10, createSimpleErrorxError) |
| 24 | + } |
| 25 | + consumeResult(errorSink) |
| 26 | +} |
| 27 | + |
| 28 | +func BenchmarkStackTraceErrorxError10(b *testing.B) { |
| 29 | + for n := 0; n < b.N; n++ { |
| 30 | + errorSink = function0(10, createErrorxError) |
| 31 | + } |
| 32 | + consumeResult(errorSink) |
| 33 | +} |
| 34 | + |
| 35 | +func BenchmarkSimpleError100(b *testing.B) { |
| 36 | + for n := 0; n < b.N; n++ { |
| 37 | + errorSink = function0(100, createSimpleError) |
| 38 | + } |
| 39 | + consumeResult(errorSink) |
| 40 | +} |
| 41 | + |
| 42 | +func BenchmarkErrorxError100(b *testing.B) { |
| 43 | + for n := 0; n < b.N; n++ { |
| 44 | + errorSink = function0(100, createSimpleErrorxError) |
| 45 | + } |
| 46 | + consumeResult(errorSink) |
| 47 | +} |
| 48 | + |
| 49 | +func BenchmarkStackTraceErrorxError100(b *testing.B) { |
| 50 | + for n := 0; n < b.N; n++ { |
| 51 | + errorSink = function0(100, createErrorxError) |
| 52 | + } |
| 53 | + consumeResult(errorSink) |
| 54 | +} |
| 55 | + |
| 56 | +func BenchmarkStackTraceNaiveError100(b *testing.B) { |
| 57 | + for n := 0; n < b.N; n++ { |
| 58 | + errorSink = function0(100, createNaiveError) |
| 59 | + } |
| 60 | + consumeResult(errorSink) |
| 61 | +} |
| 62 | + |
| 63 | +func BenchmarkSimpleErrorPrint100(b *testing.B) { |
| 64 | + for n := 0; n < b.N; n++ { |
| 65 | + err := function0(100, createSimpleError) |
| 66 | + emulateErrorPrint(err) |
| 67 | + errorSink = err |
| 68 | + } |
| 69 | + consumeResult(errorSink) |
| 70 | +} |
| 71 | + |
| 72 | +func BenchmarkErrorxErrorPrint100(b *testing.B) { |
| 73 | + for n := 0; n < b.N; n++ { |
| 74 | + err := function0(100, createSimpleErrorxError) |
| 75 | + emulateErrorPrint(err) |
| 76 | + errorSink = err |
| 77 | + } |
| 78 | + consumeResult(errorSink) |
| 79 | +} |
| 80 | + |
| 81 | +func BenchmarkStackTraceErrorxErrorPrint100(b *testing.B) { |
| 82 | + for n := 0; n < b.N; n++ { |
| 83 | + err := function0(100, createErrorxError) |
| 84 | + emulateErrorPrint(err) |
| 85 | + errorSink = err |
| 86 | + } |
| 87 | + consumeResult(errorSink) |
| 88 | +} |
| 89 | + |
| 90 | +func BenchmarkStackTraceNaiveErrorPrint100(b *testing.B) { |
| 91 | + for n := 0; n < b.N; n++ { |
| 92 | + err := function0(100, createNaiveError) |
| 93 | + emulateErrorPrint(err) |
| 94 | + errorSink = err |
| 95 | + } |
| 96 | + consumeResult(errorSink) |
| 97 | +} |
| 98 | + |
| 99 | +func createSimpleError() error { |
| 100 | + return errors.New("benchmark") |
| 101 | +} |
| 102 | + |
| 103 | +var ( |
| 104 | + Errors = errorx.NewNamespace("errorx.benchmark") |
| 105 | + NoStackTraceError = Errors.NewType("no_stack_trace").ApplyModifiers(errorx.TypeModifierOmitStackTrace) |
| 106 | + StackTraceError = Errors.NewType("stack_trace") |
| 107 | +) |
| 108 | + |
| 109 | +func createSimpleErrorxError() error { |
| 110 | + return NoStackTraceError.New("benchmark") |
| 111 | +} |
| 112 | + |
| 113 | +func createErrorxError() error { |
| 114 | + return StackTraceError.New("benchmark") |
| 115 | +} |
| 116 | + |
| 117 | +type naiveError struct { |
| 118 | + stack []byte |
| 119 | +} |
| 120 | + |
| 121 | +func (err naiveError) Error() string { |
| 122 | + return fmt.Sprintf("benchmark\n%s", err.stack) |
| 123 | +} |
| 124 | + |
| 125 | +func createNaiveError() error { |
| 126 | + return naiveError{stack: debug.Stack()} |
| 127 | +} |
| 128 | + |
| 129 | +func function0(depth int, generate func() error) error { |
| 130 | + if depth == 0 { |
| 131 | + return generate() |
| 132 | + } |
| 133 | + |
| 134 | + switch depth % 3 { |
| 135 | + case 0: |
| 136 | + return function1(depth-1, generate) |
| 137 | + case 1: |
| 138 | + return function2(depth-1, generate) |
| 139 | + default: |
| 140 | + return function3(depth-1, generate) |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +func function1(depth int, generate func() error) error { |
| 145 | + if depth == 0 { |
| 146 | + return generate() |
| 147 | + } |
| 148 | + |
| 149 | + return function4(depth-1, generate) |
| 150 | +} |
| 151 | + |
| 152 | +func function2(depth int, generate func() error) error { |
| 153 | + if depth == 0 { |
| 154 | + return generate() |
| 155 | + } |
| 156 | + |
| 157 | + return function4(depth-1, generate) |
| 158 | +} |
| 159 | + |
| 160 | +func function3(depth int, generate func() error) error { |
| 161 | + if depth == 0 { |
| 162 | + return generate() |
| 163 | + } |
| 164 | + |
| 165 | + return function4(depth-1, generate) |
| 166 | +} |
| 167 | + |
| 168 | +func function4(depth int, generate func() error) error { |
| 169 | + switch depth { |
| 170 | + case 0: |
| 171 | + return generate() |
| 172 | + default: |
| 173 | + return function0(depth-1, generate) |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +type sinkError struct { |
| 178 | + value int |
| 179 | +} |
| 180 | + |
| 181 | +func (sinkError) Error() string { |
| 182 | + return "" |
| 183 | +} |
| 184 | + |
| 185 | +// Perform error formatting and consume the result to disallow optimizations against output |
| 186 | +func emulateErrorPrint(err error) { |
| 187 | + output := fmt.Sprintf("%+v", err) |
| 188 | + if len(output) > 10000 && output[1000:1004] == "DOOM" { |
| 189 | + panic("this was not supposed to happen") |
| 190 | + } |
| 191 | +} |
| 192 | + |
| 193 | +// Consume error with a possible side effect to disallow optimizations against err |
| 194 | +func consumeResult(err error) { |
| 195 | + if e, ok := err.(sinkError); ok && e.value == 1 { |
| 196 | + panic("this was not supposed to happen") |
| 197 | + } |
| 198 | +} |
| 199 | + |
| 200 | +// A public function to discourage optimizations against errorSink variable |
| 201 | +func ExportSink() error { |
| 202 | + return errorSink |
| 203 | +} |
0 commit comments