feat: port the client to MyGo native UI (0.3.0)
Switch from Asuka to MyGo, keeping the API client and the feature set: - servers: saved servers, passwordless and password login, proxy, self-signed certificates, per-server device tokens (Keystore), connect form, instance list sheet with switch/disconnect - sessions: virtualized list, search, new session, rename/archive/delete, running indicator, event stream keeps it live - chat: pushed window with system back bar, streaming Markdown via ui/markdown, reasoning and tool rows with image previews, footer menu (read aloud via a Kotlin TTS plugin, copy, fork), user message menu (copy, revert, fork), context usage ring and detail sheet, attachments (photos/files via the document picker), permissions - release builds allow plain http:// LAN servers (Go's net/http dials directly, no cleartext manifest needed) - a local fork of MyGo (third_party/mygo) lowers the Android host's minSdk to 24 (Android 7.0); Go 1.27.4 via mise Version 0.3.0.
This commit is contained in:
1 parent
2fb4a8ea79
commit
6c0ee61750
2538 files changed
+675779
-4792
No files matched your search
+181
@@ -0,0 +1,181 @@
|
||||
// Command inputlatency measures how long input takes to show on screen in
|
||||
// native UI on macOS: from when an event arrives to when the frame showing
|
||||
// it reaches the display. It needs a desktop session.
|
||||
//
|
||||
// go run ./internal/inputlatency [-mode type,scroll,drag,anim] [-runs 8] [-events 300]
|
||||
//
|
||||
// Each run is a process of its own with a window of native UI, shown
|
||||
// without activating the app, so it takes neither the keyboard nor the
|
||||
// front: typing into a text area (a key every 60 to 140 ms, each frame
|
||||
// after a pause), scrolling a list of a million rows with the trackpad
|
||||
// (an event every 4 to 12 ms), dragging a box (the same), or animating a
|
||||
// box with no input (anim), or boxes casting wide shadows (30, -boxes),
|
||||
// which keep the GPU busy (heavy). Events come
|
||||
// from another goroutine through the main run loop, as the window
|
||||
// server's do, at any time within the display's refresh, and go through
|
||||
// the window as AppKit's (sendEvent:, scrollWheel:); an event is taken to
|
||||
// arrive when it is posted, so the time the main thread was busy counts.
|
||||
//
|
||||
// The frame showing an event is the next one the main thread presents
|
||||
// after handling it: the main thread handles events and builds frames in
|
||||
// turn. Its drawable's presented handler gives when it reached the screen
|
||||
// (presentedTime). The tool swizzles the presents (the command buffer's
|
||||
// presentDrawable:, the drawable's present), -[CAMetalLayer nextDrawable]
|
||||
// for the main thread's waits, and the surface view's mygoTick: and
|
||||
// updateLayer (internal/darwin) for when frames start, and fails if they
|
||||
// are gone.
|
||||
//
|
||||
// Each run prints the latency's mean and percentiles in milliseconds, and
|
||||
// the means of what it is made of: waiting for the main thread (queue),
|
||||
// handling the event (handle), from then to the frame's present (waiting
|
||||
// for the display link's next tick, or the end of the run loop's pass
|
||||
// after a pause, building and painting, waiting for a drawable), and from
|
||||
// the present to the screen (the GPU, the window server's composite and
|
||||
// the display). And of the frames: how long the main thread waited for
|
||||
// drawables a frame, and how often more than 4 ms; what share of the
|
||||
// refreshes showed a new frame (all of them for -mode anim, an animation
|
||||
// with no input, but for frames not drawn in time); and what share of the
|
||||
// frames presented never showed, replaced by the next. The summary gives the median of the runs' figures: a run
|
||||
// keeps the state the window server settles in, which changes from run
|
||||
// to run, so compare medians of several runs, alternating builds.
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
modes := flag.String("mode", "type,scroll,drag,anim", "what the runs do: type, scroll, drag, anim, heavy, separated by commas")
|
||||
runs := flag.Int("runs", 6, "how many runs of each mode")
|
||||
events := flag.Int("events", 300, "events measured in a run, after warmup (refreshes for anim and heavy)")
|
||||
warmup := flag.Int("warmup", 30, "events not measured at the start of a run (refreshes for anim and heavy)")
|
||||
x := flag.Int("x", 0, "where the window opens, as WindowOptions.X")
|
||||
y := flag.Int("y", 0, "where the window opens, as WindowOptions.Y")
|
||||
boxes := flag.Int("boxes", 30, "how many boxes heavy draws")
|
||||
child := flag.String("run", "", "run one measurement of this mode in this process, printing its figures")
|
||||
flag.Parse()
|
||||
if *child != "" {
|
||||
if err := measure(*child, *events, *warmup, *x, *y, *boxes); err != nil {
|
||||
fmt.Fprintln(os.Stderr, "inputlatency:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
return
|
||||
}
|
||||
self, err := os.Executable()
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "inputlatency:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
for mode := range strings.SplitSeq(*modes, ",") {
|
||||
var all []figures
|
||||
for i := range *runs {
|
||||
out, err := exec.Command(self, "-run", mode, "-events", strconv.Itoa(*events), "-warmup", strconv.Itoa(*warmup),
|
||||
"-x", strconv.Itoa(*x), "-y", strconv.Itoa(*y), "-boxes", strconv.Itoa(*boxes)).Output()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "inputlatency: %s run %d: %v\n", mode, i+1, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
f, err := parseFigures(string(out))
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "inputlatency: %s run %d: %v\n", mode, i+1, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Printf("%-6s run %d: %s\n", mode, i+1, f)
|
||||
all = append(all, f)
|
||||
}
|
||||
fmt.Printf("%-6s median of %d runs: %s\n", mode, len(all), medianFigures(all))
|
||||
}
|
||||
}
|
||||
|
||||
// figures are a run's, in milliseconds but for refresh (Hz) and events.
|
||||
type figures struct {
|
||||
refresh, events float64
|
||||
mean, p50, p90, p99 float64
|
||||
queue, handle, toPresent, toScreen float64 // means
|
||||
// The frames': the main thread's wait for drawables a frame, the
|
||||
// percent of frames it waited over 4 ms, the percent of refreshes
|
||||
// showing a new frame, and of frames presented and never shown.
|
||||
mainWait, blocked, newFrames, notShown float64
|
||||
}
|
||||
|
||||
var figureNames = []string{"refresh", "events", "mean", "p50", "p90", "p99", "queue", "handle", "toPresent", "toScreen", "mainWait", "blocked", "newFrames", "notShown"}
|
||||
|
||||
func (f *figures) values() []*float64 {
|
||||
return []*float64{&f.refresh, &f.events, &f.mean, &f.p50, &f.p90, &f.p99, &f.queue, &f.handle, &f.toPresent, &f.toScreen,
|
||||
&f.mainWait, &f.blocked, &f.newFrames, &f.notShown}
|
||||
}
|
||||
|
||||
func (f figures) String() string {
|
||||
return fmt.Sprintf("mean %5.1f p50 %5.1f p90 %5.1f p99 %5.1f ms | queue %3.1f handle %3.1f to present %4.1f to screen %4.1f | main waits %4.2f ms a frame, %4.1f%% over 4 ms | %5.1f%% of refreshes new, %3.1f%% not shown | %.0f Hz, %.0f events",
|
||||
f.mean, f.p50, f.p90, f.p99, f.queue, f.handle, f.toPresent, f.toScreen, f.mainWait, f.blocked, f.newFrames, f.notShown, f.refresh, f.events)
|
||||
}
|
||||
|
||||
// line is how a run hands its figures to the parent process.
|
||||
func (f figures) line() string {
|
||||
var b strings.Builder
|
||||
b.WriteString("figures")
|
||||
for i, v := range f.values() {
|
||||
fmt.Fprintf(&b, " %s=%.3f", figureNames[i], *v)
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func parseFigures(out string) (figures, error) {
|
||||
var f figures
|
||||
for line := range strings.SplitSeq(out, "\n") {
|
||||
rest, ok := strings.CutPrefix(line, "figures ")
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
vals := f.values()
|
||||
for field := range strings.FieldsSeq(rest) {
|
||||
name, v, _ := strings.Cut(field, "=")
|
||||
i := slices.Index(figureNames, name)
|
||||
n, err := strconv.ParseFloat(v, 64)
|
||||
if i < 0 || err != nil {
|
||||
return f, fmt.Errorf("bad figure %q", field)
|
||||
}
|
||||
*vals[i] = n
|
||||
}
|
||||
return f, nil
|
||||
}
|
||||
return f, fmt.Errorf("no figures in %q", out)
|
||||
}
|
||||
|
||||
func medianFigures(all []figures) figures {
|
||||
var m figures
|
||||
for i, v := range m.values() {
|
||||
var xs []float64
|
||||
for _, f := range all {
|
||||
xs = append(xs, *f.values()[i])
|
||||
}
|
||||
*v = median(xs)
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
func median(xs []float64) float64 {
|
||||
if len(xs) == 0 {
|
||||
return 0
|
||||
}
|
||||
xs = slices.Clone(xs)
|
||||
slices.Sort(xs)
|
||||
if n := len(xs); n%2 == 0 {
|
||||
return (xs[n/2-1] + xs[n/2]) / 2
|
||||
}
|
||||
return xs[len(xs)/2]
|
||||
}
|
||||
|
||||
// percentile returns the p-th of sorted xs, 0 ≤ p ≤ 1.
|
||||
func percentile(sorted []float64, p float64) float64 {
|
||||
if len(sorted) == 0 {
|
||||
return 0
|
||||
}
|
||||
return sorted[int(p*float64(len(sorted)-1))]
|
||||
}
|
||||
@@ -0,0 +1,511 @@
|
||||
//go:build !ios
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
"math/rand/v2"
|
||||
"slices"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/ebitengine/purego"
|
||||
"github.com/ebitengine/purego/objc"
|
||||
|
||||
"github.com/egoist/mygo"
|
||||
"github.com/egoist/mygo/ui"
|
||||
)
|
||||
|
||||
type nsPoint struct{ X, Y float64 }
|
||||
|
||||
type nsRect struct{ X, Y, W, H float64 }
|
||||
|
||||
var (
|
||||
mediaTime func() float64
|
||||
getMethod func(cls, sel uintptr) uintptr
|
||||
setImp func(m, imp uintptr) uintptr
|
||||
keyEvent func(cls, sel uintptr, typ uint, loc nsPoint, flags uint, ts float64, window int, ctx, chars, unmodified uintptr, repeat bool, code uint16) uintptr
|
||||
mouseEvent func(cls, sel uintptr, typ uint, loc nsPoint, flags uint, ts float64, window int, ctx uintptr, number, clicks int, pressure float32) uintptr
|
||||
convertPoint func(obj, sel uintptr, p nsPoint, view uintptr) nsPoint
|
||||
scrollEvent func(source uintptr, units, count uint32, dy, dx, dz int32) uintptr
|
||||
setField func(event uintptr, field uint32, v int64)
|
||||
setLocation func(event uintptr, p nsPoint)
|
||||
cfRelease func(uintptr)
|
||||
createDevice func() uintptr
|
||||
getClass func(obj uintptr) uintptr
|
||||
isMain func() int32
|
||||
)
|
||||
|
||||
func load() error {
|
||||
libs := map[string]uintptr{}
|
||||
for _, path := range []string{
|
||||
"/usr/lib/libobjc.A.dylib",
|
||||
"/System/Library/Frameworks/QuartzCore.framework/QuartzCore",
|
||||
"/System/Library/Frameworks/CoreGraphics.framework/CoreGraphics",
|
||||
"/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation",
|
||||
"/System/Library/Frameworks/Metal.framework/Metal",
|
||||
"/usr/lib/libSystem.B.dylib",
|
||||
} {
|
||||
h, err := purego.Dlopen(path, purego.RTLD_GLOBAL|purego.RTLD_NOW)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
libs[path[strings.LastIndex(path, "/")+1:]] = h
|
||||
}
|
||||
objcLib, cg := libs["libobjc.A.dylib"], libs["CoreGraphics"]
|
||||
purego.RegisterLibFunc(&mediaTime, libs["QuartzCore"], "CACurrentMediaTime")
|
||||
purego.RegisterLibFunc(&getMethod, objcLib, "class_getInstanceMethod")
|
||||
purego.RegisterLibFunc(&setImp, objcLib, "method_setImplementation")
|
||||
purego.RegisterLibFunc(&scrollEvent, cg, "CGEventCreateScrollWheelEvent2")
|
||||
purego.RegisterLibFunc(&setField, cg, "CGEventSetIntegerValueField")
|
||||
purego.RegisterLibFunc(&setLocation, cg, "CGEventSetLocation")
|
||||
purego.RegisterLibFunc(&cfRelease, libs["CoreFoundation"], "CFRelease")
|
||||
purego.RegisterLibFunc(&createDevice, libs["Metal"], "MTLCreateSystemDefaultDevice")
|
||||
purego.RegisterLibFunc(&getClass, objcLib, "object_getClass")
|
||||
purego.RegisterLibFunc(&isMain, libs["libSystem.B.dylib"], "pthread_main_np")
|
||||
send, err := purego.Dlsym(objcLib, "objc_msgSend")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
purego.RegisterFunc(&keyEvent, send)
|
||||
purego.RegisterFunc(&mouseEvent, send)
|
||||
purego.RegisterFunc(&convertPoint, send)
|
||||
return nil
|
||||
}
|
||||
|
||||
func sel(name string) objc.SEL { return objc.RegisterName(name) }
|
||||
|
||||
// An event: when it arrived, and when the main thread began and finished
|
||||
// handling it.
|
||||
type event struct{ arrived, handling, handled float64 }
|
||||
|
||||
// A frame: when it started (the tick or display that drew it), when the
|
||||
// main thread presented it, and the events it shows.
|
||||
type frame struct {
|
||||
start, presented float64
|
||||
events []event
|
||||
}
|
||||
|
||||
// A measured event, its frame, and when that reached the screen.
|
||||
type sample struct {
|
||||
event
|
||||
frame *frame
|
||||
shown float64
|
||||
}
|
||||
|
||||
var (
|
||||
mu sync.Mutex
|
||||
measuring bool
|
||||
pending []event // handled since the last frame presented
|
||||
frames = map[uint64]*frame{} // by drawableID, until shown
|
||||
samples []sample
|
||||
shown []float64 // when the frames presented while measuring were shown
|
||||
presents int // frames presented while measuring
|
||||
notShown int // of them
|
||||
// The main thread's waits for drawables while measuring, in seconds.
|
||||
mainWaits []float64
|
||||
// On the main thread: when the frame being drawn started.
|
||||
frameStart float64
|
||||
presented objc.Block
|
||||
|
||||
origNext, origPresentIn, origPresent, origTick, origUpdate uintptr
|
||||
)
|
||||
|
||||
// swizzle wraps -[CAMetalLayer nextDrawable], the presents of drawables,
|
||||
// and the surface view's frame methods.
|
||||
func swizzle() error {
|
||||
wrap := func(class uintptr, name string, fn any) (uintptr, error) {
|
||||
m := getMethod(class, uintptr(sel(name)))
|
||||
if m == 0 {
|
||||
return 0, fmt.Errorf("no method %s", name)
|
||||
}
|
||||
return setImp(m, purego.NewCallback(fn)), nil
|
||||
}
|
||||
var err error
|
||||
origNext, err = wrap(uintptr(objc.GetClass("CAMetalLayer")), "nextDrawable", func(self, cmd uintptr) uintptr {
|
||||
asked := mediaTime()
|
||||
d, _, _ := purego.SyscallN(origNext, self, cmd)
|
||||
if isMain() != 0 {
|
||||
mu.Lock()
|
||||
if measuring {
|
||||
mainWaits = append(mainWaits, mediaTime()-asked)
|
||||
}
|
||||
mu.Unlock()
|
||||
}
|
||||
return d
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// The frame showing the events handled until it presents: frames
|
||||
// present on the main thread, as soon as drawn (the command buffer's
|
||||
// presentDrawable:) or with the transaction (the drawable's present).
|
||||
queue := objc.ID(createDevice()).Send(sel("newCommandQueue"))
|
||||
buffer := queue.Send(sel("commandBuffer"))
|
||||
origPresentIn, err = wrap(uintptr(getClass(uintptr(buffer))), "presentDrawable:", func(self, cmd, d uintptr) {
|
||||
present(d)
|
||||
purego.SyscallN(origPresentIn, self, cmd, d)
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
origPresent, err = wrap(uintptr(objc.GetClass("CAMetalDrawable")), "present", func(self, cmd uintptr) {
|
||||
present(self)
|
||||
purego.SyscallN(origPresent, self, cmd)
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
view := uintptr(objc.GetClass("MyGoSurfaceView"))
|
||||
origTick, err = wrap(view, "mygoTick:", func(self, cmd, link uintptr) {
|
||||
frameStart = mediaTime()
|
||||
purego.SyscallN(origTick, self, cmd, link)
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
origUpdate, err = wrap(view, "updateLayer", func(self, cmd uintptr) {
|
||||
frameStart = mediaTime()
|
||||
purego.SyscallN(origUpdate, self, cmd)
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
// present notes the frame presenting drawable d, once: a command
|
||||
// buffer's presentDrawable: may present it with present.
|
||||
func present(d uintptr) {
|
||||
f := &frame{start: frameStart, presented: mediaTime()}
|
||||
id := objc.Send[uint64](objc.ID(d), sel("drawableID"))
|
||||
mu.Lock()
|
||||
if frames[id] != nil {
|
||||
mu.Unlock()
|
||||
return
|
||||
}
|
||||
f.events, pending = pending, nil
|
||||
frames[id] = f
|
||||
if measuring {
|
||||
presents++
|
||||
}
|
||||
mu.Unlock()
|
||||
objc.ID(d).Send(sel("addPresentedHandler:"), presented)
|
||||
}
|
||||
|
||||
func init() {
|
||||
presented = objc.NewBlock(func(_ objc.Block, d objc.ID) {
|
||||
at := objc.Send[float64](d, sel("presentedTime"))
|
||||
id := objc.Send[uint64](d, sel("drawableID"))
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
f := frames[id]
|
||||
delete(frames, id)
|
||||
if f == nil || !measuring {
|
||||
return
|
||||
}
|
||||
if at == 0 {
|
||||
// Another frame replaced it before the screen showed it,
|
||||
// showing its events too: they are not counted.
|
||||
notShown++
|
||||
return
|
||||
}
|
||||
shown = append(shown, at)
|
||||
for _, e := range f.events {
|
||||
samples = append(samples, sample{e, f, at})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// The window's state.
|
||||
var (
|
||||
text = strings.Repeat("The quick brown fox jumps over the lazy dog. ", 40)
|
||||
list ui.ListState
|
||||
offset ui.Point
|
||||
mode string
|
||||
window *mygo.Window
|
||||
nsWindow uintptr
|
||||
surface uintptr
|
||||
errResult error
|
||||
)
|
||||
|
||||
func view(c *ui.Context) {
|
||||
switch mode {
|
||||
case "type":
|
||||
ui.Column(c).Fill().Padding(16).Children(func() {
|
||||
ui.TextArea(c, &text).AutoFocus().Grow(1)
|
||||
})
|
||||
case "scroll":
|
||||
ui.List(c, &list, 1_000_000, func(i int) {
|
||||
ui.Row(c).Height(float32(28 + i%3*8)).Gap(8).Padding(4).Children(func() {
|
||||
ui.Textf(c, "Row %d", i).Grow(1)
|
||||
ui.Text(c, "detail").TextColor(c.Theme().TextMuted)
|
||||
})
|
||||
}).Grow(1)
|
||||
case "anim":
|
||||
ui.Box(c).Fill().Draw(func(p *ui.Painter, r ui.Rect) {
|
||||
// A box crossing the window every second, a frame each refresh.
|
||||
x := float32(p.Now().UnixNano()%1e9) / 1e9 * (r.W - 80)
|
||||
p.Fill(ui.Rect{X: r.X + x, Y: r.Y + 100, W: 80, H: 80}, c.Theme().Accent, 8)
|
||||
p.AnimationFrame()
|
||||
})
|
||||
case "heavy":
|
||||
ui.Box(c).Fill().Draw(func(p *ui.Painter, r ui.Rect) {
|
||||
// The same with large boxes casting wide shadows, which keep
|
||||
// the GPU busy several milliseconds a frame.
|
||||
t := float32(p.Now().UnixNano()%1e9) / 1e9
|
||||
for i := range boxes {
|
||||
x := float32(i%20)*36 + t*80
|
||||
y := float32(i/20)*50 + 20
|
||||
p.Shadow(ui.Rect{X: r.X + x, Y: r.Y + y, W: 160, H: 120}, 16, 0, 8, 48, 0, ui.RGBA(0, 0, 0, 0.3))
|
||||
p.Fill(ui.Rect{X: r.X + x, Y: r.Y + y, W: 160, H: 120}, c.Theme().Accent, 16)
|
||||
}
|
||||
p.AnimationFrame()
|
||||
})
|
||||
case "drag":
|
||||
ui.Box(c).Fill().Children(func() {
|
||||
ui.Column(c).Fill().Children(func() {
|
||||
for i := range 30 {
|
||||
ui.Textf(c, "Row %d of the content under the box", i)
|
||||
}
|
||||
})
|
||||
ui.Box(c).Absolute().Width(80).Height(80).Background(c.Theme().Accent).Draggable(&offset, ui.DraggableOptions{})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// boxes is how many boxes heavy draws.
|
||||
var boxes int
|
||||
|
||||
func measure(m string, events, warmup, x, y, n int) error {
|
||||
boxes = n
|
||||
switch m {
|
||||
case "type", "scroll", "drag", "anim", "heavy":
|
||||
default:
|
||||
return fmt.Errorf("unknown mode %q", m)
|
||||
}
|
||||
mode = m
|
||||
if err := load(); err != nil {
|
||||
return err
|
||||
}
|
||||
// Out of the user's way: the app is not activated, and the window
|
||||
// neither takes the keyboard nor comes to the front of the others.
|
||||
mygo.App.SetActivationPolicy(mygo.ActivationPolicyAccessory)
|
||||
mygo.App.WhenReady(func() {
|
||||
window = mygo.NewWindow(mygo.WindowOptions{Title: "inputlatency", Width: 800, Height: 600, X: x, Y: y, Hidden: true, Content: ui.View(view)})
|
||||
nsWindow = window.NativeHandle()
|
||||
// As the app the user types into, which is active: App Nap would
|
||||
// delay the inactive app's wakeups by milliseconds.
|
||||
const userInitiated, latencyCritical = 0x00EFFFFF, 0xFF00000000
|
||||
reason := objc.ID(objc.GetClass("NSString")).Send(sel("stringWithUTF8String:"), "measuring input latency\x00")
|
||||
objc.ID(objc.GetClass("NSProcessInfo")).Send(sel("processInfo")).Send(sel("beginActivityWithOptions:reason:"), uint64(userInitiated|latencyCritical), reason).Send(sel("retain"))
|
||||
objc.ID(nsWindow).Send(sel("orderFrontRegardless"))
|
||||
go func() {
|
||||
errResult = run(events, warmup)
|
||||
mygo.App.Quit()
|
||||
}()
|
||||
})
|
||||
if err := mygo.App.Run(); err != nil {
|
||||
return err
|
||||
}
|
||||
return errResult
|
||||
}
|
||||
|
||||
func findSurface(v objc.ID) objc.ID {
|
||||
if objc.Send[bool](v, sel("isKindOfClass:"), objc.GetClass("MyGoSurfaceView")) {
|
||||
return v
|
||||
}
|
||||
subviews := v.Send(sel("subviews"))
|
||||
for i := range objc.Send[int](subviews, sel("count")) {
|
||||
if s := findSurface(subviews.Send(sel("objectAtIndex:"), i)); s != 0 {
|
||||
return s
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// inject has the main thread handle an event arriving now.
|
||||
func inject(handle func()) {
|
||||
e := event{arrived: mediaTime()}
|
||||
mygo.RunOnMain(func() {
|
||||
e.handling = mediaTime()
|
||||
handle()
|
||||
e.handled = mediaTime()
|
||||
mu.Lock()
|
||||
pending = append(pending, e)
|
||||
mu.Unlock()
|
||||
})
|
||||
}
|
||||
|
||||
func windowPoint(x, y float64) nsPoint {
|
||||
return convertPoint(surface, uintptr(sel("convertPoint:toView:")), nsPoint{x, y}, 0)
|
||||
}
|
||||
|
||||
func sendKey() {
|
||||
number := objc.Send[int](objc.ID(nsWindow), sel("windowNumber"))
|
||||
s := objc.ID(objc.GetClass("NSString")).Send(sel("stringWithUTF8String:"), "a\x00")
|
||||
for _, typ := range []uint{10, 11} { // NSEventTypeKeyDown, KeyUp
|
||||
ev := keyEvent(uintptr(objc.GetClass("NSEvent")), uintptr(sel("keyEventWithType:location:modifierFlags:timestamp:windowNumber:context:characters:charactersIgnoringModifiers:isARepeat:keyCode:")),
|
||||
typ, nsPoint{}, 0, mediaTime(), number, 0, uintptr(s), uintptr(s), false, 0)
|
||||
objc.ID(nsWindow).Send(sel("sendEvent:"), ev)
|
||||
}
|
||||
}
|
||||
|
||||
func sendMouse(typ uint, x, y float64) {
|
||||
number := objc.Send[int](objc.ID(nsWindow), sel("windowNumber"))
|
||||
ev := mouseEvent(uintptr(objc.GetClass("NSEvent")), uintptr(sel("mouseEventWithType:location:modifierFlags:timestamp:windowNumber:context:eventNumber:clickCount:pressure:")),
|
||||
typ, windowPoint(x, y), 0, mediaTime(), number, 0, 0, 1, 1)
|
||||
objc.ID(nsWindow).Send(sel("sendEvent:"), ev)
|
||||
}
|
||||
|
||||
// sendScroll scrolls as a trackpad does, by dy points, in a phase of the
|
||||
// gesture (kCGScrollPhase: 1 began, 2 changed, 4 ended).
|
||||
func sendScroll(phase int64, dy int32) {
|
||||
ev := scrollEvent(0, 0, 1, dy, 0, 0) // in pixels
|
||||
setField(ev, 88, 1) // kCGScrollWheelEventIsContinuous
|
||||
setField(ev, 99, phase) // kCGScrollWheelEventScrollPhase
|
||||
setField(ev, 96, int64(dy)) // kCGScrollWheelEventPointDeltaAxis1
|
||||
// A window-less event's location is on the screen, flipped by Core
|
||||
// Graphics: its locationInWindow is then this point.
|
||||
p := windowPoint(300, 300)
|
||||
screen := objc.ID(objc.GetClass("NSScreen")).Send(sel("screens")).Send(sel("objectAtIndex:"), 0)
|
||||
setLocation(ev, nsPoint{p.X, objc.Send[nsRect](screen, sel("frame")).H - p.Y})
|
||||
objc.ID(surface).Send(sel("scrollWheel:"), objc.ID(objc.GetClass("NSEvent")).Send(sel("eventWithCGEvent:"), ev))
|
||||
cfRelease(ev)
|
||||
}
|
||||
|
||||
func run(events, warmup int) error {
|
||||
// The first frames, at full speed.
|
||||
time.Sleep(1500 * time.Millisecond)
|
||||
var err error
|
||||
var refresh float64
|
||||
mygo.RunOnMain(func() {
|
||||
surface = uintptr(findSurface(objc.ID(nsWindow).Send(sel("contentView"))))
|
||||
if surface == 0 {
|
||||
err = errors.New("no MyGoSurfaceView in the window")
|
||||
return
|
||||
}
|
||||
refresh = float64(objc.Send[int](objc.ID(nsWindow).Send(sel("screen")), sel("maximumFramesPerSecond")))
|
||||
err = swizzle()
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
between := func(lo, hi float64) time.Duration {
|
||||
return time.Duration((lo + rand.Float64()*(hi-lo)) * float64(time.Millisecond))
|
||||
}
|
||||
n := 0
|
||||
next := func() bool {
|
||||
if n == warmup {
|
||||
mu.Lock()
|
||||
measuring = true
|
||||
mu.Unlock()
|
||||
}
|
||||
n++
|
||||
return n <= warmup+events
|
||||
}
|
||||
switch mode {
|
||||
case "type":
|
||||
for next() {
|
||||
inject(sendKey)
|
||||
time.Sleep(between(60, 140))
|
||||
}
|
||||
case "scroll":
|
||||
for n < warmup+events {
|
||||
mygo.RunOnMain(func() { sendScroll(1, -2) })
|
||||
for range 60 {
|
||||
if !next() {
|
||||
break
|
||||
}
|
||||
inject(func() { sendScroll(2, -3) })
|
||||
time.Sleep(between(4, 12))
|
||||
}
|
||||
mygo.RunOnMain(func() { sendScroll(4, 0) })
|
||||
time.Sleep(300 * time.Millisecond)
|
||||
}
|
||||
case "anim", "heavy":
|
||||
// warmup and events count refreshes.
|
||||
time.Sleep(time.Duration(float64(warmup) / refresh * float64(time.Second)))
|
||||
mu.Lock()
|
||||
measuring = true
|
||||
mu.Unlock()
|
||||
time.Sleep(time.Duration(float64(events) / refresh * float64(time.Second)))
|
||||
case "drag":
|
||||
for n < warmup+events {
|
||||
var x, y float64
|
||||
mygo.RunOnMain(func() {
|
||||
x, y = 40+float64(offset.X), 40+float64(offset.Y)
|
||||
sendMouse(1, x, y) // NSEventTypeLeftMouseDown
|
||||
})
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
for i := range 60 {
|
||||
if !next() {
|
||||
break
|
||||
}
|
||||
if i < 30 {
|
||||
x += 3
|
||||
} else {
|
||||
x -= 3
|
||||
}
|
||||
px := x
|
||||
inject(func() { sendMouse(6, px, y) }) // NSEventTypeLeftMouseDragged
|
||||
time.Sleep(between(4, 12))
|
||||
}
|
||||
mygo.RunOnMain(func() { sendMouse(2, x, y) }) // NSEventTypeLeftMouseUp
|
||||
time.Sleep(300 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
// The last frames reach the screen.
|
||||
time.Sleep(300 * time.Millisecond)
|
||||
mu.Lock()
|
||||
measuring = false
|
||||
ss, at, waits, np, ns := samples, shown, mainWaits, presents, notShown
|
||||
mu.Unlock()
|
||||
if np == 0 {
|
||||
return errors.New("no frame presented")
|
||||
}
|
||||
var f figures
|
||||
f.refresh, f.events = refresh, float64(len(ss))
|
||||
// The frames: how long the main thread waited for drawables, and how
|
||||
// many refreshes, from the first frame shown to the last, showed a new
|
||||
// one (all, for an animation, but for frames not drawn in time).
|
||||
for _, w := range waits {
|
||||
f.mainWait += w * 1000 / float64(np)
|
||||
if w > 0.004 {
|
||||
f.blocked += 100 / float64(np)
|
||||
}
|
||||
}
|
||||
f.notShown = 100 * float64(ns) / float64(np)
|
||||
if len(at) > 1 && refresh > 0 {
|
||||
slices.Sort(at)
|
||||
refreshes := map[int64]bool{}
|
||||
for _, t := range at {
|
||||
refreshes[int64(math.Round(t*refresh))] = true
|
||||
}
|
||||
span := math.Round((at[len(at)-1]-at[0])*refresh) + 1
|
||||
f.newFrames = 100 * float64(len(refreshes)) / span
|
||||
}
|
||||
if len(ss) == 0 {
|
||||
if mode != "anim" && mode != "heavy" {
|
||||
return errors.New("no event reached the screen")
|
||||
}
|
||||
fmt.Println(f.line())
|
||||
return nil
|
||||
}
|
||||
var total []float64
|
||||
k := float64(len(ss))
|
||||
for _, s := range ss {
|
||||
total = append(total, (s.shown-s.arrived)*1000)
|
||||
f.queue += (s.handling - s.arrived) * 1000 / k
|
||||
f.handle += (s.handled - s.handling) * 1000 / k
|
||||
f.toPresent += (s.frame.presented - s.handled) * 1000 / k
|
||||
f.toScreen += (s.shown - s.frame.presented) * 1000 / k
|
||||
}
|
||||
slices.Sort(total)
|
||||
for _, t := range total {
|
||||
f.mean += t / k
|
||||
}
|
||||
f.p50, f.p90, f.p99 = percentile(total, .5), percentile(total, .9), percentile(total, .99)
|
||||
fmt.Println(f.line())
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
//go:build !(darwin && !ios)
|
||||
|
||||
package main
|
||||
|
||||
import "errors"
|
||||
|
||||
func measure(mode string, events, warmup, x, y, boxes int) error {
|
||||
return errors.New("measures macOS only")
|
||||
}
|
||||
Reference in new issue
Block a user