From 2f3b061c5f59b96fca7d9bef9e7baaa7229ef67f Mon Sep 17 00:00:00 2001 From: Daniel Patterson Date: Tue, 24 Nov 2026 18:24:27 +0000 Subject: [PATCH] Measure a pane's frames as the window draws them, part by part BenchmarkFrame measured a pane drawn with Gio alone, which the window no longer does. It now draws the pane as the window does, as a layer, composited, after each kind of change (none, one row, a scrolled region, every row), at a quarter of a 1080p screen, all of it, and all of a large laptop's. It reports the parts of a frame: the layout on the CPU, handing the layer to Gio and the compositor, uploads included, and waiting for the GPU. On an Intel N100, every row of a 1080p pane drawn again took 17.5ms: 6.7ms painting rows on the CPU, 4.9ms uploading them and 5.9ms of the GPU drawing them. No one part is most of it, which is what drawing the cells on the GPU removes all three of. --- internal/termview/bench_test.go | 104 +++++++++++++++++++++++--------- 1 file changed, 75 insertions(+), 29 deletions(-) diff --git a/internal/termview/bench_test.go b/internal/termview/bench_test.go index 4e6057e..7d5b054 100644 --- a/internal/termview/bench_test.go +++ b/internal/termview/bench_test.go @@ -4,18 +4,20 @@ import ( "bytes" "fmt" "image" + "slices" "testing" "time" "gioui.org/font/gofont" - "gioui.org/gpu/headless" "gioui.org/layout" "gioui.org/op" "gioui.org/unit" "go.mitchellh.com/libghostty" + "100gex/internal/present" "100gex/internal/sample" + "100gex/internal/scene" "100gex/internal/termscreen" "100gex/internal/termview" "100gex/internal/theme" @@ -159,37 +161,81 @@ func BenchmarkLayout(b *testing.B) { } } -// BenchmarkFrame measures a full pane's whole frame, with every row drawn -// again: the layout, and Gio drawing it on the GPU, off-screen, until the -// GPU is done. Its fps is the frame rate one such pane alone allows. +// frameSizes are the panes whose whole frames are measured: paneSizes, +// and the whole of a large laptop's screen. +var frameSizes = append(paneSizes, image.Pt(3356, 2060)) + +// frameChanges are the changes whose frames are measured, by name. +var frameChanges = []string{"unchanged", "one-row", "region-scroll", "every-row"} + +// BenchmarkFrame measures a pane's whole frames as the window draws them, +// after each of frameChanges: the pane is a layer, drawn again only when +// it changed, then composited, until the GPU is done. It reports the +// frame's parts: layout-µs on the CPU, reading cells and painting rows; +// submit-µs handing the layer to Gio and the compositor, uploads included; +// and gpu-µs waiting for the GPU to finish. Its fps is the frame rate one +// such pane alone allows. func BenchmarkFrame(b *testing.B) { - for _, size := range paneSizes { - for _, s := range screens { - b.Run(fmt.Sprintf("%dx%d/%s", size.X, size.Y, s.name), func(b *testing.B) { - win, err := headless.NewWindow(size.X, size.Y) - if err != nil { - b.Skipf("no headless GPU context: %v", err) - } - defer win.Release() - p := newBenchPane(b, size, s.fill) - if err := win.Frame(&p.ops); err != nil { - b.Fatal(err) - } - b.ReportAllocs() - start := time.Now() - for b.Loop() { - termview.ForgetRows(p.view) - p.layout(b) - if err := win.Frame(&p.ops); err != nil { - b.Fatal(err) + for _, size := range frameSizes { + for _, sc := range screens[1:] { // code, and the worst case, styled + for _, name := range frameChanges { + i := slices.IndexFunc(changes, func(c struct { + name string + change func(p *benchPane) + }) bool { + return c.name == name + }) + change := changes[i].change + b.Run(fmt.Sprintf("%dx%d/%s/%s", size.X, size.Y, sc.name, name), func(b *testing.B) { + win, err := present.NewHeadless(size) + if err != nil { + b.Skipf("no headless GPU context: %v", err) + } + defer win.Release() + p := newBenchPane(b, size, sc.fill) + var s scene.Scene + var root op.Ops + var lay, submit, gpu time.Duration + frame := func() { + change(p) + t0 := time.Now() + root.Reset() + s.Begin(size) + gtx := s.Layer(p.gtx, "pane", scene.Spec{Bounds: image.Rectangle{Max: size}, Opaque: true}) + if err := p.view.Update(gtx, p.scr); err != nil { + b.Fatal(err) + } + if _, err := p.view.Layout(gtx); err != nil { + b.Fatal(err) + } + s.End(&root) + t1 := time.Now() + if _, err := win.Frame(&s); err != nil { + b.Fatal(err) + } + t2 := time.Now() + // Wait for the GPU to draw it. + if err := win.Screenshot(onePixel); err != nil { + b.Fatal(err) + } + lay, submit, gpu = lay+t1.Sub(t0), submit+t2.Sub(t1), gpu+time.Since(t2) } - // Wait for the GPU to draw it. - if err := win.Screenshot(onePixel); err != nil { - b.Fatal(err) + for range 5 { + frame() } - } - b.ReportMetric(float64(b.N)/time.Since(start).Seconds(), "fps") - }) + lay, submit, gpu = 0, 0, 0 + b.ReportAllocs() + start := time.Now() + for b.Loop() { + frame() + } + n := float64(b.N) + b.ReportMetric(float64(b.N)/time.Since(start).Seconds(), "fps") + b.ReportMetric(float64(lay.Microseconds())/n, "layout-µs") + b.ReportMetric(float64(submit.Microseconds())/n, "submit-µs") + b.ReportMetric(float64(gpu.Microseconds())/n, "gpu-µs") + }) + } } } } -- knotmirror 0.0.1