sessionstats

package
v0.4.0-rc.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 4, 2026 License: Apache-2.0 Imports: 4 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

func New(interval time.Duration) *Recorder

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

func (r *Recorder) Observe(dt time.Duration)

Observe records one foreground frame. It ignores non-positive durations and samples while paused.

func (*Recorder) SetPaused

func (r *Recorder) SetPaused(paused bool)

SetPaused excludes samples while the application is backgrounded.

func (*Recorder) Summary

func (r *Recorder) Summary() Summary

Summary returns the session so far: the p50, p95 and p99 frame times of the last 4096 frames, the longest frame, the count of frames at least twice the interval, and the peak heap, runtime memory, GC cycles and longest GC pause observed.

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

func (s Summary) MarshalJSON() ([]byte, error)

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}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL