Repository navigation
Expand file tree
/
Copy pathcore_bench_test.go
More file actions
265 lines (228 loc) · 5.01 KB
/
Copy pathcore_bench_test.go
File metadata and controls
265 lines (228 loc) · 5.01 KB
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// SPDX-License-Identifier: EUPL-1.2
// Benchmarks for the Core primitive in core.go.
// Per AX-11 — *Core sits at the centre of every consumer. Each accessor
// gates a subsystem the rest of the framework defers through (Fs,
// Config, Drive, Data, App, IPC, etc.) and is read inline on the hot
// path. New() is one-shot per process but worth gating against
// regressions in the constructor surface.
//
// Run: go test -bench='BenchmarkCore' -benchmem -run='^$' .
package core_test
import (
"io"
. "dappco.re/go"
)
// quietDefault redirects the default logger to io.Discard for the duration
// of a sub-test. Used by LogError / LogWarn benches that otherwise spam
// stderr with formatted log lines.
func quietDefault(b *B) {
prev := Default()
SetDefault(NewLog(LogOptions{Level: LevelError, Output: io.Discard}))
b.Cleanup(func() { SetDefault(prev) })
}
// Sinks defeat compiler DCE.
var (
coreSinkCore *Core
coreSinkOptions *Options
coreSinkApp *App
coreSinkData *Data
coreSinkDrive *Drive
coreSinkFs *Fs
coreSinkConfig *Config
coreSinkErrPanic *ErrorPanic
coreSinkErrLog *ErrorLog
coreSinkIpc *Ipc
coreSinkI18n *I18n
coreSinkEnv string
coreSinkCtx Context
coreSinkResult Result
)
// --- Constructor ---
func BenchmarkCore_New(b *B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
coreSinkCore = New()
}
}
// --- Accessors (the inline hot path) ---
func BenchmarkCore_Options(b *B) {
c := New()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
coreSinkOptions = c.Options()
}
}
func BenchmarkCore_App(b *B) {
c := New()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
coreSinkApp = c.App()
}
}
func BenchmarkCore_Data(b *B) {
c := New()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
coreSinkData = c.Data()
}
}
func BenchmarkCore_Drive(b *B) {
c := New()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
coreSinkDrive = c.Drive()
}
}
func BenchmarkCore_Fs(b *B) {
c := New()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
coreSinkFs = c.Fs()
}
}
func BenchmarkCore_Config(b *B) {
c := New()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
coreSinkConfig = c.Config()
}
}
func BenchmarkCore_Error(b *B) {
c := New()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
coreSinkErrPanic = c.Error()
}
}
func BenchmarkCore_Log(b *B) {
c := New()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
coreSinkErrLog = c.Log()
}
}
func BenchmarkCore_IPC(b *B) {
c := New()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
coreSinkIpc = c.IPC()
}
}
func BenchmarkCore_I18n(b *B) {
c := New()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
coreSinkI18n = c.I18n()
}
}
func BenchmarkCore_Env(b *B) {
c := New()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
coreSinkEnv = c.Env("DIR_HOME")
}
}
func BenchmarkCore_Context(b *B) {
c := New()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
coreSinkCtx = c.Context()
}
}
func BenchmarkCore_Core_Self(b *B) {
c := New()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
coreSinkCore = c.Core()
}
}
// --- IPC convenience aliases ---
func BenchmarkCore_ACTION_NoHandlers(b *B) {
c := New()
msg := ActionServiceStartup{}
b.ReportAllocs()
for i := 0; i < b.N; i++ {
coreSinkResult = c.ACTION(msg)
}
}
// --- LogError / LogWarn ---
func BenchmarkCore_LogError(b *B) {
quietDefault(b)
c := New()
err := NewError("bench.LogError")
b.ReportAllocs()
for i := 0; i < b.N; i++ {
coreSinkResult = c.LogError(err, "bench.op", "synthetic")
}
}
func BenchmarkCore_LogWarn(b *B) {
quietDefault(b)
c := New()
err := NewError("bench.LogWarn")
b.ReportAllocs()
for i := 0; i < b.N; i++ {
coreSinkResult = c.LogWarn(err, "bench.op", "synthetic")
}
}
// --- RegistryOf ---
func BenchmarkCore_RegistryOf_Services(b *B) {
c := New()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
coreSinkResult = c.RegistryOf("services")
}
}
func BenchmarkCore_RegistryOf_Actions(b *B) {
c := New()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
coreSinkResult = c.RegistryOf("actions")
}
}
func BenchmarkCore_RegistryOf_Unknown(b *B) {
c := New()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
coreSinkResult = c.RegistryOf("noexist")
}
}
var coreSinkFeature Feature
func BenchmarkCore_Feature(b *B) {
c := New()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
coreSinkFeature = c.Feature("dark-mode")
}
}
func BenchmarkCore_WithContext(b *B) {
c := New()
ctx := Background()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
coreSinkCore = c.WithContext(ctx)
}
}
func BenchmarkCore_Must(b *B) {
c := New()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
c.Must(nil, "Bench", "no error") // nil err = no panic
}
}
// BenchmarkCore_RunResult and _Run start + shut down a bare Core (no
// services, no commands → CLI returns the empty-success case); they also
// exercise ServiceStartup/ServiceShutdown.
func BenchmarkCore_RunResult(b *B) {
quietDefault(b)
b.ReportAllocs()
for i := 0; i < b.N; i++ {
coreSinkResult = New().RunResult()
}
}
func BenchmarkCore_Run(b *B) {
quietDefault(b)
b.ReportAllocs()
for i := 0; i < b.N; i++ {
New().Run()
}
}