-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark_test.go
More file actions
431 lines (402 loc) · 12 KB
/
benchmark_test.go
File metadata and controls
431 lines (402 loc) · 12 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
package demojify_test
import (
"fmt"
"os"
"path/filepath"
"strings"
"testing"
demojify "github.com/nicholashoule/demojify-sanitize"
)
// benchInput builds a string with n emoji interspersed in plain text.
func benchInput(n int) string {
var b strings.Builder
for i := 0; i < n; i++ {
b.WriteString("build step ")
b.WriteString("\u2705 passed, deploy \U0001F680 done, check \U0001F4CA report\n")
}
return b.String()
}
// buildLargeMarkdown generates a realistic Markdown document of approximately
// the requested byte size. The output mixes headers, prose paragraphs, fenced
// code blocks, tables, bullet lists, links, and scattered emoji -- including
// ZWJ sequences, variation selectors, and skin-tone modifiers -- to simulate
// a complex, real-world document that the library must process efficiently.
func buildLargeMarkdown(targetBytes int) string {
// Reusable fragments that appear in real Markdown documents.
const header = "# Project Status Report\n\n"
const subheader = "## Section %d: Feature Update\n\n"
const prose = "The deployment pipeline completed successfully and all integration " +
"tests passed on the staging environment. Performance metrics remain within " +
"acceptable thresholds across all monitored endpoints. The team reviewed the " +
"architecture decision records and agreed on the proposed changes to the " +
"caching layer.\n\n"
const proseEmoji = "Status: \u2705 passing | Build: \U0001F680 deployed | " +
"Coverage: \U0001F4CA 94% | Alerts: \u26A0\uFE0F none | " +
"Review: \U0001F44D\U0001F3FD approved | " + // skin-tone modifier
"Team: \U0001F468\u200D\U0001F4BB\u200D\U0001F469\u200D\U0001F4BB pair | " + // ZWJ
"Flag: \U0001F1FA\U0001F1F8 | " + // flag sequence
"Heart: \u2764\uFE0F\u200D\U0001F525\n\n" // variation selector + ZWJ
const codeBlock = "```go\nfunc process(ctx context.Context, items []Item) error {\n" +
"\tfor _, item := range items {\n" +
"\t\tif err := validate(item); err != nil {\n" +
"\t\t\treturn fmt.Errorf(\"validate %s: %w\", item.ID, err)\n" +
"\t\t}\n" +
"\t}\n" +
"\treturn nil\n}\n```\n\n"
const table = "| Metric | Value | Status |\n" +
"|--------|-------|--------|\n" +
"| Latency p99 | 142ms | \u2705 |\n" +
"| Error rate | 0.02% | \u2705 |\n" +
"| Throughput | 12,400 req/s | \U0001F680 |\n" +
"| Memory | 1.2 GiB | \u26A0\uFE0F |\n\n"
const bulletList = "- Refactored the authentication middleware \U0001F512\n" +
"- Added retry logic with exponential backoff \u23F3\n" +
"- Updated API documentation for v2 endpoints \U0001F4DD\n" +
"- Resolved flaky test in CI \U0001F527\n" +
"- Deployed canary to 5% of traffic \U0001F6A6\n\n"
const link = "[Architecture Decision Records](https://example.com/adr) | " +
"[Runbook](https://example.com/runbook) | " +
"[Dashboard](https://example.com/dashboard)\n\n"
const whitespaceMessy = "Some text with extra spaces scattered " +
"throughout the document.\n\n\n\n" +
"And multiple blank lines above.\n\n"
var b strings.Builder
b.Grow(targetBytes + 4096) // pre-allocate to reduce resizing
b.WriteString(header)
section := 0
for b.Len() < targetBytes {
section++
fmt.Fprintf(&b, subheader, section)
b.WriteString(prose)
b.WriteString(proseEmoji)
b.WriteString(codeBlock)
b.WriteString(table)
b.WriteString(bulletList)
b.WriteString(link)
b.WriteString(whitespaceMessy)
}
return b.String()
}
// largeSizes defines the document sizes used by scaled benchmarks.
// Each entry is a human-readable label paired with a target byte count.
var largeSizes = []struct {
name string
bytes int
}{
{"10KB", 10 * 1024},
{"100KB", 100 * 1024},
{"1MB", 1024 * 1024},
}
// ---------- Original benchmarks (unchanged) ----------
func BenchmarkDemojify(b *testing.B) {
input := benchInput(100)
b.ResetTimer()
for i := 0; i < b.N; i++ {
demojify.Demojify(input)
}
}
func BenchmarkContainsEmoji(b *testing.B) {
input := benchInput(100)
b.ResetTimer()
for i := 0; i < b.N; i++ {
demojify.ContainsEmoji(input)
}
}
func BenchmarkNormalize(b *testing.B) {
input := strings.Repeat("Hello World \n\n\n\nMore text here\n", 100)
b.ResetTimer()
for i := 0; i < b.N; i++ {
demojify.Normalize(input)
}
}
func BenchmarkReplace(b *testing.B) {
input := benchInput(100)
repl := demojify.DefaultReplacements()
b.ResetTimer()
for i := 0; i < b.N; i++ {
demojify.Replace(input, repl)
}
}
func BenchmarkReplaceCount(b *testing.B) {
input := benchInput(100)
repl := demojify.DefaultReplacements()
b.ResetTimer()
for i := 0; i < b.N; i++ {
demojify.ReplaceCount(input, repl)
}
}
func BenchmarkSanitize(b *testing.B) {
input := benchInput(100)
opts := demojify.DefaultOptions()
b.ResetTimer()
for i := 0; i < b.N; i++ {
demojify.Sanitize(input, opts)
}
}
func BenchmarkScanDir(b *testing.B) {
dir := b.TempDir()
// Create 10 files with emoji content.
for i := 0; i < 10; i++ {
name := "file" + strings.Repeat("x", i) + ".md"
content := benchInput(10)
if err := os.WriteFile(filepath.Join(dir, name), []byte(content), 0o644); err != nil {
b.Fatalf("write %s: %v", name, err)
}
}
cfg := demojify.DefaultScanConfig()
cfg.Root = dir
cfg.ExemptSuffixes = nil // don't skip anything
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := demojify.ScanDir(cfg)
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkFindAll(b *testing.B) {
input := benchInput(100)
b.ResetTimer()
for i := 0; i < b.N; i++ {
demojify.FindAll(input)
}
}
// ---------- Large-document benchmarks ----------
//
// Each benchmark generates a realistic Markdown document at multiple sizes
// (10 KB, 100 KB, 1 MB) and reports bytes-per-op so throughput can be
// compared across runs.
func BenchmarkLargeDemojify(b *testing.B) {
for _, sz := range largeSizes {
input := buildLargeMarkdown(sz.bytes)
b.Run(sz.name, func(b *testing.B) {
b.SetBytes(int64(len(input)))
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
demojify.Demojify(input)
}
})
}
}
func BenchmarkLargeContainsEmoji(b *testing.B) {
for _, sz := range largeSizes {
input := buildLargeMarkdown(sz.bytes)
b.Run(sz.name, func(b *testing.B) {
b.SetBytes(int64(len(input)))
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
demojify.ContainsEmoji(input)
}
})
}
}
func BenchmarkLargeNormalize(b *testing.B) {
for _, sz := range largeSizes {
input := buildLargeMarkdown(sz.bytes)
b.Run(sz.name, func(b *testing.B) {
b.SetBytes(int64(len(input)))
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
demojify.Normalize(input)
}
})
}
}
func BenchmarkLargeReplace(b *testing.B) {
repl := demojify.DefaultReplacements()
for _, sz := range largeSizes {
input := buildLargeMarkdown(sz.bytes)
b.Run(sz.name, func(b *testing.B) {
b.SetBytes(int64(len(input)))
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
demojify.Replace(input, repl)
}
})
}
}
func BenchmarkLargeReplaceCount(b *testing.B) {
repl := demojify.DefaultReplacements()
for _, sz := range largeSizes {
input := buildLargeMarkdown(sz.bytes)
b.Run(sz.name, func(b *testing.B) {
b.SetBytes(int64(len(input)))
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
demojify.ReplaceCount(input, repl)
}
})
}
}
func BenchmarkLargeSanitize(b *testing.B) {
opts := demojify.DefaultOptions()
for _, sz := range largeSizes {
input := buildLargeMarkdown(sz.bytes)
b.Run(sz.name, func(b *testing.B) {
b.SetBytes(int64(len(input)))
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
demojify.Sanitize(input, opts)
}
})
}
}
func BenchmarkLargeFindAll(b *testing.B) {
for _, sz := range largeSizes {
input := buildLargeMarkdown(sz.bytes)
b.Run(sz.name, func(b *testing.B) {
b.SetBytes(int64(len(input)))
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
demojify.FindAll(input)
}
})
}
}
func BenchmarkLargeFindAllMapped(b *testing.B) {
repl := demojify.DefaultReplacements()
for _, sz := range largeSizes {
input := buildLargeMarkdown(sz.bytes)
b.Run(sz.name, func(b *testing.B) {
b.SetBytes(int64(len(input)))
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
demojify.FindAllMapped(input, repl)
}
})
}
}
// BenchmarkLargeScanDir creates a directory tree of large Markdown files and
// benchmarks the full scan pipeline at different total-data sizes.
func BenchmarkLargeScanDir(b *testing.B) {
for _, sz := range largeSizes {
b.Run(sz.name, func(b *testing.B) {
dir := b.TempDir()
// Distribute the target size across 5 files to simulate a
// realistic directory with multiple documents.
const fileCount = 5
perFile := sz.bytes / fileCount
totalWritten := 0
for i := 0; i < fileCount; i++ {
content := buildLargeMarkdown(perFile)
totalWritten += len(content)
name := fmt.Sprintf("doc_%02d.md", i)
if err := os.WriteFile(filepath.Join(dir, name), []byte(content), 0o644); err != nil {
b.Fatalf("write %s: %v", name, err)
}
}
cfg := demojify.DefaultScanConfig()
cfg.Root = dir
cfg.ExemptSuffixes = nil
b.SetBytes(int64(totalWritten))
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
if _, err := demojify.ScanDir(cfg); err != nil {
b.Fatal(err)
}
}
})
}
}
// BenchmarkLargeScanFile benchmarks scanning a single large file.
func BenchmarkLargeScanFile(b *testing.B) {
opts := demojify.DefaultOptions()
for _, sz := range largeSizes {
b.Run(sz.name, func(b *testing.B) {
dir := b.TempDir()
content := buildLargeMarkdown(sz.bytes)
path := filepath.Join(dir, "large.md")
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
b.Fatalf("write: %v", err)
}
b.SetBytes(int64(len(content)))
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
if _, err := demojify.ScanFile(path, opts); err != nil {
b.Fatal(err)
}
}
})
}
}
// BenchmarkLargeFindMatchesInFile benchmarks per-occurrence match extraction
// from a single large file.
func BenchmarkLargeFindMatchesInFile(b *testing.B) {
repl := demojify.DefaultReplacements()
for _, sz := range largeSizes {
b.Run(sz.name, func(b *testing.B) {
dir := b.TempDir()
content := buildLargeMarkdown(sz.bytes)
path := filepath.Join(dir, "large.md")
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
b.Fatalf("write: %v", err)
}
b.SetBytes(int64(len(content)))
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
if _, err := demojify.FindMatchesInFile(path, repl); err != nil {
b.Fatal(err)
}
}
})
}
}
// BenchmarkLargeSanitizeFile benchmarks the full sanitize-and-write-back path
// on a single large file. Each iteration re-creates the file so the write
// path is exercised every time.
func BenchmarkLargeSanitizeFile(b *testing.B) {
opts := demojify.DefaultOptions()
for _, sz := range largeSizes {
b.Run(sz.name, func(b *testing.B) {
dir := b.TempDir()
content := buildLargeMarkdown(sz.bytes)
path := filepath.Join(dir, "large.md")
b.SetBytes(int64(len(content)))
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
// Re-write the dirty file before each sanitize pass.
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
b.Fatalf("write: %v", err)
}
if _, err := demojify.SanitizeFile(path, opts); err != nil {
b.Fatal(err)
}
}
})
}
}
// BenchmarkLargeReplaceFile benchmarks file-level emoji substitution on a
// large file. Each iteration re-creates the dirty file.
func BenchmarkLargeReplaceFile(b *testing.B) {
repl := demojify.DefaultReplacements()
for _, sz := range largeSizes {
b.Run(sz.name, func(b *testing.B) {
dir := b.TempDir()
content := buildLargeMarkdown(sz.bytes)
path := filepath.Join(dir, "large.md")
b.SetBytes(int64(len(content)))
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
b.Fatalf("write: %v", err)
}
if _, err := demojify.ReplaceFile(path, repl); err != nil {
b.Fatal(err)
}
}
})
}
}