]> Cypherpunks repositories - gostls13.git/commitdiff
image/gif: add BenchmarkDecode.
authorNigel Tao <nigeltao@golang.org>
Thu, 5 Oct 2017 05:52:04 +0000 (16:52 +1100)
committerNigel Tao <nigeltao@golang.org>
Sat, 7 Oct 2017 05:23:42 +0000 (05:23 +0000)
Also add some b.ReportAllocs calls to other image codec benchmarks.

Change-Id: I0f055dc76bffb66329c621a5f1ccd239f0cdd30b
Reviewed-on: https://go-review.googlesource.com/68390
Reviewed-by: Jed Denlea <jed@fastly.com>
Reviewed-by: Nigel Tao <nigeltao@golang.org>
src/image/gif/reader_test.go
src/image/gif/writer_test.go
src/image/jpeg/reader_test.go
src/image/jpeg/writer_test.go
src/image/png/reader_test.go
src/image/png/writer_test.go

index 51c64b7328f2ec09da20e7b54d1af0591c28aae7..4b83c9662af3efd7abf832798d89799f8967bb36 100644 (file)
@@ -9,6 +9,7 @@ import (
        "compress/lzw"
        "image"
        "image/color"
+       "io/ioutil"
        "reflect"
        "strings"
        "testing"
@@ -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))
+       }
+}
index 1bba9b8ece58290bd2ad76b72a47f2f252b8180d..eb17cf28ed0863008ede5219a967ac6260c1d8ae 100644 (file)
@@ -500,8 +500,6 @@ func TestEncodeCroppedSubImages(t *testing.T) {
 }
 
 func BenchmarkEncode(b *testing.B) {
-       b.StopTimer()
-
        bo := image.Rect(0, 0, 640, 480)
        rnd := rand.New(rand.NewSource(123))
 
@@ -523,14 +521,14 @@ func BenchmarkEncode(b *testing.B) {
        }
 
        b.SetBytes(640 * 480 * 4)
-       b.StartTimer()
+       b.ReportAllocs()
+       b.ResetTimer()
        for i := 0; i < b.N; i++ {
                Encode(ioutil.Discard, img, nil)
        }
 }
 
 func BenchmarkQuantizedEncode(b *testing.B) {
-       b.StopTimer()
        img := image.NewRGBA(image.Rect(0, 0, 640, 480))
        bo := img.Bounds()
        rnd := rand.New(rand.NewSource(123))
@@ -545,7 +543,8 @@ func BenchmarkQuantizedEncode(b *testing.B) {
                }
        }
        b.SetBytes(640 * 480 * 4)
-       b.StartTimer()
+       b.ReportAllocs()
+       b.ResetTimer()
        for i := 0; i < b.N; i++ {
                Encode(ioutil.Discard, img, nil)
        }
