Debian Codegraph @

_benchmark/cmark/goldmark_benchmark.go

raw symbols SCIP
1 package main
2
3 import (
4 "bytes"
5 "fmt"
6 "io/ioutil"
7 "log"
8 "os"
9 "runtime/pprof"
10 "strconv"
11 "time"
12
13 "github.com/yuin/goldmark"
14 "github.com/yuin/goldmark/renderer/html"
15 )
16
17 func main() {
18 n := 50
19 file := "_data.md"
20 if len(os.Args) > 1 {
21 n, _ = strconv.Atoi(os.Args[1])
22 }
23 if len(os.Args) > 2 {
24 file = os.Args[2]
25 }
26 if len(os.Args) > 3 {
27 f, err := os.Create(os.Args[3])
28 if err != nil {
29 log.Fatal(err)
30 }
31 defer f.Close()
32
33 if err := pprof.StartCPUProfile(f); err != nil {
34 log.Fatal(err)
35 }
36 defer pprof.StopCPUProfile()
37 }
38 source, err := ioutil.ReadFile(file)
39 if err != nil {
40 panic(err)
41 }
42 markdown := goldmark.New(goldmark.WithRendererOptions(html.WithXHTML(), html.WithUnsafe()))
43 var out bytes.Buffer
44 markdown.Convert([]byte(""), &out)
45
46 sum := time.Duration(0)
47 for i := 0; i < n; i++ {
48 start := time.Now()
49 out.Reset()
50 if err := markdown.Convert(source, &out); err != nil {
51 panic(err)
52 }
53 sum += time.Since(start)
54 }
55 fmt.Printf("------- goldmark -------\n")
56 fmt.Printf("file: %s\n", file)
57 fmt.Printf("iteration: %d\n", n)
58 fmt.Printf("average: %.10f sec\n", float64((int64(sum)/int64(n)))/1000000000.0)
59 }