Documentation
¶
Overview ¶
Package sessionstats is experimental: its API and documented formats may change in a minor release; see https://nsgo.games/docs/stability/.
Package sessionstats records release-safe frame pacing and runtime memory metrics.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Recorder ¶
type Recorder struct {
// contains filtered or unexported fields
}
Recorder keeps a bounded frame-time sample window and release-safe runtime metrics.
Example ¶
package main
import (
"fmt"
"time"
"github.com/nsgo-dev/game/pkg/sessionstats"
)
func main() {
r := sessionstats.New(16 * time.Millisecond)
for _, ms := range []int{16, 16, 17, 16, 40} {
r.Observe(time.Duration(ms) * time.Millisecond)
}
// Frames while the app is in the background are not counted.
r.SetPaused(true)
r.Observe(500 * time.Millisecond)
r.SetPaused(false)
r.Observe(16 * time.Millisecond)
// Memory and GC figures depend on the runtime, so only the frame pacing is printed.
s := r.Summary()
fmt.Println("frames:", s.Frames)
fmt.Println("p50:", s.P50, "p95:", s.P95, "p99:", s.P99)
fmt.Println("longest:", s.Longest, "jank:", s.Jank2x)
}
Output: frames: 6 p50: 16ms p95: 17ms p99: 17ms longest: 40ms jank: 1
func New ¶
New returns a recorder for a target frame interval (16 ms when zero or negative), against which it counts frames that take twice as long.
func (*Recorder) Observe ¶
Observe records one foreground frame. It ignores non-positive durations and samples while paused.
type Summary ¶
type Summary struct {
Version int `json:"version" yaml:"version"`
Frames uint64 `json:"frames" yaml:"frames"`
P50 time.Duration `json:"p50" yaml:"p50"`
P95 time.Duration `json:"p95" yaml:"p95"`
P99 time.Duration `json:"p99" yaml:"p99"`
Longest time.Duration `json:"longest" yaml:"longest"`
Jank2x uint64 `json:"jank2x" yaml:"jank2x"`
HeapPeak uint64 `json:"heapPeak" yaml:"heapPeak"`
TotalMemory uint64 `json:"totalMemory" yaml:"totalMemory"`
GCCycles uint64 `json:"gcCycles" yaml:"gcCycles"`
MaxPause time.Duration `json:"maxPause" yaml:"maxPause"`
}
Summary is a deterministic snapshot of one recording session.
func (Summary) MarshalJSON ¶
MarshalJSON keeps field order stable for analytics payloads and snapshots.
Example ¶
package main
import (
"encoding/json"
"fmt"
"time"
"github.com/nsgo-dev/game/pkg/sessionstats"
)
func main() {
s := sessionstats.Summary{Version: 1, Frames: 3, P50: 2 * time.Millisecond, P95: 3 * time.Millisecond, P99: 3 * time.Millisecond, Longest: 3 * time.Millisecond, Jank2x: 1, HeapPeak: 12, TotalMemory: 34, GCCycles: 5, MaxPause: 6 * time.Millisecond}
b, err := json.Marshal(s)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(b))
}
Output: {"version":1,"frames":3,"p50":2000000,"p95":3000000,"p99":3000000,"longest":3000000,"jank2x":1,"heapPeak":12,"totalMemory":34,"gcCycles":5,"maxPause":6000000}