index 77376152bc04e156c6f07d6a0b3fbae30b34beef..a62b509234e44b8506e2778fd23a678fa130d12b 100644 (file)
@@ -323,7 +323,6 @@ func TestExtraneousData(t *testing.T) {
 }
 
 func benchmarkDecode(b *testing.B, filename string) {
-       b.StopTimer()
        data, err := ioutil.ReadFile(filename)
        if err != nil {
                b.Fatal(err)
@@ -333,7 +332,8 @@ func benchmarkDecode(b *testing.B, filename string) {
                b.Fatal(err)
        }
        b.SetBytes(int64(cfg.Width * cfg.Height * 4))
-       b.StartTimer()
+       b.ReportAllocs()
+       b.ResetTimer()
        for i := 0; i < b.N; i++ {
                Decode(bytes.NewReader(data))
        }
index a6c056174bdc029e31c7dbd1c33b53bc9ec62a4e..3aff74263253fc2e365bb89c5739f9425bae855e 100644 (file)
@@ -243,7 +243,6 @@ func TestEncodeYCbCr(t *testing.T) {
 }
 
 func BenchmarkEncodeRGBA(b *testing.B) {
-       b.StopTimer()
        img := image.NewRGBA(image.Rect(0, 0, 640, 480))
        bo := img.Bounds()
        rnd := rand.New(rand.NewSource(123))
@@ -258,7 +257,8 @@ func BenchmarkEncodeRGBA(b *testing.B) {
                }
        }
        b.SetBytes(640 * 480 * 4)
-       b.StartTimer()
+       b.ReportAllocs()
+       b.ResetTimer()
        options := &Options{Quality: 90}
        for i := 0; i < b.N; i++ {
                Encode(ioutil.Discard, img, options)
@@ -266,7 +266,6 @@ func BenchmarkEncodeRGBA(b *testing.B) {
 }
 
 func BenchmarkEncodeYCbCr(b *testing.B) {
-       b.StopTimer()
        img := image.NewYCbCr(image.Rect(0, 0, 640, 480), image.YCbCrSubsampleRatio420)
        bo := img.Bounds()
        rnd := rand.New(rand.NewSource(123))
@@ -280,7 +279,8 @@ func BenchmarkEncodeYCbCr(b *testing.B) {
                }
        }
        b.SetBytes(640 * 480 * 3)
-       b.StartTimer()
+       b.ReportAllocs()
+       b.ResetTimer()
        options := &Options{Quality: 90}
        for i := 0; i < b.N; i++ {
                Encode(ioutil.Discard, img, options)
index cabf533adcd68821cc1613303c0e724adaceba9e..da498fe2070934417a59422984e477e37b8e45f8 100644 (file)
@@ -650,20 +650,19 @@ func TestGray8Transparent(t *testing.T) {
 }
 
 func benchmarkDecode(b *testing.B, filename string, bytesPerPixel int) {
-       b.StopTimer()
        data, err := ioutil.ReadFile(filename)
        if err != nil {
                b.Fatal(err)
        }
-       s := string(data)
-       cfg, err := DecodeConfig(strings.NewReader(s))
+       cfg, err := DecodeConfig(bytes.NewReader(data))
        if err != nil {
                b.Fatal(err)
        }
        b.SetBytes(int64(cfg.Width * cfg.Height * bytesPerPixel))
-       b.StartTimer()
+       b.ReportAllocs()
+       b.ResetTimer()
        for i := 0; i < b.N; i++ {
-               Decode(strings.NewReader(s))
+               Decode(bytes.NewReader(data))
        }
 }
 
index b1f97b1d7bf9798cd7c70bcfc8af579fee8a8039..1107ea0e7fc2460c9121e91f1723790661abcb59 100644 (file)
@@ -121,10 +121,10 @@ func TestSubImage(t *testing.T) {
 }
 
 func BenchmarkEncodeGray(b *testing.B) {
-       b.StopTimer()
        img := image.NewGray(image.Rect(0, 0, 640, 480))
        b.SetBytes(640 * 480 * 1)
-       b.StartTimer()
+       b.ReportAllocs()
+       b.ResetTimer()
        for i := 0; i < b.N; i++ {
                Encode(ioutil.Discard, img)
        }
@@ -143,20 +143,19 @@ func (p *pool) Put(b *EncoderBuffer) {
 }
 
 func BenchmarkEncodeGrayWithBufferPool(b *testing.B) {
-       b.StopTimer()
        img := image.NewGray(image.Rect(0, 0, 640, 480))
        e := Encoder{
                BufferPool: &pool{},
        }
        b.SetBytes(640 * 480 * 1)
-       b.StartTimer()
+       b.ReportAllocs()
+       b.ResetTimer()
        for i := 0; i < b.N; i++ {
                e.Encode(ioutil.Discard, img)
        }
 }
 
 func BenchmarkEncodeNRGBOpaque(b *testing.B) {
-       b.StopTimer()
        img := image.NewNRGBA(image.Rect(0, 0, 640, 480))
        // Set all pixels to 0xFF alpha to force opaque mode.
        bo := img.Bounds()
@@ -169,40 +168,40 @@ func BenchmarkEncodeNRGBOpaque(b *testing.B) {
                b.Fatal("expected image to be opaque")
        }
        b.SetBytes(640 * 480 * 4)
-       b.StartTimer()
+       b.ReportAllocs()
+       b.ResetTimer()
        for i := 0; i < b.N; i++ {
                Encode(ioutil.Discard, img)
        }
 }
 
 func BenchmarkEncodeNRGBA(b *testing.B) {
-       b.StopTimer()
        img := image.NewNRGBA(image.Rect(0, 0, 640, 480))
        if img.Opaque() {
                b.Fatal("expected image not to be opaque")
        }
        b.SetBytes(640 * 480 * 4)
-       b.StartTimer()
+       b.ReportAllocs()
+       b.ResetTimer()
        for i := 0; i < b.N; i++ {
                Encode(ioutil.Discard, img)
        }
 }
 
 func BenchmarkEncodePaletted(b *testing.B) {
-       b.StopTimer()
        img := image.NewPaletted(image.Rect(0, 0, 640, 480), color.Palette{
                color.RGBA{0, 0, 0, 255},
                color.RGBA{255, 255, 255, 255},
        })
        b.SetBytes(640 * 480 * 1)
-       b.StartTimer()
+       b.ReportAllocs()
+       b.ResetTimer()
        for i := 0; i < b.N; i++ {
                Encode(ioutil.Discard, img)
        }
 }
 
 func BenchmarkEncodeRGBOpaque(b *testing.B) {
-       b.StopTimer()
        img := image.NewRGBA(image.Rect(0, 0, 640, 480))
        // Set all pixels to 0xFF alpha to force opaque mode.
        bo := img.Bounds()
@@ -215,20 +214,21 @@ func BenchmarkEncodeRGBOpaque(b *testing.B) {
                b.Fatal("expected image to be opaque")
        }
        b.SetBytes(640 * 480 * 4)
-       b.StartTimer()
+       b.ReportAllocs()
+       b.ResetTimer()
        for i := 0; i < b.N; i++ {
                Encode(ioutil.Discard, img)
        }
 }
 
 func BenchmarkEncodeRGBA(b *testing.B) {
-       b.StopTimer()
        img := image.NewRGBA(image.Rect(0, 0, 640, 480))
        if img.Opaque() {
                b.Fatal("expected image not to be opaque")
        }
        b.SetBytes(640 * 480 * 4)
-       b.StartTimer()
+       b.ReportAllocs()
+       b.ResetTimer()
        for i := 0; i < b.N; i++ {
                Encode(ioutil.Discard, img)
        }