Debian Codegraph @

_benchmark/go/benchmark_test.go

raw symbols SCIP
1 package benchmark
2
3 import (
4 "bytes"
5 "io/ioutil"
6 "testing"
7
8 gomarkdown "github.com/gomarkdown/markdown"
9 "github.com/yuin/goldmark"
10 "github.com/yuin/goldmark/renderer/html"
11 "github.com/yuin/goldmark/util"
12 "gitlab.com/golang-commonmark/markdown"
13
14 "github.com/russross/blackfriday/v2"
15
16 "github.com/88250/lute"
17 )
18
19 func BenchmarkMarkdown(b *testing.B) {
20 b.Run("Blackfriday-v2", func(b *testing.B) {
21 r := func(src []byte) ([]byte, error) {
22 out := blackfriday.Run(src)
23 return out, nil
24 }
25 doBenchmark(b, r)
26 })
27
28 b.Run("GoldMark", func(b *testing.B) {
29 markdown := goldmark.New(
30 goldmark.WithRendererOptions(html.WithXHTML(), html.WithUnsafe()),
31 )
32 r := func(src []byte) ([]byte, error) {
33 var out bytes.Buffer
34 err := markdown.Convert(src, &out)
35 return out.Bytes(), err
36 }
37 doBenchmark(b, r)
38 })
39
40 b.Run("CommonMark", func(b *testing.B) {
41 md := markdown.New(markdown.XHTMLOutput(true))
42 r := func(src []byte) ([]byte, error) {
43 var out bytes.Buffer
44 err := md.Render(&out, src)
45 return out.Bytes(), err
46 }
47 doBenchmark(b, r)
48 })
49
50 b.Run("Lute", func(b *testing.B) {
51 luteEngine := lute.New()
52 luteEngine.SetGFMAutoLink(false)
53 luteEngine.SetGFMStrikethrough(false)
54 luteEngine.SetGFMTable(false)
55 luteEngine.SetGFMTaskListItem(false)
56 luteEngine.SetCodeSyntaxHighlight(false)
57 luteEngine.SetSoftBreak2HardBreak(false)
58 luteEngine.SetAutoSpace(false)
59 luteEngine.SetFixTermTypo(false)
60 r := func(src []byte) ([]byte, error) {
61 out := luteEngine.MarkdownStr("Benchmark", util.BytesToReadOnlyString(src))
62 return util.StringToReadOnlyBytes(out), nil
63 }
64 doBenchmark(b, r)
65 })
66
67 b.Run("GoMarkdown", func(b *testing.B) {
68 r := func(src []byte) ([]byte, error) {
69 out := gomarkdown.ToHTML(src, nil, nil)
70 return out, nil
71 }
72 doBenchmark(b, r)
73 })
74
75 }
76
77 // The different frameworks have different APIs. Create an adapter that
78 // should behave the same in the memory department.
79 func doBenchmark(b *testing.B, render func(src []byte) ([]byte, error)) {
80 b.StopTimer()
81 source, err := ioutil.ReadFile("_data.md")
82 if err != nil {
83 b.Fatal(err)
84 }
85 b.StartTimer()
86 for i := 0; i < b.N; i++ {
87 out, err := render(source)
88 if err != nil {
89 b.Fatal(err)
90 }
91 if len(out) < 100 {
92 b.Fatal("No result")
93 }
94 }
95 }