apps/gophers: upgrade to new profiling api

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2019-07-12 15:36:38 +02:00
parent 86533ae683
commit 1336ca109a
2 changed files with 32 additions and 17 deletions
+1 -1
View File
@@ -3,7 +3,7 @@ module gioui.org/apps
go 1.12
require (
gioui.org/ui v0.0.0-20190712120518-326f78c3413a
gioui.org/ui v0.0.0-20190712123730-25a20af2843d
github.com/google/go-github/v24 v24.0.1
golang.org/x/exp v0.0.0-20190627132806-fd42eb6b336f
golang.org/x/image v0.0.0-20190703141733-d6a02ce849c9
+31 -16
View File
@@ -29,6 +29,7 @@ import (
"gioui.org/ui/f32"
"gioui.org/ui/gesture"
"gioui.org/ui/input"
"gioui.org/ui/input/system"
"gioui.org/ui/key"
"gioui.org/ui/layout"
"gioui.org/ui/measure"
@@ -67,6 +68,11 @@ type App struct {
ctx context.Context
ctxCancel context.CancelFunc
// Profiling.
profiling bool
profile system.ProfileEvent
lastMallocs uint64
}
type userPage struct {
@@ -182,9 +188,8 @@ func colorMaterial(ops *ui.Ops, color color.RGBA) ui.BlockOp {
}
func (a *App) run() error {
a.w.Profiling = *stats
a.profiling = *stats
ops := new(ui.Ops)
var lastMallocs uint64
for {
select {
case users := <-a.updateUsers:
@@ -200,7 +205,7 @@ func (a *App) run() error {
os.Exit(0)
case 'P':
if e.Modifiers.Contain(key.ModCommand) {
a.w.Profiling = !a.w.Profiling
a.profiling = !a.profiling
a.w.Redraw()
}
}
@@ -236,19 +241,8 @@ func (a *App) run() error {
a.cfg = e.Config
cs := layout.ExactConstraints(a.w.Size())
a.Layout(ops, cs)
if a.w.Profiling {
var mstats runtime.MemStats
runtime.ReadMemStats(&mstats)
mallocs := mstats.Mallocs - lastMallocs
lastMallocs = mstats.Mallocs
al := layout.Align{Alignment: layout.NE}
cs := al.Begin(ops, cs)
in := layout.Insets{Top: ui.Dp(16)}
cs = in.Begin(&a.cfg, ops, cs)
txt := fmt.Sprintf("m: %d %s", mallocs, a.w.Timings())
dims := text.Label{Material: theme.text, Face: a.face(fonts.mono, 10), Text: txt}.Layout(ops, cs)
dims = in.End(dims)
al.End(dims)
if a.profiling {
a.layoutTimings(ops, cs)
}
a.w.Draw(ops)
a.faces.Frame()
@@ -396,6 +390,27 @@ func (a *App) face(f *sfnt.Font, size float32) text.Face {
return a.faces.For(f, ui.Sp(size))
}
func (a *App) layoutTimings(ops *ui.Ops, cs layout.Constraints) layout.Dimens {
for _, e := range a.inputs.Events(a) {
if e, ok := e.(system.ProfileEvent); ok {
a.profile = e
}
}
system.ProfileOp{Key: a}.Add(ops)
var mstats runtime.MemStats
runtime.ReadMemStats(&mstats)
mallocs := mstats.Mallocs - a.lastMallocs
a.lastMallocs = mstats.Mallocs
al := layout.Align{Alignment: layout.NE}
cs = al.Begin(ops, cs)
in := layout.Insets{Top: ui.Dp(16)}
cs = in.Begin(&a.cfg, ops, cs)
txt := fmt.Sprintf("m: %d %s", mallocs, a.profile.Timings)
dims := text.Label{Material: theme.text, Face: a.face(fonts.mono, 10), Text: txt}.Layout(ops, cs)
dims = in.End(dims)
return al.End(dims)
}
func (a *App) Layout(ops *ui.Ops, cs layout.Constraints) layout.Dimens {
for i := range a.userClicks {
click := &a.userClicks[i]