testtiming

package
v0.18.1 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2026 License: MIT 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 testtiming is the one way a test judges how long something took (iss-2612022218392055). A hosted runner can be several times slower than the machine a bound was written on, and slower again under the race detector, so a test that holds a run to a fixed time fails there on code with nothing wrong: over sixty days of CI, half the failing runs were a wall-clock bound or a fixed budget running out. A test judges time by a Bound instead, in one of two ways:

  • against a reference measured in the same run, on the same machine at the same moment, so a slow runner slows both alike: a cost against the cost of a smaller input, by the ratio of the sizes, or a refusal that must come at once against one pass over its input;
  • against the alternative a run must cut short (CutShort): a call that a context or a kill ends comes back well inside the time the work it ended would have taken, so the test makes that work long and judges the call by a fraction of it, never by a constant just above the context.

A Bound is never under its Floor, so a scheduler or collector pause cannot fail a run whose reference was a few milliseconds. CPU is the clock for a cost: it does not charge the time a loaded machine keeps the process off a core. A test that waits for another goroutine or process to reach a point waits for a sign that it did, never for a fixed sleep: Await asks for the sign for as long as Patience allows, a share of the time the test binary has left rather than a constant. A fake clock is the only clock a fixture built on it reads.

Index

Constants

View Source
const NoDeadlinePatience = 10 * time.Minute

NoDeadlinePatience is Patience for a run given no deadline: long enough for any loaded runner, and a bound on a hang all the same.

Variables

View Source
var CutShort = Bound{Ratio: 0.5}

CutShort judges a run that must cut short an alternative, the work a context, a deadline or a kill ends: it comes back in under half the time the alternative takes. A slow runner adds to the run what it adds to starting and stopping a process, seconds at most, so the alternative is made long enough that half of it is far past that, and the run is still told from one that waited the alternative out.

Functions

func Await

func Await(t *testing.T, poll time.Duration, done func() bool) bool

Await asks done every poll until it reports true, for at most Patience, and reports whether it did. It is the wait for a sign that work elsewhere has reached a point, never a sleep the work is assumed to finish within; the caller fails with what it was waiting for.

func AwaitWithin

func AwaitWithin(limit, poll time.Duration, done func() bool) bool

AwaitWithin is Await for at most limit, for a sign that the work it waits for would also give, later, were it wrong: a process killed with its group is gone, as one that escaped the kill is once its own minute runs out. Such a wait is bounded by the alternative (CutShort.Limit of it), never by Patience, which could outlast it and pass the escape.

func CPU

func CPU() time.Duration

CPU is the CPU time this process has used, user and system, across every thread, the collector's work included. It counts the work a run does and not the time it waits for a core, so a cost read off it on a loaded machine reads as on an idle one (iss-2609240046582859); a slower core still slows it, which is why a cost is judged against a reference rather than a constant. It is the process's clock, not one thread's: Linux reads a thread's time without the slice it is running, so a run shorter than a scheduler tick could read as nothing. getrusage is the call on the macOS and Linux the tests run on.

func Patience

func Patience(t *testing.T) time.Duration

Patience is how long a test waits for something an idle machine does in a second or two, another goroutine's or process's work it has no sign of but what that work leaves behind: three quarters of the time the test binary has left (t.Deadline), so a loaded runner, given the longer deadline a slow run is given, slows the test without failing it, and a wait that runs out still fails the test with its own message before the binary's timeout panic, with a quarter left to report and clean up (iss-2610102020447522). A fixed wait of five seconds ran out on a hosted runner while the work it waited for was still going (iss-2612022218392055). With no deadline (-timeout 0) it is NoDeadlinePatience.

Types

type Bound

type Bound struct {
	// Ratio is how many times the reference the run may take.
	Ratio float64
	// Floor is the least the bound is, whatever the reference.
	Floor time.Duration
}

Bound is the most a measured run may take: Ratio times a reference measured in the same run, and never less than Floor.

func (Bound) Check

func (b Bound) Check(what string, took, reference time.Duration) error

Check is nil when took is within b's limit for reference, and otherwise an error naming what was measured, the reference, the ratio and the floor.

func (Bound) Limit

func (b Bound) Limit(reference time.Duration) time.Duration

Limit is the most a run judged against reference may take.

Jump to

Keyboard shortcuts

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