File: goldmark_benchmark.go

package info (click to toggle)
golang-github-yuin-goldmark 1.7.13-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,692 kB
  • sloc: ansic: 76; makefile: 37
file content (45 lines) | stat: -rw-r--r-- 906 bytes parent folder | download | duplicates (4)
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
package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"os"
	"strconv"
	"time"

	"github.com/yuin/goldmark"
	"github.com/yuin/goldmark/renderer/html"
)

func main() {
	n := 50
	file := "_data.md"
	if len(os.Args) > 1 {
		n, _ = strconv.Atoi(os.Args[1])
	}
	if len(os.Args) > 2 {
		file = os.Args[2]
	}
	source, err := ioutil.ReadFile(file)
	if err != nil {
		panic(err)
	}
	markdown := goldmark.New(goldmark.WithRendererOptions(html.WithXHTML(), html.WithUnsafe()))
	var out bytes.Buffer
	markdown.Convert([]byte(""), &out)

	sum := time.Duration(0)
	for i := 0; i < n; i++ {
		start := time.Now()
		out.Reset()
		if err := markdown.Convert(source, &out); err != nil {
			panic(err)
		}
		sum += time.Since(start)
	}
	fmt.Printf("------- goldmark -------\n")
	fmt.Printf("file: %s\n", file)
	fmt.Printf("iteration: %d\n", n)
	fmt.Printf("average: %.10f sec\n", float64((int64(sum)/int64(n)))/1000000000.0)
}