From 244b621d1790f8f125081b2c989d85470c8b050b Mon Sep 17 00:00:00 2001 From: "Anthony N. Simon" Date: Tue, 30 Jul 2019 19:12:55 +0200 Subject: [PATCH] Add basic benchmarks --- Makefile | 3 +-- benchmarks.txt | 37 +++++++++++++++++++++++++++++++++ convolution/convolution_test.go | 5 ++++- effect/effect_test.go | 9 +++++--- noise/noise_test.go | 7 +++++-- transform/resize_test.go | 2 +- transform/rotate_test.go | 2 +- transform/translate_test.go | 11 ++++++++++ 8 files changed, 66 insertions(+), 10 deletions(-) create mode 100644 benchmarks.txt diff --git a/Makefile b/Makefile index 565d909..54a2324 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,4 @@ PKG = github.com/anthonynsimon/bild -BENCHMARK = BenchmarkRAWInput VERSION ?= dev LDFLAGS = -ldflags "-X $(PKG)/cmd.Version=$(VERSION) -extldflags \"-static\"" MAC_LDFLAGS = -ldflags "-X $(PKG)/cmd.Version=$(VERSION)" @@ -21,7 +20,7 @@ fmt: go fmt ./... bench: deps - go test ./... $(LDFLAGS) -v -run NOT_EXISTING -bench $(BENCHMARK) -benchtime 5s + go test $(LDFLAGS) -benchmem -bench=. -benchtime=5s ./... race: deps go test ./... -v -race -timeout 15s diff --git a/benchmarks.txt b/benchmarks.txt new file mode 100644 index 0000000..636797c --- /dev/null +++ b/benchmarks.txt @@ -0,0 +1,37 @@ +goos: darwin +goarch: amd64 + +pkg: github.com/anthonynsimon/bild/convolution +BenchmarkConvolve3-8 200 43031408 ns/op 8413514 B/op 8 allocs/op +BenchmarkConvolve8-8 30 219642438 ns/op 8462644 B/op 8 allocs/op +BenchmarkConvolve32-8 2 2869904037 ns/op 8659296 B/op 8 allocs/op +BenchmarkConvolve64-8 1 15558563740 ns/op 8929584 B/op 8 allocs/op + +pkg: github.com/anthonynsimon/bild/effect +BenchmarkMedian1-8 2000 6637173 ns/op 3686887 B/op 65544 allocs/op +BenchmarkMedian4-8 30 251734934 ns/op 23627210 B/op 65551 allocs/op +BenchmarkMedian8-8 2 2844636597 ns/op 84584800 B/op 65567 allocs/op + +pkg: github.com/anthonynsimon/bild/noise +BenchmarkUniformMonochrome-8 200 34201494 ns/op 1048900 B/op 5 allocs/op +BenchmarkUniformColored-8 100 96952189 ns/op 1048754 B/op 4 allocs/op + +pkg: github.com/anthonynsimon/bild/paint +BenchmarkFloodFill-8 100 75234019 ns/op 24616194 B/op 259073 allocs/op + +pkg: github.com/anthonynsimon/bild/transform +BenchmarkResizeTenth-8 50 121855364 ns/op 74483576 B/op 13 allocs/op +BenchmarkResizeQuarter-8 50 133837811 ns/op 88081303 B/op 11 allocs/op +BenchmarkResizeHalf-8 30 192098367 ns/op 117441409 B/op 11 allocs/op +BenchmarkResize1x-8 300 26409679 ns/op 12583343 B/op 10 allocs/op +BenchmarkResize2x-8 100 72839314 ns/op 29360562 B/op 10 allocs/op +BenchmarkResize4x-8 30 235221709 ns/op 88081381 B/op 11 allocs/op +BenchmarkResize8x-8 10 852207403 ns/op 306184608 B/op 10 allocs/op +BenchmarkResize16x-8 2 3481516525 ns/op 1145045440 B/op 10 allocs/op +BenchmarkRotation256-8 5000 1232278 ns/op 524540 B/op 7 allocs/op +BenchmarkRotation512-8 2000 5393780 ns/op 2097412 B/op 7 allocs/op +BenchmarkRotation1024-8 300 24388950 ns/op 8388865 B/op 7 allocs/op +BenchmarkRotation2048-8 100 93141358 ns/op 33554704 B/op 7 allocs/op +BenchmarkRotation4096-8 20 386209001 ns/op 134217995 B/op 7 allocs/op +BenchmarkRotation8192-8 3 1751796711 ns/op 536871202 B/op 7 allocs/op +BenchmarkTranslate-8 5000 1657326 ns/op 8389660 B/op 6 allocs/op diff --git a/convolution/convolution_test.go b/convolution/convolution_test.go index 3b0d2b2..a771a07 100644 --- a/convolution/convolution_test.go +++ b/convolution/convolution_test.go @@ -7,6 +7,9 @@ import ( "github.com/anthonynsimon/bild/util" ) +// benchResult is used to avoid having the compiler optimize the benchmark code calls +var benchResult interface{} + func TestConvolve(t *testing.T) { cases := []struct { options *Options @@ -222,6 +225,6 @@ func benchConvolve(b *testing.B, w, h int, k *Kernel) { img := image.NewRGBA(image.Rect(0, 0, w, h)) b.ResetTimer() for n := 0; n < b.N; n++ { - Convolve(img, k, &Options{Wrap: false}) + benchResult = Convolve(img, k, &Options{Wrap: false}) } } diff --git a/effect/effect_test.go b/effect/effect_test.go index 35a9966..ddbfebb 100644 --- a/effect/effect_test.go +++ b/effect/effect_test.go @@ -7,6 +7,9 @@ import ( "github.com/anthonynsimon/bild/util" ) +// benchResult is used to avoid having the compiler optimize the benchmark code calls +var benchResult interface{} + func TestSepia(t *testing.T) { cases := []struct { value image.Image @@ -854,20 +857,20 @@ func TestUnsharpMask(t *testing.T) { func BenchmarkMedian1(b *testing.B) { img := image.NewRGBA(image.Rect(0, 0, 256, 256)) for n := 0; n < b.N; n++ { - Median(img, 1) + benchResult = Median(img, 1) } } func BenchmarkMedian4(b *testing.B) { img := image.NewRGBA(image.Rect(0, 0, 256, 256)) for n := 0; n < b.N; n++ { - Median(img, 4) + benchResult = Median(img, 4) } } func BenchmarkMedian8(b *testing.B) { img := image.NewRGBA(image.Rect(0, 0, 256, 256)) for n := 0; n < b.N; n++ { - Median(img, 8) + benchResult = Median(img, 8) } } diff --git a/noise/noise_test.go b/noise/noise_test.go index d329c49..b5a5cdc 100644 --- a/noise/noise_test.go +++ b/noise/noise_test.go @@ -7,6 +7,9 @@ import ( "github.com/anthonynsimon/bild/histogram" ) +// benchResult is used to avoid having the compiler optimize the benchmark code calls +var benchResult interface{} + func TestMonochromeNoise(t *testing.T) { cases := []struct { w, h int @@ -163,13 +166,13 @@ func TestBinaryNoise(t *testing.T) { func BenchmarkUniformMonochrome(b *testing.B) { for n := 0; n < b.N; n++ { - Generate(512, 512, &Options{NoiseFn: Uniform, Monochrome: true}) + benchResult = Generate(512, 512, &Options{NoiseFn: Uniform, Monochrome: true}) } } func BenchmarkUniformColored(b *testing.B) { for n := 0; n < b.N; n++ { - Generate(512, 512, &Options{NoiseFn: Uniform, Monochrome: false}) + benchResult = Generate(512, 512, &Options{NoiseFn: Uniform, Monochrome: false}) } } diff --git a/transform/resize_test.go b/transform/resize_test.go index 854fb6a..710de99 100644 --- a/transform/resize_test.go +++ b/transform/resize_test.go @@ -830,6 +830,6 @@ func benchResize(b *testing.B, w, h int, scale float64, f ResampleFilter) { img := image.NewRGBA(image.Rect(0, 0, w, h)) b.ResetTimer() for n := 0; n < b.N; n++ { - Resize(img, newW, newH, f) + benchResult = Resize(img, newW, newH, f) } } diff --git a/transform/rotate_test.go b/transform/rotate_test.go index 8f47684..622e35c 100644 --- a/transform/rotate_test.go +++ b/transform/rotate_test.go @@ -347,6 +347,6 @@ func benchRotate(w, h int, rot float64, bench *testing.B) { img := image.NewRGBA(image.Rect(0, 0, w, h)) bench.ResetTimer() for i := 0; i < bench.N; i++ { - Rotate(img, rot, nil) + benchResult = Rotate(img, rot, nil) } } diff --git a/transform/translate_test.go b/transform/translate_test.go index ecf2da9..c2fcdbd 100644 --- a/transform/translate_test.go +++ b/transform/translate_test.go @@ -7,6 +7,17 @@ import ( "github.com/anthonynsimon/bild/util" ) +// benchResult is used to avoid having the compiler optimize the benchmark code calls +var benchResult interface{} + +func BenchmarkTranslate(b *testing.B) { + img := image.NewRGBA(image.Rect(0, 0, 1024, 1024)) + b.ResetTimer() + for n := 0; n < b.N; n++ { + benchResult = Translate(img, 512, 512) + } +} + func TestTranslate(t *testing.T) { cases := []struct { name string