-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark_test.go
More file actions
229 lines (197 loc) · 5.25 KB
/
Copy pathbenchmark_test.go
File metadata and controls
229 lines (197 loc) · 5.25 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
package markup
import (
"os"
"path/filepath"
"strings"
"testing"
)
func readBenchdata(b *testing.B, name string) []byte {
b.Helper()
data, err := os.ReadFile(filepath.Join("testdata", name))
if err != nil {
b.Fatalf("reading testdata/%s: %v", name, err)
}
return data
}
// --- Detect benchmarks ---
func BenchmarkDetect(b *testing.B) {
filenames := []string{
"README.md",
"README.adoc",
"README.rst.txt",
"README.textile",
"README.org",
"README.unknown",
}
for b.Loop() {
for _, f := range filenames {
Detect(f)
}
}
}
// --- Markdown benchmarks ---
func BenchmarkMarkdownSmall(b *testing.B) {
r := NewDefaultRegistry()
content := readBenchdata(b, "README.markdown")
b.ResetTimer()
b.ReportAllocs()
for b.Loop() {
_, _ = r.Render("README.md", content)
}
}
func BenchmarkMarkdownMedium(b *testing.B) {
r := NewDefaultRegistry()
content := []byte(`# Project Name
A description of the project with **bold**, *italic*, and ` + "`code`" + ` formatting.
## Installation
` + "```bash" + `
go get github.com/example/project
` + "```" + `
## Usage
| Command | Description |
|---------|-------------|
| build | Build the project |
| test | Run tests |
| lint | Run linters |
## Features
- [x] Feature one
- [x] Feature two
- [ ] Feature three
- [ ] Feature four
## Links
Visit https://example.com for more information.
See [the docs](https://docs.example.com) for API reference.

## License
MIT
`)
b.ResetTimer()
b.ReportAllocs()
for b.Loop() {
_, _ = r.Render("README.md", content)
}
}
func BenchmarkMarkdownLarge(b *testing.B) {
r := NewDefaultRegistry()
// Build a large document with repeated sections.
var sb strings.Builder
sb.WriteString("# Large Document\n\n")
for i := 0; i < 100; i++ {
sb.WriteString("## Section\n\n")
sb.WriteString("Paragraph with **bold** and *italic* and `code` and [links](https://example.com).\n\n")
sb.WriteString("| Col A | Col B | Col C |\n|-------|-------|-------|\n")
sb.WriteString("| one | two | three |\n| four | five | six |\n\n")
sb.WriteString("```go\nfunc example() {\n\treturn nil\n}\n```\n\n")
sb.WriteString("- item 1\n- item 2\n- item 3\n\n")
}
content := []byte(sb.String())
b.ResetTimer()
b.ReportAllocs()
for b.Loop() {
_, _ = r.Render("README.md", content)
}
}
// --- Org-mode benchmarks ---
func BenchmarkOrgSmall(b *testing.B) {
r := NewDefaultRegistry()
content := []byte("* Hello\n\nSome text with *bold* and /italic/.\n")
b.ResetTimer()
b.ReportAllocs()
for b.Loop() {
_, _ = r.Render("README.org", content)
}
}
func BenchmarkOrgFixture(b *testing.B) {
r := NewDefaultRegistry()
content := readBenchdata(b, "README.org")
b.ResetTimer()
b.ReportAllocs()
for b.Loop() {
_, _ = r.Render("README.org", content)
}
}
func BenchmarkOrgLarge(b *testing.B) {
r := NewDefaultRegistry()
var sb strings.Builder
sb.WriteString("#+TITLE: Large Document\n\n")
for i := 0; i < 100; i++ {
sb.WriteString("* Section\n\n")
sb.WriteString("Paragraph with *bold* and /italic/ and =code= and [[https://example.com][link]].\n\n")
sb.WriteString("| Col A | Col B | Col C |\n|-------+-------+-------|\n")
sb.WriteString("| one | two | three |\n| four | five | six |\n\n")
sb.WriteString("#+begin_src go\nfunc example() {\n\treturn nil\n}\n#+end_src\n\n")
sb.WriteString("- item 1\n- item 2\n- item 3\n\n")
}
content := []byte(sb.String())
b.ResetTimer()
b.ReportAllocs()
for b.Loop() {
_, _ = r.Render("README.org", content)
}
}
// --- External renderer benchmarks ---
func BenchmarkRSTFixture(b *testing.B) {
r := NewDefaultRegistry()
if !r.Supported("README.rst") {
b.Skip("rst2html not installed")
}
content := readBenchdata(b, "README.rst")
b.ResetTimer()
b.ReportAllocs()
for b.Loop() {
_, _ = r.Render("README.rst", content)
}
}
func BenchmarkAsciiDocFixture(b *testing.B) {
r := NewDefaultRegistry()
if !r.Supported("README.adoc") {
b.Skip("asciidoctor not installed")
}
content := readBenchdata(b, "README.asciidoc")
b.ResetTimer()
b.ReportAllocs()
for b.Loop() {
_, _ = r.Render("README.adoc", content)
}
}
func BenchmarkPodFixture(b *testing.B) {
r := NewDefaultRegistry()
if !r.Supported("README.pod") {
b.Skip("pod2html not installed")
}
content := readBenchdata(b, "README.pod")
b.ResetTimer()
b.ReportAllocs()
for b.Loop() {
_, _ = r.Render("README.pod", content)
}
}
// --- Body extraction benchmarks ---
func BenchmarkBodyExtractionRST(b *testing.B) {
r := NewExternalRenderer(ExternalConfig{
BodyPattern: `(?s)<body>\s*(.*?)\s*</body>`,
})
html := `<!DOCTYPE html><html><head><title>Test</title></head><body>
<div class="document">
<h1>Header</h1>
<p>Paragraph with <strong>bold</strong> and <em>italic</em>.</p>
<ul><li>item 1</li><li>item 2</li><li>item 3</li></ul>
</div>
</body></html>`
b.ResetTimer()
b.ReportAllocs()
for b.Loop() {
r.extractBody(html)
}
}
func BenchmarkBodyExtractionPod(b *testing.B) {
r := NewExternalRenderer(ExternalConfig{
BodyPattern: `(?s)<body[^>]*>\s*(.*?)\s*</body>`,
})
html := `<html><body id="pod"><h1>NAME</h1><p>Hello - a test module</p><h2>DESCRIPTION</h2><p>Some long description here with <code>inline code</code> and other formatting.</p></body></html>`
b.ResetTimer()
b.ReportAllocs()
for b.Loop() {
r.extractBody(html)
}
}