1package core
2
3import (
4 "context"
5 "fmt"
6 "testing"
7
8 "github.com/bnema/neferwl/internal/ports"
9)
10
11func benchLane(n int, stamped bool) *Workspace {
12 w := spanWorkspace(OverflowScroll, n)
13 if stamped {
14 stampSpans(w, 0, n-1)
15 }
16 w.Focus = n / 2
17 return w
18}
19
20func benchLanes(b *testing.B, f func(b *testing.B, w *Workspace)) {
21 for _, n := range []int{16, 64} {
22 for _, stamped := range []bool{false, true} {
23 b.Run(fmt.Sprintf("cols=%d/stamped=%t", n, stamped), func(b *testing.B) {
24 w := benchLane(n, stamped)
25 b.ReportAllocs()
26 f(b, w)
27 })
28 }
29 }
30}
31
32func BenchmarkSnapPoints(b *testing.B) {
33 benchLanes(b, func(b *testing.B, w *Workspace) {
34 for b.Loop() {
35 _ = w.snapPoints()
36 }
37 })
38}
39
40func BenchmarkLayout(b *testing.B) {
41 benchLanes(b, func(b *testing.B, w *Workspace) {
42 for b.Loop() {
43 _ = w.Layout()
44 }
45 })
46}
47
48func BenchmarkColumnRects(b *testing.B) {
49 benchLanes(b, func(b *testing.B, w *Workspace) {
50 var buf []Rect
51 for b.Loop() {
52 buf = w.columnRectsInto(buf, false)
53 }
54 })
55}
56
57func BenchmarkScroll(b *testing.B) {
58 benchLanes(b, func(b *testing.B, w *Workspace) {
59 for b.Loop() {
60 w.scroll()
61 }
62 })
63}
64
65// BenchmarkBorderMotion is one pointer motion of a border resize with a
66// publish; "same" repeats a position, so nothing changes after the first.
67func BenchmarkBorderMotion(b *testing.B) {
68 for _, mode := range []string{"moving", "same"} {
69 b.Run(mode, func(b *testing.B) {
70 var cfg ports.Config
71 cfg.Keyboard.CmdKey = "super"
72 cfg.Layout.MaxColumns = 3
73 scenes, cmds := make(chan []ports.Scene, 1), make(chan ports.ClientCommand, 1024)
74 c, err := New(cfg, Channels{Scenes: scenes, Commands: cmds}, Options{})
75 if err != nil {
76 b.Fatal(err)
77 }
78 c.addScreen(ports.OutputInfo{Name: "A", Width: 1920, Height: 1080})
79 sc := c.cur()
80 w := sc.mon.Current()
81 w.Overflow = OverflowScroll
82 for id := WindowID(1); id <= 12; id++ {
83 w.AddWindow(id)
84 }
85 sc.arrange()
86 ctx := context.Background()
87 if err := c.publish(ctx); err != nil {
88 b.Fatal(err)
89 }
90 rs := w.columnRects()
91 g := newBorderResize(c, sc, w, border{kind: borderColumn, col: 0}, btnRight)
92 x := float64(rs[0].X + rs[0].W)
93 c.cursorY = 100
94 n := 0
95 b.ReportAllocs()
96 for b.Loop() {
97 c.cursorX = x
98 if mode == "moving" {
99 c.cursorX += float64(n%2*8) + 8
100 n++
101 }
102 for len(scenes) > 0 {
103 <-scenes
104 }
105 for len(cmds) > 0 {
106 <-cmds
107 }
108 if err := g.motion(ctx, 0); err != nil {
109 b.Fatal(err)
110 }
111 }
112 })
113 }
114}