Files
wuffs/script/bench-go-png/main.go
T
Nigel Tao 7a73708a56 Have script/bench-*-png convert to BGRA
On a mid-range x86_64 laptop:

Compared to the previous commit:

go_decode_19k_8bpp                      39.3MB/s ± 1%  39.4MB/s ± 1%     ~     (p=0.548 n=5+5)
go_decode_40k_24bpp                     46.5MB/s ± 1%  46.7MB/s ± 1%     ~     (p=0.095 n=5+5)
go_decode_77k_8bpp                      78.3MB/s ± 0%  78.3MB/s ± 0%     ~     (p=0.286 n=4+5)
go_decode_552k_32bpp_verify_checksum     120MB/s ± 1%   118MB/s ± 0%     ~     (p=0.056 n=5+5)
go_decode_4002k_24bpp                   50.7MB/s ± 0%  50.5MB/s ± 0%   -0.58%  (p=0.016 n=5+4)

rust_decode_19k_8bpp                    88.3MB/s ± 0%  89.8MB/s ± 0%   +1.76%  (p=0.008 n=5+5)
rust_decode_40k_24bpp                    133MB/s ± 0%   122MB/s ± 0%   -8.58%  (p=0.008 n=5+5)
rust_decode_77k_8bpp                     180MB/s ± 0%   158MB/s ± 0%  -12.04%  (p=0.008 n=5+5)
rust_decode_552k_32bpp_verify_checksum   146MB/s ± 0%   136MB/s ± 0%   -6.59%  (p=0.016 n=4+5)
rust_decode_4002k_24bpp                  134MB/s ± 0%   122MB/s ± 0%   -9.02%  (p=0.008 n=5+5)

Compared to libpng (the C library):

libpng_decode_19k_8bpp                                 58.0MB/s ± 0%  1.00x
libpng_decode_40k_24bpp                                73.1MB/s ± 0%  1.00x
libpng_decode_77k_8bpp                                  177MB/s ± 0%  1.00x
libpng_decode_552k_32bpp_verify_checksum                146MB/s ± 0%  1.00x
libpng_decode_4002k_24bpp                               104MB/s ± 0%  1.00x

go_decode_19k_8bpp/go1.16                              39.4MB/s ± 1%  0.68x
go_decode_40k_24bpp/go1.16                             46.7MB/s ± 1%  0.64x
go_decode_77k_8bpp/go1.16                              78.3MB/s ± 0%  0.44x
go_decode_552k_32bpp_verify_checksum/go1.16             118MB/s ± 0%  0.81x
go_decode_4002k_24bpp/go1.16                           50.5MB/s ± 0%  0.49x

rust_decode_19k_8bpp/rust1.48                          89.8MB/s ± 0%  1.55x
rust_decode_40k_24bpp/rust1.48                          122MB/s ± 0%  1.67x
rust_decode_77k_8bpp/rust1.48                           158MB/s ± 0%  0.89x
rust_decode_552k_32bpp_verify_checksum/rust1.48         136MB/s ± 0%  0.93x
rust_decode_4002k_24bpp/rust1.48                        122MB/s ± 0%  1.17x
2021-03-25 12:38:02 +11:00

165 lines
3.8 KiB
Go

// Copyright 2021 The Wuffs Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// +build ignore
package main
// This program exercises the Go standard library's PNG decoder.
//
// Wuffs' C code doesn't depend on Go per se, but this program gives some
// performance data for specific Go PNG implementations. The equivalent Wuffs
// benchmarks (on the same test images) are run via:
//
// wuffs bench std/png
import (
"bytes"
"fmt"
"image"
"image/draw"
"image/png"
"io/ioutil"
"os"
"runtime"
"strings"
"time"
)
const (
iterscale = 20
reps = 5
)
type testCase = struct {
benchname string
src []byte
itersUnscaled uint32
}
var testCases = []testCase{{
benchname: "go_png_decode_image_19k_8bpp",
src: mustLoad("test/data/bricks-gray.no-ancillary.png"),
itersUnscaled: 50,
}, {
benchname: "go_png_decode_image_40k_24bpp",
src: mustLoad("test/data/hat.png"),
itersUnscaled: 50,
}, {
benchname: "go_png_decode_image_77k_8bpp",
src: mustLoad("test/data/bricks-dither.png"),
itersUnscaled: 50,
}, {
benchname: "go_png_decode_image_552k_32bpp_verify_checksum",
src: mustLoad("test/data/hibiscus.primitive.png"),
itersUnscaled: 4,
}, {
benchname: "go_png_decode_image_4002k_24bpp",
src: mustLoad("test/data/harvesters.png"),
itersUnscaled: 1,
}}
func mustLoad(filename string) []byte {
src, err := ioutil.ReadFile("../../" + filename)
if err != nil {
panic(err.Error())
}
return src
}
func main() {
if err := main1(); err != nil {
os.Stderr.WriteString(err.Error() + "\n")
os.Exit(1)
}
}
func main1() error {
fmt.Printf("# Go %s\n", runtime.Version())
fmt.Printf("#\n")
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 {
runtime.GC()
start := time.Now()
iters := uint64(tc.itersUnscaled) * iterscale
expandPalette := strings.HasSuffix(tc.benchname, "_77k_8bpp")
numBytes, err := decode(tc.src, expandPalette)
if err != nil {
return err
}
for j := uint64(1); j < iters; j++ {
decode(tc.src, expandPalette)
}
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)
}
}
return nil
}
func decode(src []byte, expandPalette bool) (numBytes uint64, retErr error) {
m, err := png.Decode(bytes.NewReader(src))
if err != nil {
return 0, err
}
b := m.Bounds()
n := uint64(b.Dx()) * uint64(b.Dy())
if expandPalette {
dst := image.NewRGBA(b)
draw.Draw(dst, b, m, b.Min, draw.Src)
m = dst
}
pix := []byte(nil)
switch m := m.(type) {
case *image.Gray:
n *= 1
case *image.NRGBA:
n *= 4
pix = m.Pix
case *image.RGBA:
n *= 4
pix = m.Pix
default:
return 0, fmt.Errorf("unexpected image type %T", m)
}
// Convert RGBA => BGRA.
if pix != nil {
for i, iEnd := 0, len(pix)/4; i < iEnd; i += 4 {
pix[(4*i)+0], pix[(4*i)+2] = pix[(4*i)+2], pix[(4*i)+0]
}
}
return n, nil
}