go-opengl-pixel/text/text_test.go

64 lines
1.3 KiB
Go
Raw Permalink Normal View History

2017-07-02 12:35:56 -05:00
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])
2017-07-02 16:26:26 -05:00
for j := range chunk {
2017-07-02 12:35:56 -05:00
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)
}
})
}
}