timing

package
v0.15.2 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2026 License: Apache-2.0 Imports: 7 Imported by: 1 module, 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 timing records where one request's time went (agni issue 914): its stages, which cache layer answered each read, how long each rule took, and the plan of each query evaluated.

A Recorder travels in the request's context. Every recording call here is a no-op when the context holds none, so a request nobody is timing costs one context lookup per call and nothing else. The package imports only the standard library, so the rule loop in core/check can record into it.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Absent added in v0.12.0

func Absent(ctx context.Context, query, why string)

Absent records why one query evaluation answered no rows, as jaala's Absence renders it.

func AskingWhyNot added in v0.12.0

func AskingWhyNot(ctx context.Context) bool

AskingWhyNot reports whether the request asked why an empty answer is empty.

func Begin

func Begin(ctx context.Context, name string) func()

Begin starts a stage and returns the function that ends it, for `defer timing.Begin(ctx, "x")()`.

func BeginRule

func BeginRule(ctx context.Context) func(name string)

BeginRule starts timing one rule and returns the function that records it under name, with the bytes it allocated when the Recorder asked for Memory (agni issue 994). It is Ruled for a caller that wants allocation too.

func Cached

func Cached(ctx context.Context, layer, source string)

Cached records which tier answered a read of layer.

func Explaining

func Explaining(ctx context.Context) bool

Explaining reports whether the request asked for query plans.

func Planned

func Planned(ctx context.Context, query, plan string)

Planned records the plan that one query evaluation ran.

func Ruled

func Ruled(ctx context.Context, rule string, d time.Duration)

Ruled records that rule took d to evaluate.

func With

func With(ctx context.Context, r *Recorder) context.Context

With returns a context that records into r.

Types

type Lookup

type Lookup struct {
	Layer  string
	Source string
}

Lookup is which tier answered one cached read: "memory" (kept in this process), "store" (the persistent tier, such as the browser's), "built" (read and built now) or "uncached" (no cache).

type Plan

type Plan struct {
	Query string
	Text  string
}

Plan is one query evaluation's plan, as jaala's Explain renders it.

type Recorder

type Recorder struct {
	// Explain asks query evaluations to record their plans (jaala's Explain), which costs time in
	// proportion to the rules and literals a query runs.
	Explain bool
	// WhyNot asks a query evaluation that answers no rows to record why (jaala's WhyNot, agni issue
	// 1965). It costs one more evaluation per literal, and only on an empty answer.
	WhyNot bool
	// Memory asks each stage to record the process's memory as it ends (agni issue 994). Reading it
	// stops the world briefly, so only a request that asks for it is slowed. In wasm, memory only
	// grows. So when a stage's end first shows a higher Sys, that stage raised the peak.
	Memory bool
	// contains filtered or unexported fields
}

Recorder collects one request's timings. Its methods are safe for concurrent use, since a request may read its tiers in parallel.

func From

func From(ctx context.Context) *Recorder

From returns the context's Recorder, nil when the request is not being timed.

func New

func New() *Recorder

New returns a Recorder whose clock starts now.

func (*Recorder) Snapshot

func (r *Recorder) Snapshot() Snapshot

Snapshot returns what r has recorded, with the request's total taken now.

type RuleTime

type RuleTime struct {
	Name  string
	Dur   time.Duration
	Alloc uint64
}

RuleTime is how long one rule's evaluation took, and, when the Recorder asked for Memory, how many bytes it allocated, collected or not. Allocation is read process-wide, so a rule run while another request allocates is charged for both.

type Snapshot

type Snapshot struct {
	Total   time.Duration
	Stages  []Stage    // by start
	Lookups []Lookup   // in the order the reads happened
	Rules   []RuleTime // slowest first
	Plans   []Plan     // in the order the queries ran
	// Absent holds why each empty answer is empty, in the order the queries ran. Text is jaala's
	// Absence as text.
	Absent []Plan
}

Snapshot is a Recorder's contents at one moment, in the order a reader wants them.

func (Snapshot) Slowest

func (s Snapshot) Slowest() (Stage, bool)

Slowest returns the stage taking the most time, so a log line can name what dominated. A stage that only wraps others still counts, so the answer is the widest span, and a reader goes from it to the stages inside it.

func (Snapshot) Text

func (s Snapshot) Text(topRules int) string

Text renders the snapshot for a terminal: the total, each stage, each cache lookup, the slowest rules (at most topRules, all of them when topRules is 0), and each plan.

type Stage

type Stage struct {
	Name string
	// Start is the span's offset from the start of the request.
	Start time.Duration
	Dur   time.Duration
	// Sys and HeapAlloc are runtime.MemStats' figures as the stage ended, zero unless the Recorder
	// asked for Memory. Sys counts what the runtime has obtained, and in wasm that is the memory's
	// size. HeapAlloc includes garbage not yet collected, since no collection is forced.
	Sys       uint64
	HeapAlloc uint64
}

Stage is one named span of a request. Stages nest, so a parent's duration includes its children's.

Jump to

Keyboard shortcuts

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