go.image/tiff: add a benchmark for the encoder

The idea is to see whether it is worthwhile to implement special cases.

On my Mac Book Pro:

BenchmarkEncode	    1000	   1689656 ns/op	  36.69 MB/s

R=nigeltao, minux.ma
CC=golang-dev
https://golang.org/cl/6278050
This commit is contained in:
Benny Siegert 2012-06-07 09:41:24 +10:00 committed by Nigel Tao
parent 2b631034c7
commit de792da79c

View File

@ -6,6 +6,8 @@ package tiff
import ( import (
"bytes" "bytes"
"image"
"io/ioutil"
"os" "os"
"testing" "testing"
) )
@ -15,18 +17,21 @@ var roundtripTests = []string{
"bw-packbits.tiff", "bw-packbits.tiff",
} }
func TestRoundtrip(t *testing.T) { func openImage(filename string) (image.Image, error) {
for _, filename := range roundtripTests {
f, err := os.Open(testdataDir + filename) f, err := os.Open(testdataDir + filename)
if err != nil { if err != nil {
t.Fatal(err) return nil, err
} }
defer f.Close() defer f.Close()
img, err := Decode(f) return Decode(f)
}
func TestRoundtrip(t *testing.T) {
for _, filename := range roundtripTests {
img, err := openImage(filename)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
out := new(bytes.Buffer) out := new(bytes.Buffer)
err = Encode(out, img) err = Encode(out, img)
if err != nil { if err != nil {
@ -40,3 +45,17 @@ func TestRoundtrip(t *testing.T) {
compare(t, img, img2) compare(t, img, img2)
} }
} }
// BenchmarkEncode benchmarks the encoding of an image.
func BenchmarkEncode(b *testing.B) {
img, err := openImage("video-001.tiff")
if err != nil {
b.Fatal(err)
}
s := img.Bounds().Size()
b.SetBytes(int64(s.X * s.Y * 4))
b.ResetTimer()
for i := 0; i < b.N; i++ {
Encode(ioutil.Discard, img)
}
}