mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-24 00:52:18 +01:00
Have bench-go-gif process multiple images
This commit is contained in:
1 parent
c3a07db892
commit
e071e171cc
1 file changed
+110
-42
+110
-42
@@ -20,34 +20,71 @@ package main
|
|||||||
//
|
//
|
||||||
// Wuffs' C code doesn't depend on Go per se, but this program gives some
|
// Wuffs' C code doesn't depend on Go per se, but this program gives some
|
||||||
// performance data for specific Go GIF implementations. The equivalent Wuffs
|
// performance data for specific Go GIF implementations. The equivalent Wuffs
|
||||||
// benchmarks (on the same test image) are run via:
|
// benchmarks (on the same test images) are run via:
|
||||||
//
|
//
|
||||||
// wuffs bench -mimic -focus=wuffs_gif_decode_1000k,mimic_gif_decode_1000k std/gif
|
// wuffs bench std/gif
|
||||||
//
|
|
||||||
// The "1000k" is because the test image (harvesters.gif) has approximately 1
|
|
||||||
// million pixels.
|
|
||||||
//
|
|
||||||
// The Wuffs benchmark reports megabytes per second. This program reports
|
|
||||||
// megapixels per second. The two concepts should be equivalent, since GIF
|
|
||||||
// images' pixel data are always 1 byte per pixel indices into a color palette.
|
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"image"
|
"image"
|
||||||
|
"image/draw"
|
||||||
"image/gif"
|
"image/gif"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
"runtime"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// These constants are hard-coded to the harvesters.gif test image.
|
const (
|
||||||
const WIDTH = 1165
|
iterscale = 20
|
||||||
const HEIGHT = 859
|
reps = 5
|
||||||
const BYTES_PER_PIXEL = 1 // Palette index.
|
)
|
||||||
const NUM_BYTES = WIDTH * HEIGHT * BYTES_PER_PIXEL
|
|
||||||
const FIRST_PIXEL = 0 // Top left pixel's palette index is 0x00.
|
type testCase = struct {
|
||||||
const LAST_PIXEL = 1 // Bottom right pixel's palette index is 0x01.
|
benchname string
|
||||||
|
filename string
|
||||||
|
itersUnscaled uint32
|
||||||
|
}
|
||||||
|
|
||||||
|
var testCases = []testCase{{
|
||||||
|
benchname: "go_gif_decode_1k_bw",
|
||||||
|
filename: "test/data/pjw-thumbnail.gif",
|
||||||
|
itersUnscaled: 2000,
|
||||||
|
}, {
|
||||||
|
benchname: "go_gif_decode_1k_color",
|
||||||
|
filename: "test/data/hippopotamus.regular.gif",
|
||||||
|
itersUnscaled: 1000,
|
||||||
|
}, {
|
||||||
|
benchname: "go_gif_decode_10k_bgra",
|
||||||
|
filename: "test/data/hat.gif",
|
||||||
|
itersUnscaled: 100,
|
||||||
|
}, {
|
||||||
|
benchname: "go_gif_decode_10k_indexed",
|
||||||
|
filename: "test/data/hat.gif",
|
||||||
|
itersUnscaled: 100,
|
||||||
|
}, {
|
||||||
|
benchname: "go_gif_decode_20k",
|
||||||
|
filename: "test/data/bricks-gray.gif",
|
||||||
|
itersUnscaled: 50,
|
||||||
|
}, {
|
||||||
|
benchname: "go_gif_decode_100k_artificial",
|
||||||
|
filename: "test/data/hibiscus.primitive.gif",
|
||||||
|
itersUnscaled: 15,
|
||||||
|
}, {
|
||||||
|
benchname: "go_gif_decode_100k_realistic",
|
||||||
|
filename: "test/data/hibiscus.regular.gif",
|
||||||
|
itersUnscaled: 10,
|
||||||
|
}, {
|
||||||
|
benchname: "go_gif_decode_1000k",
|
||||||
|
filename: "test/data/harvesters.gif",
|
||||||
|
itersUnscaled: 1,
|
||||||
|
}, {
|
||||||
|
benchname: "go_gif_decode_anim_screencap",
|
||||||
|
filename: "test/data/gifplayer-muybridge.gif",
|
||||||
|
itersUnscaled: 1,
|
||||||
|
}}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
if err := main1(); err != nil {
|
if err := main1(); err != nil {
|
||||||
@@ -57,39 +94,70 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main1() error {
|
func main1() error {
|
||||||
src, err := ioutil.ReadFile("../../test/data/harvesters.gif")
|
fmt.Printf("# Go %s\n", runtime.Version())
|
||||||
if err != nil {
|
fmt.Printf("#\n")
|
||||||
return err
|
fmt.Printf("# The output format, including the \"Benchmark\" prefixes, is compatible with the\n")
|
||||||
|
fmt.Printf("# https://godoc.org/golang.org/x/perf/cmd/benchstat tool. To install it, first\n")
|
||||||
|
fmt.Printf("# install Go, then run \"go get golang.org/x/perf/cmd/benchstat\".\n")
|
||||||
|
|
||||||
|
for i := -1; i < reps; i++ {
|
||||||
|
for _, tc := range testCases {
|
||||||
|
src, err := ioutil.ReadFile("../../" + tc.filename)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
runtime.GC()
|
||||||
|
|
||||||
|
start := time.Now()
|
||||||
|
|
||||||
|
iters := uint64(tc.itersUnscaled) * iterscale
|
||||||
|
bgra := strings.HasSuffix(tc.benchname, "_bgra")
|
||||||
|
numBytes, err := decode(src, bgra)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for i := uint64(1); i < iters; i++ {
|
||||||
|
decode(src, bgra)
|
||||||
|
}
|
||||||
|
|
||||||
|
elapsedNanos := time.Since(start)
|
||||||
|
|
||||||
|
kbPerS := numBytes * uint64(iters) * 1000000 / uint64(elapsedNanos)
|
||||||
|
|
||||||
|
if i < 0 {
|
||||||
|
continue // Warm up rep.
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("Benchmark%-30s %8d %12d ns/op %8d.%03d MB/s\n",
|
||||||
|
tc.benchname, iters, uint64(elapsedNanos)/iters, kbPerS/1000, kbPerS%1000)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
start := time.Now()
|
|
||||||
|
|
||||||
const REPS = 50
|
|
||||||
for i := 0; i < REPS; i++ {
|
|
||||||
decode(src)
|
|
||||||
}
|
|
||||||
|
|
||||||
elapsedNanos := time.Since(start)
|
|
||||||
|
|
||||||
totalPixels := uint64(WIDTH * HEIGHT * REPS)
|
|
||||||
kpPerS := totalPixels * 1000000 / uint64(elapsedNanos)
|
|
||||||
|
|
||||||
fmt.Printf("Go %3d.%03d megapixels/second\n", kpPerS/1000, kpPerS%1000)
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func decode(src []byte) {
|
func decode(src []byte, bgra bool) (numBytes uint64, retErr error) {
|
||||||
img, err := gif.Decode(bytes.NewReader(src))
|
g, err := gif.DecodeAll(bytes.NewReader(src))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
return 0, err
|
||||||
}
|
|
||||||
m := img.(*image.Paletted)
|
|
||||||
if len(m.Pix) != NUM_BYTES {
|
|
||||||
panic("wrong num_bytes")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// A hard-coded sanity check that we decoded the pixel data correctly.
|
for _, m := range g.Image {
|
||||||
if (m.Pix[0] != FIRST_PIXEL) || (m.Pix[NUM_BYTES-1] != LAST_PIXEL) {
|
b := m.Bounds()
|
||||||
panic("wrong dst pixels")
|
n := uint64(b.Dx()) * uint64(b.Dy())
|
||||||
|
|
||||||
|
// Go converts to RGBA (as that's what Go's image/draw standard library is
|
||||||
|
// optimized for); Wuffs converts to BGRA. The difference isn't important,
|
||||||
|
// as we just want measure how long it takes.
|
||||||
|
if bgra {
|
||||||
|
dst := image.NewRGBA(b)
|
||||||
|
draw.Draw(dst, b, m, b.Min, draw.Src)
|
||||||
|
n *= 4
|
||||||
|
}
|
||||||
|
|
||||||
|
numBytes += n
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return numBytes, nil
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user