Copied from #20

This commit is contained in:
Yusuke Inuzuka 2019-08-20 18:33:01 +09:00 committed by GitHub
parent 3190eb8348
commit b067a12f6b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -5,53 +5,79 @@ import (
"io/ioutil" "io/ioutil"
"testing" "testing"
gomarkdown "github.com/gomarkdown/markdown"
"github.com/yuin/goldmark" "github.com/yuin/goldmark"
"github.com/yuin/goldmark/renderer/html" "github.com/yuin/goldmark/renderer/html"
"gitlab.com/golang-commonmark/markdown" "gitlab.com/golang-commonmark/markdown"
"gopkg.in/russross/blackfriday.v2" bf1 "github.com/russross/blackfriday"
bf2 "github.com/russross/blackfriday/v2"
) )
func BenchmarkGoldMark(b *testing.B) { func BenchmarkMarkdown(b *testing.B) {
b.ResetTimer() b.Run("Blackfriday-v1", func(b *testing.B) {
source, err := ioutil.ReadFile("_data.md") r := func(src []byte) ([]byte, error) {
if err != nil { out := bf1.MarkdownBasic(src)
panic(err) return out, nil
} }
doBenchmark(b, r)
})
b.Run("Blackfriday-v2", func(b *testing.B) {
r := func(src []byte) ([]byte, error) {
out := bf2.Run(src)
return out, nil
}
doBenchmark(b, r)
})
b.Run("GoldMark", func(b *testing.B) {
markdown := goldmark.New( markdown := goldmark.New(
goldmark.WithRendererOptions(html.WithXHTML(), html.WithUnsafe()), goldmark.WithRendererOptions(html.WithXHTML(), html.WithUnsafe()),
) )
r := func(src []byte) ([]byte, error) {
var out bytes.Buffer var out bytes.Buffer
markdown.Convert([]byte(""), &out) err := markdown.Convert(src, &out)
return out.Bytes(), err
}
doBenchmark(b, r)
})
for i := 0; i < b.N; i++ { b.Run("CommonMark", func(b *testing.B) {
out.Reset()
if err := markdown.Convert(source, &out); err != nil {
panic(err)
}
}
}
func BenchmarkGolangCommonMark(b *testing.B) {
b.ResetTimer()
source, err := ioutil.ReadFile("_data.md")
if err != nil {
panic(err)
}
md := markdown.New(markdown.XHTMLOutput(true)) md := markdown.New(markdown.XHTMLOutput(true))
for i := 0; i < b.N; i++ { r := func(src []byte) ([]byte, error) {
md.RenderToString(source) var out bytes.Buffer
err := md.Render(&out, src)
return out.Bytes(), err
} }
doBenchmark(b, r)
})
b.Run("GoMarkdown", func(b *testing.B) {
r := func(src []byte) ([]byte, error) {
out := gomarkdown.ToHTML(src, nil, nil)
return out, nil
}
doBenchmark(b, r)
})
} }
func BenchmarkBlackFriday(b *testing.B) { // The different frameworks have different APIs. Create an adapter that
b.ResetTimer() // should behave the same in the memory department.
func doBenchmark(b *testing.B, render func(src []byte) ([]byte, error)) {
b.StopTimer()
source, err := ioutil.ReadFile("_data.md") source, err := ioutil.ReadFile("_data.md")
if err != nil { if err != nil {
panic(err) b.Fatal(err)
} }
b.StartTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
blackfriday.Run(source) out, err := render(source)
if err != nil {
b.Fatal(err)
}
if len(out) < 100 {
b.Fatal("No result")
}
} }
} }