Commit eab5bc9a authored by Nigel Tao's avatar Nigel Tao

image/gif: add BenchmarkDecode.

Also add some b.ReportAllocs calls to other image codec benchmarks.

Change-Id: I0f055dc76bffb66329c621a5f1ccd239f0cdd30b
Reviewed-on: https://go-review.googlesource.com/68390Reviewed-by: 's avatarJed Denlea <jed@fastly.com>
Reviewed-by: 's avatarNigel Tao <nigeltao@golang.org>
parent 35483c8e
...@@ -9,6 +9,7 @@ import ( ...@@ -9,6 +9,7 @@ import (
"compress/lzw" "compress/lzw"
"image" "image"
"image/color" "image/color"
"io/ioutil"
"reflect" "reflect"
"strings" "strings"
"testing" "testing"
...@@ -342,3 +343,20 @@ func TestUnexpectedEOF(t *testing.T) { ...@@ -342,3 +343,20 @@ func TestUnexpectedEOF(t *testing.T) {
} }
} }
} }
func BenchmarkDecode(b *testing.B) {
data, err := ioutil.ReadFile("../testdata/video-001.gif")
if err != nil {
b.Fatal(err)
}
cfg, err := DecodeConfig(bytes.NewReader(data))
if err != nil {
b.Fatal(err)
}
b.SetBytes(int64(cfg.Width * cfg.Height))
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
Decode(bytes.NewReader(data))
}
}
...@@ -500,8 +500,6 @@ func TestEncodeCroppedSubImages(t *testing.T) { ...@@ -500,8 +500,6 @@ func TestEncodeCroppedSubImages(t *testing.T) {
} }
func BenchmarkEncode(b *testing.B) { func BenchmarkEncode(b *testing.B) {
b.StopTimer()
bo := image.Rect(0, 0, 640, 480) bo := image.Rect(0, 0, 640, 480)
rnd := rand.New(rand.NewSource(123)) rnd := rand.New(rand.NewSource(123))
...@@ -523,14 +521,14 @@ func BenchmarkEncode(b *testing.B) { ...@@ -523,14 +521,14 @@ func BenchmarkEncode(b *testing.B) {
} }
b.SetBytes(640 * 480 * 4) b.SetBytes(640 * 480 * 4)
b.StartTimer() b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
Encode(ioutil.Discard, img, nil) Encode(ioutil.Discard, img, nil)
} }
} }
func BenchmarkQuantizedEncode(b *testing.B) { func BenchmarkQuantizedEncode(b *testing.B) {
b.StopTimer()
img := image.NewRGBA(image.Rect(0, 0, 640, 480)) img := image.NewRGBA(image.Rect(0, 0, 640, 480))
bo := img.Bounds() bo := img.Bounds()
rnd := rand.New(rand.NewSource(123)) rnd := rand.New(rand.NewSource(123))
...@@ -545,7 +543,8 @@ func BenchmarkQuantizedEncode(b *testing.B) { ...@@ -545,7 +543,8 @@ func BenchmarkQuantizedEncode(b *testing.B) {
} }
} }
b.SetBytes(640 * 480 * 4) b.SetBytes(640 * 480 * 4)
b.StartTimer() b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
Encode(ioutil.Discard, img, nil) Encode(ioutil.Discard, img, nil)
} }
......
...@@ -323,7 +323,6 @@ func TestExtraneousData(t *testing.T) { ...@@ -323,7 +323,6 @@ func TestExtraneousData(t *testing.T) {
} }
func benchmarkDecode(b *testing.B, filename string) { func benchmarkDecode(b *testing.B, filename string) {
b.StopTimer()
data, err := ioutil.ReadFile(filename) data, err := ioutil.ReadFile(filename)
if err != nil { if err != nil {
b.Fatal(err) b.Fatal(err)
...@@ -333,7 +332,8 @@ func benchmarkDecode(b *testing.B, filename string) { ...@@ -333,7 +332,8 @@ func benchmarkDecode(b *testing.B, filename string) {
b.Fatal(err) b.Fatal(err)
} }
b.SetBytes(int64(cfg.Width * cfg.Height * 4)) b.SetBytes(int64(cfg.Width * cfg.Height * 4))
b.StartTimer() b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
Decode(bytes.NewReader(data)) Decode(bytes.NewReader(data))
} }
......
...@@ -243,7 +243,6 @@ func TestEncodeYCbCr(t *testing.T) { ...@@ -243,7 +243,6 @@ func TestEncodeYCbCr(t *testing.T) {
} }
func BenchmarkEncodeRGBA(b *testing.B) { func BenchmarkEncodeRGBA(b *testing.B) {
b.StopTimer()
img := image.NewRGBA(image.Rect(0, 0, 640, 480)) img := image.NewRGBA(image.Rect(0, 0, 640, 480))
bo := img.Bounds() bo := img.Bounds()
rnd := rand.New(rand.NewSource(123)) rnd := rand.New(rand.NewSource(123))
...@@ -258,7 +257,8 @@ func BenchmarkEncodeRGBA(b *testing.B) { ...@@ -258,7 +257,8 @@ func BenchmarkEncodeRGBA(b *testing.B) {
} }
} }
b.SetBytes(640 * 480 * 4) b.SetBytes(640 * 480 * 4)
b.StartTimer() b.ReportAllocs()
b.ResetTimer()
options := &Options{Quality: 90} options := &Options{Quality: 90}
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
Encode(ioutil.Discard, img, options) Encode(ioutil.Discard, img, options)
...@@ -266,7 +266,6 @@ func BenchmarkEncodeRGBA(b *testing.B) { ...@@ -266,7 +266,6 @@ func BenchmarkEncodeRGBA(b *testing.B) {
} }
func BenchmarkEncodeYCbCr(b *testing.B) { func BenchmarkEncodeYCbCr(b *testing.B) {
b.StopTimer()
img := image.NewYCbCr(image.Rect(0, 0, 640, 480), image.YCbCrSubsampleRatio420) img := image.NewYCbCr(image.Rect(0, 0, 640, 480), image.YCbCrSubsampleRatio420)
bo := img.Bounds() bo := img.Bounds()
rnd := rand.New(rand.NewSource(123)) rnd := rand.New(rand.NewSource(123))
...@@ -280,7 +279,8 @@ func BenchmarkEncodeYCbCr(b *testing.B) { ...@@ -280,7 +279,8 @@ func BenchmarkEncodeYCbCr(b *testing.B) {
} }
} }
b.SetBytes(640 * 480 * 3) b.SetBytes(640 * 480 * 3)
b.StartTimer() b.ReportAllocs()
b.ResetTimer()
options := &Options{Quality: 90} options := &Options{Quality: 90}
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
Encode(ioutil.Discard, img, options) Encode(ioutil.Discard, img, options)
......
...@@ -650,20 +650,19 @@ func TestGray8Transparent(t *testing.T) { ...@@ -650,20 +650,19 @@ func TestGray8Transparent(t *testing.T) {
} }
func benchmarkDecode(b *testing.B, filename string, bytesPerPixel int) { func benchmarkDecode(b *testing.B, filename string, bytesPerPixel int) {
b.StopTimer()
data, err := ioutil.ReadFile(filename) data, err := ioutil.ReadFile(filename)
if err != nil { if err != nil {
b.Fatal(err) b.Fatal(err)
} }
s := string(data) cfg, err := DecodeConfig(bytes.NewReader(data))
cfg, err := DecodeConfig(strings.NewReader(s))
if err != nil { if err != nil {
b.Fatal(err) b.Fatal(err)
} }
b.SetBytes(int64(cfg.Width * cfg.Height * bytesPerPixel)) b.SetBytes(int64(cfg.Width * cfg.Height * bytesPerPixel))
b.StartTimer() b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
Decode(strings.NewReader(s)) Decode(bytes.NewReader(data))
} }
} }
......
...@@ -121,10 +121,10 @@ func TestSubImage(t *testing.T) { ...@@ -121,10 +121,10 @@ func TestSubImage(t *testing.T) {
} }
func BenchmarkEncodeGray(b *testing.B) { func BenchmarkEncodeGray(b *testing.B) {
b.StopTimer()
img := image.NewGray(image.Rect(0, 0, 640, 480)) img := image.NewGray(image.Rect(0, 0, 640, 480))
b.SetBytes(640 * 480 * 1) b.SetBytes(640 * 480 * 1)
b.StartTimer() b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
Encode(ioutil.Discard, img) Encode(ioutil.Discard, img)
} }
...@@ -143,20 +143,19 @@ func (p *pool) Put(b *EncoderBuffer) { ...@@ -143,20 +143,19 @@ func (p *pool) Put(b *EncoderBuffer) {
} }
func BenchmarkEncodeGrayWithBufferPool(b *testing.B) { func BenchmarkEncodeGrayWithBufferPool(b *testing.B) {
b.StopTimer()
img := image.NewGray(image.Rect(0, 0, 640, 480)) img := image.NewGray(image.Rect(0, 0, 640, 480))
e := Encoder{ e := Encoder{
BufferPool: &pool{}, BufferPool: &pool{},
} }
b.SetBytes(640 * 480 * 1) b.SetBytes(640 * 480 * 1)
b.StartTimer() b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
e.Encode(ioutil.Discard, img) e.Encode(ioutil.Discard, img)
} }
} }
func BenchmarkEncodeNRGBOpaque(b *testing.B) { func BenchmarkEncodeNRGBOpaque(b *testing.B) {
b.StopTimer()
img := image.NewNRGBA(image.Rect(0, 0, 640, 480)) img := image.NewNRGBA(image.Rect(0, 0, 640, 480))
// Set all pixels to 0xFF alpha to force opaque mode. // Set all pixels to 0xFF alpha to force opaque mode.
bo := img.Bounds() bo := img.Bounds()
...@@ -169,40 +168,40 @@ func BenchmarkEncodeNRGBOpaque(b *testing.B) { ...@@ -169,40 +168,40 @@ func BenchmarkEncodeNRGBOpaque(b *testing.B) {
b.Fatal("expected image to be opaque") b.Fatal("expected image to be opaque")
} }
b.SetBytes(640 * 480 * 4) b.SetBytes(640 * 480 * 4)
b.StartTimer() b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
Encode(ioutil.Discard, img) Encode(ioutil.Discard, img)
} }
} }
func BenchmarkEncodeNRGBA(b *testing.B) { func BenchmarkEncodeNRGBA(b *testing.B) {
b.StopTimer()
img := image.NewNRGBA(image.Rect(0, 0, 640, 480)) img := image.NewNRGBA(image.Rect(0, 0, 640, 480))
if img.Opaque() { if img.Opaque() {
b.Fatal("expected image not to be opaque") b.Fatal("expected image not to be opaque")
} }
b.SetBytes(640 * 480 * 4) b.SetBytes(640 * 480 * 4)
b.StartTimer() b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
Encode(ioutil.Discard, img) Encode(ioutil.Discard, img)
} }
} }
func BenchmarkEncodePaletted(b *testing.B) { func BenchmarkEncodePaletted(b *testing.B) {
b.StopTimer()
img := image.NewPaletted(image.Rect(0, 0, 640, 480), color.Palette{ img := image.NewPaletted(image.Rect(0, 0, 640, 480), color.Palette{
color.RGBA{0, 0, 0, 255}, color.RGBA{0, 0, 0, 255},
color.RGBA{255, 255, 255, 255}, color.RGBA{255, 255, 255, 255},
}) })
b.SetBytes(640 * 480 * 1) b.SetBytes(640 * 480 * 1)
b.StartTimer() b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
Encode(ioutil.Discard, img) Encode(ioutil.Discard, img)
} }
} }
func BenchmarkEncodeRGBOpaque(b *testing.B) { func BenchmarkEncodeRGBOpaque(b *testing.B) {
b.StopTimer()
img := image.NewRGBA(image.Rect(0, 0, 640, 480)) img := image.NewRGBA(image.Rect(0, 0, 640, 480))
// Set all pixels to 0xFF alpha to force opaque mode. // Set all pixels to 0xFF alpha to force opaque mode.
bo := img.Bounds() bo := img.Bounds()
...@@ -215,20 +214,21 @@ func BenchmarkEncodeRGBOpaque(b *testing.B) { ...@@ -215,20 +214,21 @@ func BenchmarkEncodeRGBOpaque(b *testing.B) {
b.Fatal("expected image to be opaque") b.Fatal("expected image to be opaque")
} }
b.SetBytes(640 * 480 * 4) b.SetBytes(640 * 480 * 4)
b.StartTimer() b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
Encode(ioutil.Discard, img) Encode(ioutil.Discard, img)
} }
} }
func BenchmarkEncodeRGBA(b *testing.B) { func BenchmarkEncodeRGBA(b *testing.B) {
b.StopTimer()
img := image.NewRGBA(image.Rect(0, 0, 640, 480)) img := image.NewRGBA(image.Rect(0, 0, 640, 480))
if img.Opaque() { if img.Opaque() {
b.Fatal("expected image not to be opaque") b.Fatal("expected image not to be opaque")
} }
b.SetBytes(640 * 480 * 4) b.SetBytes(640 * 480 * 4)
b.StartTimer() b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
Encode(ioutil.Discard, img) Encode(ioutil.Discard, img)
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment