forked from jbeda/mdreflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbench_test.go
More file actions
65 lines (61 loc) · 1.86 KB
/
Copy pathbench_test.go
File metadata and controls
65 lines (61 loc) · 1.86 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
package mdreflow_test
import (
"fmt"
"math/rand"
"strings"
"testing"
"github.com/jbeda/mdreflow"
)
// benchParagraph builds a single-paragraph document of roughly n words,
// salted with the inline constructs whose no-break spans make width
// measurement non-trivial (code spans, links, multi-space runs). The
// generator is deterministic so allocations and timings are comparable
// across runs.
func benchParagraph(n int) []byte {
rng := rand.New(rand.NewSource(1))
words := strings.Fields("the quick reflow pipeline measures every candidate against its width budget and keeps prose readable without changing how a document renders")
var b strings.Builder
for i := range n {
if i > 0 {
b.WriteByte(' ')
}
switch r := rng.Float64(); {
case r < 0.02:
b.WriteString("`inline code`")
case r < 0.04:
b.WriteString("[a link](https://example.com/path)")
default:
b.WriteString(words[rng.Intn(len(words))])
}
}
b.WriteByte('\n')
return []byte(b.String())
}
// BenchmarkFormat exercises each mode at growing paragraph sizes. The
// width-constrained modes (wrap, sentence+MaxWidth) are the ones with a
// history of superlinear behavior; sentence and para are the controls.
func BenchmarkFormat(b *testing.B) {
modes := []struct {
name string
opts mdreflow.Options
}{
{"sentence", mdreflow.Options{}},
{"para", mdreflow.Options{Mode: mdreflow.ModePara}},
{"wrap80", mdreflow.Options{Mode: mdreflow.ModeWrap, MaxWidth: 80}},
{"sentence-mw80", mdreflow.Options{MaxWidth: 80}},
}
for _, m := range modes {
for _, words := range []int{100, 400, 1600} {
src := benchParagraph(words)
b.Run(fmt.Sprintf("%s/words=%d", m.name, words), func(b *testing.B) {
b.SetBytes(int64(len(src)))
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := mdreflow.Format(src, m.opts); err != nil {
b.Fatal(err)
}
}
})
}
}
}