~eliasnaur/gio-patches

gio: example/kitchen: add benchmark v1 PROPOSED

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

 1 files changed, 91 insertions(+), 0 deletions(-)
Btw, is there something going on with the patches on sr.ht?

It's the second commit which ended up without the subject line:

*  https://git.sr.ht/~eliasnaur/gio/commit/f70eec31651314f7911bee30f8853d2e696a26c8
* https://git.sr.ht/~eliasnaur/gio/commit/918a5da308d7b82d3082dab596f704bba2c15007
On Wed, Nov 25, 2020 at 7:52 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/15303/mbox | git am -3
Learn more about email & git
View this thread in the archives

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

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

Signed-off-by: Egon Elbre <egonelbre@gmail.com>
---
 example/kitchen/main_test.go | 91 ++++++++++++++++++++++++++++++++++++
 1 file changed, 91 insertions(+)
 create mode 100644 example/kitchen/main_test.go

diff --git a/example/kitchen/main_test.go b/example/kitchen/main_test.go
new file mode 100644
index 0000000..91a1130
--- /dev/null
+++ b/example/kitchen/main_test.go
@@ -0,0 +1,91 @@
// SPDX-License-Identifier: Unlicense OR MIT

package main

import (
	"image"
	"testing"
	"time"

	"gioui.org/layout"
	"gioui.org/op"
	"gioui.org/app/headless"
	"gioui.org/f32"
	"gioui.org/font/gofont"
	"gioui.org/widget/material"
)

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) {
	th := material.NewTheme(gofont.Collection())

	w, err := headless.NewWindow(800, 600)
	if err != nil {
		b.Fatal(err)
	}
	defer w.Release()

	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)),
		}
		addTransform(i, transform, gtx.Ops)
		layoutTime += measure(func(){ kitchen(gtx, th) })
		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
Thank you, applied.