Skip to content

Commit 034fa90

Browse files
committed
exp/html: add some tokenizer and parser benchmarks.
$GOROOT/src/pkg/exp/html/testdata/go1.html is an execution of the $GOROOT/doc/go1.html template by godoc. Sample numbers on my linux,amd64 desktop: BenchmarkParser 500 4699198 ns/op 16.63 MB/s --- BENCH: BenchmarkParser parse_test.go:409: 1 iterations, 14653 mallocs per iteration parse_test.go:409: 100 iterations, 14651 mallocs per iteration parse_test.go:409: 500 iterations, 14651 mallocs per iteration BenchmarkRawLevelTokenizer 2000 904957 ns/op 86.37 MB/s --- BENCH: BenchmarkRawLevelTokenizer token_test.go:657: 1 iterations, 28 mallocs per iteration token_test.go:657: 100 iterations, 28 mallocs per iteration token_test.go:657: 2000 iterations, 28 mallocs per iteration BenchmarkLowLevelTokenizer 2000 1134300 ns/op 68.91 MB/s --- BENCH: BenchmarkLowLevelTokenizer token_test.go:657: 1 iterations, 41 mallocs per iteration token_test.go:657: 100 iterations, 41 mallocs per iteration token_test.go:657: 2000 iterations, 41 mallocs per iteration BenchmarkHighLevelTokenizer 1000 2096179 ns/op 37.29 MB/s --- BENCH: BenchmarkHighLevelTokenizer token_test.go:657: 1 iterations, 6616 mallocs per iteration token_test.go:657: 100 iterations, 6616 mallocs per iteration token_test.go:657: 1000 iterations, 6616 mallocs per iteration R=rsc CC=andybalholm, golang-dev, r https://golang.org/cl/6257067
1 parent 397b687 commit 034fa90

File tree

3 files changed

+2324
-0
lines changed

3 files changed

+2324
-0
lines changed

src/pkg/exp/html/parse_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ import (
1111
"flag"
1212
"fmt"
1313
"io"
14+
"io/ioutil"
1415
"os"
1516
"path/filepath"
17+
"runtime"
1618
"sort"
1719
"strings"
1820
"testing"
@@ -386,3 +388,23 @@ var renderTestBlacklist = map[string]bool{
386388
// A <plaintext> element can't have anything after it in HTML.
387389
`<table><plaintext><td>`: true,
388390
}
391+
392+
func BenchmarkParser(b *testing.B) {
393+
buf, err := ioutil.ReadFile("testdata/go1.html")
394+
if err != nil {
395+
b.Fatalf("could not read testdata/go1.html: %v", err)
396+
}
397+
b.SetBytes(int64(len(buf)))
398+
runtime.GC()
399+
var ms runtime.MemStats
400+
runtime.ReadMemStats(&ms)
401+
mallocs := ms.Mallocs
402+
b.ResetTimer()
403+
for i := 0; i < b.N; i++ {
404+
Parse(bytes.NewBuffer(buf))
405+
}
406+
b.StopTimer()
407+
runtime.ReadMemStats(&ms)
408+
mallocs = ms.Mallocs - mallocs
409+
b.Logf("%d iterations, %d mallocs per iteration\n", b.N, int(mallocs)/b.N)
410+
}

0 commit comments

Comments
 (0)