From 0c28c0785e1c62cbe3524a918a9a5ffb7d946155 Mon Sep 17 00:00:00 2001 From: faiface Date: Sun, 2 Jul 2017 19:35:56 +0200 Subject: [PATCH] add text benchmarks --- text/text_test.go | 63 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 text/text_test.go diff --git a/text/text_test.go b/text/text_test.go new file mode 100644 index 0000000..6396647 --- /dev/null +++ b/text/text_test.go @@ -0,0 +1,63 @@ +package text_test + +import ( + "fmt" + "math/rand" + "testing" + "unicode" + + "golang.org/x/image/font/basicfont" + "golang.org/x/image/font/gofont/goregular" + + "github.com/faiface/pixel" + "github.com/faiface/pixel/text" + "github.com/golang/freetype/truetype" +) + +func BenchmarkNewAtlas(b *testing.B) { + runeSets := []struct { + name string + set []rune + }{ + {"ASCII", text.ASCII}, + {"Latin", text.RangeTable(unicode.Latin)}, + } + + ttf, _ := truetype.Parse(goregular.TTF) + face := truetype.NewFace(ttf, &truetype.Options{ + Size: 16, + GlyphCacheEntries: 1, + }) + + for _, runeSet := range runeSets { + b.Run(runeSet.name, func(b *testing.B) { + for i := 0; i < b.N; i++ { + _ = text.NewAtlas(face, runeSet.set) + } + }) + } +} + +func BenchmarkTextWrite(b *testing.B) { + runeSet := text.ASCII + atlas := text.NewAtlas(basicfont.Face7x13, runeSet) + + lengths := []int{1, 10, 100, 1000} + chunks := make([][]byte, len(lengths)) + for i := range chunks { + chunk := make([]rune, lengths[i]) + for j := range chunks[i] { + chunk[j] = runeSet[rand.Intn(len(runeSet))] + } + chunks[i] = []byte(string(chunk)) + } + + for _, chunk := range chunks { + b.Run(fmt.Sprintf("%d", len(chunk)), func(b *testing.B) { + txt := text.New(pixel.ZV, atlas) + for i := 0; i < b.N; i++ { + txt.Write(chunk) + } + }) + } +}