...

Source file src/github.com/golang/freetype/freetype_test.go

Documentation: github.com/golang/freetype

     1  // Copyright 2012 The Freetype-Go Authors. All rights reserved.
     2  // Use of this source code is governed by your choice of either the
     3  // FreeType License or the GNU General Public License version 2 (or
     4  // any later version), both of which can be found in the LICENSE file.
     5  
     6  package freetype
     7  
     8  import (
     9  	"image"
    10  	"image/draw"
    11  	"io/ioutil"
    12  	"runtime"
    13  	"strings"
    14  	"testing"
    15  )
    16  
    17  func BenchmarkDrawString(b *testing.B) {
    18  	data, err := ioutil.ReadFile("licenses/gpl.txt")
    19  	if err != nil {
    20  		b.Fatal(err)
    21  	}
    22  	lines := strings.Split(string(data), "\n")
    23  
    24  	data, err = ioutil.ReadFile("testdata/luxisr.ttf")
    25  	if err != nil {
    26  		b.Fatal(err)
    27  	}
    28  	f, err := ParseFont(data)
    29  	if err != nil {
    30  		b.Fatal(err)
    31  	}
    32  
    33  	dst := image.NewRGBA(image.Rect(0, 0, 800, 600))
    34  	draw.Draw(dst, dst.Bounds(), image.White, image.ZP, draw.Src)
    35  
    36  	c := NewContext()
    37  	c.SetDst(dst)
    38  	c.SetClip(dst.Bounds())
    39  	c.SetSrc(image.Black)
    40  	c.SetFont(f)
    41  
    42  	var ms runtime.MemStats
    43  	runtime.ReadMemStats(&ms)
    44  	mallocs := ms.Mallocs
    45  
    46  	b.ResetTimer()
    47  	for i := 0; i < b.N; i++ {
    48  		for j, line := range lines {
    49  			_, err := c.DrawString(line, Pt(0, (j*16)%600))
    50  			if err != nil {
    51  				b.Fatal(err)
    52  			}
    53  		}
    54  	}
    55  	b.StopTimer()
    56  	runtime.ReadMemStats(&ms)
    57  	mallocs = ms.Mallocs - mallocs
    58  	b.Logf("%d iterations, %d mallocs per iteration\n", b.N, int(mallocs)/b.N)
    59  }
    60  

View as plain text