Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315package ui
import ( "fmt" "image" "os" "path/filepath" "slices" "strings" "testing" "time"
"gioui.org/f32" "gioui.org/io/pointer"
"100gex/internal/present" "100gex/internal/rex/rextest" "100gex/internal/rexsession" "100gex/internal/sample")
// benchScreens are what the window benchmarks fill panes with.var benchScreens = []struct { name string fill func(cols, rows int) []byte // budgeted screens must draw within FrameBudget; the others are // measured, to watch, but not held to it. budgeted bool}{ {"plain", sample.Plain, true}, {"code", sample.Code, true}, {"styled", sample.Styled, true},}
// benchWindow is the window size benchmarked: a 1080p screen.var benchWindow = image.Pt(1920, 1080)
// budgetWindows are the window sizes held to the frame budget: 1080p, and// a large laptop screen, as a frame mustn't cost more as screens grow.var budgetWindows = []image.Point{benchWindow, {3356, 2060}}
// fourPanes starts a UI of size, showing one window split into four panes,// each full of what fill writes for its size, drawn as the window draws// them, with cells. It is written once, so the frames drawn afterwards cost// only drawing.func fourPanes(tb testing.TB, size image.Point, fill func(cols, rows int) []byte) *uiHarness { tb.Helper() srv := rextest.Start(tb) other := srv.Dial(tb, "other-device") dir := tb.TempDir() // Each block waits for a byte, then shows its file and stays. command := func(n int) []string { file := filepath.Join(dir, fmt.Sprint(n)) return []string{"/bin/sh", "-c", fmt.Sprintf("stty raw -echo; printf 'ready\\r\\n'; head -c 1 >/dev/null; cat '%s'; exec cat >/dev/null", file)} } sid, b1 := rextest.NewSession(tb, other, command(0)...) blocks := []string{b1} split := func(anchor, direction string, n int) string { var out struct { BlockIDs []string `json:"block_ids"` } rextest.Call(tb, other, "session.new_split", map[string]any{ "session_id": sid, "anchor_id": anchor, "direction": direction, "side": "after", "layout": rextest.Block(command(n)...), "focus": false, }, &out) return out.BlockIDs[0] } b2 := split(b1, "horizontal", 1) blocks = append(blocks, b2, split(b1, "vertical", 2), split(b2, "vertical", 3))
h := startUI(tb, srv, srv.Dial(tb, "100gex-bench"), sid) h.size = size h.u.RowImages = false // as the window draws them // Each terminal takes this client's size for its pane. sized := func(f rexsession.Frame) bool { if len(f.Visible) != 4 || !f.Ready() { return false } for _, b := range blocks { p, pv := f.Panes[b], h.u.panes[b] if pv == nil || !p.State.Owned || !strings.Contains(screenText(h.term(b)), "ready") { return false } cols, rows := pv.view.Size() tc, _ := h.term(b).Cols() tr, _ := h.term(b).Rows() if cols == 0 || tc != cols || tr != rows { return false } } return true } // The other device made the blocks, so they are its until taken, as // clicking them would. h.until("four panes", func(f rexsession.Frame) bool { return len(f.Visible) == 4 && f.Ready() }) for _, b := range blocks { h.u.session.Claim(b) } h.until("four panes sized for this client", sized) for n, b := range blocks { cols, rows := h.u.panes[b].view.Size() if err := os.WriteFile(filepath.Join(dir, fmt.Sprint(n)), fill(int(cols), int(rows)), 0o600); err != nil { tb.Fatal(err) } h.u.session.PasteTo(b, "x") } h.until("four full panes", func(f rexsession.Frame) bool { for _, b := range blocks { text := screenText(h.term(b)) cols, rows := h.u.panes[b].view.Size() if strings.Contains(text, "ready") || len(strings.Join(strings.Fields(text), "")) < int(cols)*int(rows)/2 { return false } } return true }) return h}
// gpuFrame draws a UI frame and has the GPU draw it, off-screen, as the// window does: its layers, each only if it changed, then put together. It// waits for the GPU to finish: handing a frame to the GPU returns long// before it's drawn, and the drawing is most of a frame's cost.func gpuFrame(tb testing.TB, h *uiHarness, win *present.Headless) { h.frame() if _, err := win.Frame(h.u.Scene()); err != nil { tb.Fatal(err) } // Reading a pixel back waits for everything drawn before it. if err := win.Screenshot(onePixel); err != nil { tb.Fatal(err) }}
var onePixel = image.NewRGBA(image.Rect(0, 0, 1, 1))
// newTarget is an off-screen window of size to draw frames to, or skips tb// if there's no GPU to draw with.func newTarget(tb testing.TB, size image.Point) *present.Headless { tb.Helper() win, err := present.NewHeadless(size) if err != nil { tb.Skipf("no headless GPU context: %v", err) } tb.Cleanup(win.Release) return win}
// BenchmarkWindow measures whole frames of a 1080p window split into four// full panes, unchanged: the UI's layout, and the frame drawn on the GPU,// off-screen.func BenchmarkWindow(b *testing.B) { for _, s := range benchScreens { b.Run(s.name, func(b *testing.B) { win := newTarget(b, benchWindow) h := fourPanes(b, benchWindow, s.fill) gpuFrame(b, h, win) b.ReportAllocs() start := time.Now() for b.Loop() { gpuFrame(b, h, win) } b.ReportMetric(float64(b.N)/time.Since(start).Seconds(), "fps") }) }}
// TestFrameBudget draws windows of four full panes again and again, and// fails if frames, until the GPU has drawn them, take longer than// FrameBudget: 240 frames a second. It holds on the machine it runs on: to// check weaker hardware, run it there.func TestFrameBudget(t *testing.T) { if testing.Short() { t.Skip("measures frames for a few seconds") } for _, size := range budgetWindows { for _, s := range benchScreens { t.Run(fmt.Sprintf("%dx%d/%s", size.X, size.Y, s.name), func(t *testing.T) { frameBudget(t, size, s.fill, s.budgeted) }) } }}
func frameBudget(t *testing.T, size image.Point, fill func(cols, rows int) []byte, budgeted bool) { win := newTarget(t, size) h := fourPanes(t, size, fill) for range 10 { // warm the glyph caches gpuFrame(t, h, win) } times := make([]time.Duration, 120) for i := range times { start := time.Now() gpuFrame(t, h, win) times[i] = time.Since(start) } slices.Sort(times) median, p95, worst := times[len(times)/2], times[len(times)*95/100], times[len(times)-1] t.Logf("median %v, 95th percentile %v, worst %v: %.0f fps at the 95th percentile", median.Round(10*time.Microsecond), p95.Round(10*time.Microsecond), worst.Round(10*time.Microsecond), float64(time.Second)/float64(p95)) if p95 > FrameBudget { msg := fmt.Sprintf("1 frame in 20 takes %v or more; 240 fps needs %v", p95.Round(10*time.Microsecond), FrameBudget.Round(10*time.Microsecond)) if budgeted { t.Error(msg) } else { t.Log("over budget, not yet held to it: " + msg) } }}
// BenchmarkPaneDrag measures frames of a 1080p window of four full panes// while one pane is dragged by its header across the others: what moves// is the wash over the dragged pane, the drop zone and the chip.func BenchmarkPaneDrag(b *testing.B) { win := newTarget(b, benchWindow) h := fourPanes(b, benchWindow, sample.Code) send := func(kind pointer.Kind, p image.Point) { h.router.Queue(pointer.Event{Kind: kind, Source: pointer.Mouse, Position: f32.Pt(float32(p.X), float32(p.Y)), Buttons: pointer.ButtonPrimary}) } r := h.u.paneSlots[0].rect from := image.Pt(r.Min.X+r.Dx()/2, r.Min.Y+12) send(pointer.Press, from) gpuFrame(b, h, win) // Back and forth across the window, 2px a frame, as a quick drag. x, dx := from.X, 2 step := func() { x += dx if x < 20 || x > benchWindow.X-20 { dx = -dx } send(pointer.Move, image.Pt(x, benchWindow.Y/2)) gpuFrame(b, h, win) } for range 30 { step() } if !h.u.paneDrag.active { b.Fatal("the drag didn't start") } start := time.Now() for b.Loop() { step() } b.ReportMetric(float64(b.N)/time.Since(start).Seconds(), "fps")}
// BenchmarkFloatingDrag measures frames of a 1080p window of four full// panes, one of them floating, while the floating one is dragged by its// header across the others: what moves is a whole pane, over the others.func BenchmarkFloatingDrag(b *testing.B) { win := newTarget(b, benchWindow) h := fourPanes(b, benchWindow, sample.Code) block := h.u.paneSlots[3].block h.u.session.FloatBlock(block) h.until("a floating pane", func(f rexsession.Frame) bool { floating, _ := floatState(f.Window, block) return floating }) var r image.Rectangle for _, s := range h.u.paneSlots { if s.block == block { r = s.rect } } send := func(kind pointer.Kind, p image.Point) { h.router.Queue(pointer.Event{Kind: kind, Source: pointer.Mouse, Position: f32.Pt(float32(p.X), float32(p.Y)), Buttons: pointer.ButtonPrimary}) } from := r.Min.Add(image.Pt(30, 12)) send(pointer.Press, from) gpuFrame(b, h, win) // Back and forth, 2px a frame, as a quick drag. x, dx := from.X, -2 step := func() { x += dx if x < from.X-600 || x > from.X { dx = -dx } send(pointer.Move, image.Pt(x, from.Y+(from.X-x)/4)) gpuFrame(b, h, win) } for range 30 { step() } if !h.u.paneDrag.active || h.u.paneDrag.layer == "" { b.Fatal("the floating pane's drag didn't start") } start := time.Now() for b.Loop() { step() } b.ReportMetric(float64(b.N)/time.Since(start).Seconds(), "fps")}
// BenchmarkResize measures frames of a 1080p window of four full panes// while the window is resized, a few pixels a frame, so that every// pane's terminal reflows and every row is painted again.func BenchmarkResize(b *testing.B) { win := newTarget(b, benchWindow) h := fourPanes(b, benchWindow, sample.Code) w, dw := benchWindow.X, -4 step := func() { w += dw if w < benchWindow.X-400 || w > benchWindow.X { dw = -dw } h.size = image.Pt(w, benchWindow.Y) gpuFrame(b, h, win) } start := time.Now() for b.Loop() { step() } b.ReportMetric(float64(b.N)/time.Since(start).Seconds(), "fps")}