~eliasnaur/gio-patches

gio: example/gophers: add benchmark v1 PROPOSED

~egonelbre
~egonelbre: 1
 example/gophers: add benchmark

 1 files changed, 68 insertions(+), 2 deletions(-)
Currently the fetch is stubbed out, but, yeah, it probably will end up
just testing layouting the text.

I'll send the benchmark for kitchen.

On Wed, Nov 25, 2020 at 4:59 PM Elias Naur <mail@eliasnaur.com> wrote:
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~eliasnaur/gio-patches/patches/15296/mbox | git am -3
Learn more about email & git
View this thread in the archives

[PATCH gio] example/gophers: add benchmark Export this patch

~egonelbre
From: Egon Elbre <egonelbre@gmail.com>

Add benchmark that accounts for layout and frame rendering. Also
benchmark different cases with transforms.

Signed-off-by: Egon Elbre <egonelbre@gmail.com>
---
 example/gophers/main_test.go | 70 ++++++++++++++++++++++++++++++++++--
 1 file changed, 68 insertions(+), 2 deletions(-)

diff --git a/example/gophers/main_test.go b/example/gophers/main_test.go
index bc147f7..fa9200c 100644
--- a/example/gophers/main_test.go
+++ b/example/gophers/main_test.go
@@ -5,20 +5,86 @@ package main
import (
	"image"
	"testing"
	"time"

	"gioui.org/layout"
	"gioui.org/op"
	"gioui.org/app/headless"
	"gioui.org/f32"
)

func BenchmarkUI(b *testing.B) {
func BenchmarkUI(b *testing.B) { benchmarkUI(b, transformation{}) }
func BenchmarkUI_Offset(b *testing.B) { benchmarkUI(b, transformation{offset: true}) }
func BenchmarkUI_Scale(b *testing.B) { benchmarkUI(b, transformation{scale: true}) }
func BenchmarkUI_Rotate(b *testing.B) { benchmarkUI(b, transformation{rotate: true}) }
func BenchmarkUI_All(b *testing.B) { benchmarkUI(b, transformation{offset: true, rotate: true, scale: true}) }

func benchmarkUI(b *testing.B, transform transformation) {
	w, err := headless.NewWindow(800, 600)
	if err != nil {
		b.Fatal(err)
	}
	defer w.Release()

	fetch := func(_ string) {}
	u := newUI(fetch)

	var layoutTime time.Duration
	var frameTime time.Duration

	b.ResetTimer()
	var ops op.Ops
	for i := 0; i < b.N; i++ {
		ops.Reset()
		gtx := layout.Context{
			Ops:         &ops,
			Constraints: layout.Exact(image.Pt(800, 600)),
		}
		u.Layout(gtx)
		addTransform(i, transform, gtx.Ops)
		layoutTime += measure(func(){ u.Layout(gtx) })
		frameTime += measure(func(){ w.Frame(&ops) })
	}
	b.StopTimer()

	b.ReportMetric(float64(layoutTime.Nanoseconds()) / float64(b.N), "ns/layout")
	b.ReportMetric(float64(frameTime.Nanoseconds()) / float64(b.N), "ns/frame")
}

type transformation struct {
	offset bool
	rotate bool
	scale bool
}

func addTransform(i int, transform transformation, ops *op.Ops) {
	if !(transform.offset || transform.rotate || transform.scale) {
		return
	}
	dt := float32(i)
	tr := f32.Affine2D{}
	if transform.rotate {
		angle := dt * .1
		tr = tr.Rotate(f32.Pt(300, 20), -angle)
	}
	if transform.scale {
		scale := 1.0 - dt*.5
		if scale < 0.5 {
			scale = 0.5
		}
		tr = tr.Scale(f32.Pt(300, 20), f32.Pt(scale, scale))
	}
	if transform.offset {
		offset := dt * 50
		if offset > 200 {
			offset = 200
		}
		tr = tr.Offset(f32.Pt(0, offset))
	}
	op.Affine(tr).Add(ops)
}

func measure(fn func()) time.Duration {
	start := time.Now()
	fn()
	return time.Since(start)
}
\ No newline at end of file
-- 
2.26.2
Thanks. It occurred to me the gophers example depends on the internet. I
think the kitchen example is better for benchmarking.