loadtest

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2026 License: MIT Imports: 15 Imported by: ? 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 loadtest is a game-agnostic WebSocket load-test harness: slots and ramp, histograms, per-second rates, disconnect reasons and the player loop (player.go).

Index

Constants

View Source
const HistMax = 1000

HistMax is the last finite bucket bound; slower observations land in the overflow bucket and read as HistMax+1 ms.

Variables

This section is empty.

Functions

func HumansPerRoom

func HumansPerRoom(players, rooms int) int

HumansPerRoom is the most humans any one room gets from Assign.

func Line

func Line(prev, cur Sample) string

line is the per-interval report between two samples. "in" is what the players received (the server's outbound), "out" what they sent; rx/client is the server's outbound bandwidth per connected player.

func PerClientKB

func PerClientKB(bytesPerSec float64, conns int64) float64

func Reason

func Reason(err error, serverMsg string) string

reason names why a player's socket ended: the server's error message if it sent one, else the close status or the kind of read error.

func Run

func Run(ctx context.Context, c Config, sc Script) error

Run starts every player on its ramp slot and reports until the run ends. The game's cadence has no default: a wrong SnapEvery miscounts gaps silently.

func SnapTick

func SnapTick(b []byte) (tick int, ok bool)

SnapTick reads a snapshot's tick from its prefix without decoding the rest; ok is false for anything else (decode it in full).

func StartAt

func StartAt(i, n int, ramp time.Duration) time.Duration

StartAt is player i's connect time after the run starts: players start evenly over ramp (all at once with ramp 0).

func Summary

func Summary(from, to Sample, hs Counts, reasons map[string]int) string

summary is the whole run between the steady-state window's samples (from: ramp done, to: end) plus the handshakes and disconnect reasons.

func WSURL

func WSURL(raw string) (string, error)

WSURL checks a target URL: ws:// or wss:// with a host; an empty path becomes the game socket path /ws.

Types

type Config

type Config struct {
	URL                           string
	Players, Rooms                int
	InputHz                       int // inputs a player sends per second (pings follow every InputHz inputs: 1 s)
	SnapEvery                     int // server ticks between two snapshots: a larger tick step is a gap
	Duration, Ramp, Settle, Every time.Duration
}

Config is one run: the target, the crowd, the game's cadence and the timing.

type Counts

type Counts [HistMax + 1]uint64

Counts is a point-in-time copy of a Hist's buckets.

func (Counts) Above

func (c Counts) Above(ms int) uint64

Above counts observations of at least ms milliseconds.

func (Counts) Quantile

func (c Counts) Quantile(q float64) int

Quantile is the upper bound in ms of the bucket where the q-th observation falls (bucket i reads i+1); 0 with no observations.

func (Counts) Sub

func (c Counts) Sub(old Counts) Counts

Sub is c minus an older snapshot of the same Hist: the observations made in between.

func (Counts) Total

func (c Counts) Total() uint64

type Hist

type Hist struct {
	// contains filtered or unexported fields
}

Hist is a lock-free latency histogram with 1 ms buckets: bucket i holds observations in [i, i+1) ms, the last one everything >= HistMax ms. Writers Observe from any goroutine; the reporter reads counts.

func (*Hist) Observe

func (h *Hist) Observe(d time.Duration)

func (*Hist) Snapshot

func (h *Hist) Snapshot() Counts

type Sample

type Sample struct {
	At time.Duration
	// contains filtered or unexported fields
}

Sample is a point-in-time copy of the cumulative tally.

type Script

type Script interface {
	Hello(i int) any                            // player i's hello
	Create() any                                // a creator's create message
	Input(i int, seq uint32) any                // player i's input number seq (Config.InputHz)
	React(i, you int, t string, raw []byte) any // a reply to a server message other than snap/pong/error, or nil; raw is valid until return
}

Script is what a game tells the load test.

Concurrency: methods are called from several goroutines at once. For one player, Hello and Create run on the player's goroutine, Input on its send goroutine and React on its read goroutine, so Input and React run concurrently; one player's React calls are serial. Different players run concurrently too. An implementation must guard any state it shares between them. React's replies are best-effort: one dropped while another is still waiting to be sent is not retried.

type Slot

type Slot struct {
	Room    int
	Creator bool
}

Slot is one simulated player's place in the run: its room (-1 = quick play) and whether it creates that room.

func Assign

func Assign(players, rooms int) []Slot

Assign spreads players over rooms round-robin, so a linear ramp fills every room evenly; the first player of each room creates it. rooms == 0 sends every player through quick play.

type Stats

type Stats struct {
	Conns                    atomic.Int64 // players past the handshake, still connected
	MsgsIn, MsgsOut, BytesIn atomic.Uint64
	BytesOut, Snaps, Gaps    atomic.Uint64
	SnapIv, RTT, Handshake   Hist
	// contains filtered or unexported fields
}

Stats is the run's shared tally. Players write it lock-free (atomics and hists); only Disconnect reasons, rare, take the lock.

func NewStats

func NewStats() *Stats

func (*Stats) Disconnect

func (s *Stats) Disconnect(reason string)

func (*Stats) ReasonCounts

func (s *Stats) ReasonCounts() map[string]int

func (*Stats) Sample

func (s *Stats) Sample(at time.Duration) Sample

Jump to

Keyboard shortcuts

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