File: stacktrace_benchmark_test.go

package info (click to toggle)
golang-github-joomcode-errorx 1.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 280 kB
  • sloc: makefile: 2
file content (203 lines) | stat: -rw-r--r-- 4,232 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package benchmark

import (
	"errors"
	"fmt"
	"runtime/debug"
	"testing"

	"github.com/joomcode/errorx"
)

var errorSink error

func BenchmarkSimpleError10(b *testing.B) {
	for n := 0; n < b.N; n++ {
		errorSink = function0(10, createSimpleError)
	}
	consumeResult(errorSink)
}

func BenchmarkErrorxError10(b *testing.B) {
	for n := 0; n < b.N; n++ {
		errorSink = function0(10, createSimpleErrorxError)
	}
	consumeResult(errorSink)
}

func BenchmarkStackTraceErrorxError10(b *testing.B) {
	for n := 0; n < b.N; n++ {
		errorSink = function0(10, createErrorxError)
	}
	consumeResult(errorSink)
}

func BenchmarkSimpleError100(b *testing.B) {
	for n := 0; n < b.N; n++ {
		errorSink = function0(100, createSimpleError)
	}
	consumeResult(errorSink)
}

func BenchmarkErrorxError100(b *testing.B) {
	for n := 0; n < b.N; n++ {
		errorSink = function0(100, createSimpleErrorxError)
	}
	consumeResult(errorSink)
}

func BenchmarkStackTraceErrorxError100(b *testing.B) {
	for n := 0; n < b.N; n++ {
		errorSink = function0(100, createErrorxError)
	}
	consumeResult(errorSink)
}

func BenchmarkStackTraceNaiveError100(b *testing.B) {
	for n := 0; n < b.N; n++ {
		errorSink = function0(100, createNaiveError)
	}
	consumeResult(errorSink)
}

func BenchmarkSimpleErrorPrint100(b *testing.B) {
	for n := 0; n < b.N; n++ {
		err := function0(100, createSimpleError)
		emulateErrorPrint(err)
		errorSink = err
	}
	consumeResult(errorSink)
}

func BenchmarkErrorxErrorPrint100(b *testing.B) {
	for n := 0; n < b.N; n++ {
		err := function0(100, createSimpleErrorxError)
		emulateErrorPrint(err)
		errorSink = err
	}
	consumeResult(errorSink)
}

func BenchmarkStackTraceErrorxErrorPrint100(b *testing.B) {
	for n := 0; n < b.N; n++ {
		err := function0(100, createErrorxError)
		emulateErrorPrint(err)
		errorSink = err
	}
	consumeResult(errorSink)
}

func BenchmarkStackTraceNaiveErrorPrint100(b *testing.B) {
	for n := 0; n < b.N; n++ {
		err := function0(100, createNaiveError)
		emulateErrorPrint(err)
		errorSink = err
	}
	consumeResult(errorSink)
}

func createSimpleError() error {
	return errors.New("benchmark")
}

var (
	Errors            = errorx.NewNamespace("errorx.benchmark")
	NoStackTraceError = Errors.NewType("no_stack_trace").ApplyModifiers(errorx.TypeModifierOmitStackTrace)
	StackTraceError   = Errors.NewType("stack_trace")
)

func createSimpleErrorxError() error {
	return NoStackTraceError.New("benchmark")
}

func createErrorxError() error {
	return StackTraceError.New("benchmark")
}

type naiveError struct {
	stack []byte
}

func (err naiveError) Error() string {
	return fmt.Sprintf("benchmark\n%s", err.stack)
}

func createNaiveError() error {
	return naiveError{stack: debug.Stack()}
}

func function0(depth int, generate func() error) error {
	if depth == 0 {
		return generate()
	}

	switch depth % 3 {
	case 0:
		return function1(depth-1, generate)
	case 1:
		return function2(depth-1, generate)
	default:
		return function3(depth-1, generate)
	}
}

func function1(depth int, generate func() error) error {
	if depth == 0 {
		return generate()
	}

	return function4(depth-1, generate)
}

func function2(depth int, generate func() error) error {
	if depth == 0 {
		return generate()
	}

	return function4(depth-1, generate)
}

func function3(depth int, generate func() error) error {
	if depth == 0 {
		return generate()
	}

	return function4(depth-1, generate)
}

func function4(depth int, generate func() error) error {
	switch depth {
	case 0:
		return generate()
	default:
		return function0(depth-1, generate)
	}
}

type sinkError struct {
	value int
}

func (sinkError) Error() string {
	return ""
}

// Perform error formatting and consume the result to disallow optimizations against output
func emulateErrorPrint(err error) {
	output := fmt.Sprintf("%+v", err)
	if len(output) > 10000 && output[1000:1004] == "DOOM" {
		panic("this was not supposed to happen")
	}
}

// Consume error with a possible side effect to disallow optimizations against err
func consumeResult(err error) {
	if e, ok := err.(sinkError); ok && e.value == 1 {
		panic("this was not supposed to happen")
	}
}

// A public function to discourage optimizations against errorSink variable
func ExportSink() error {
	return errorSink
}