...

Source file src/github.com/yuin/goldmark/_benchmark/cmark/goldmark_benchmark.go

Documentation: github.com/yuin/goldmark/_benchmark/cmark

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

View as plain text