testutil

package
v0.1.21 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: Apache-2.0 Imports: 15 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 testutil provides utilities for testing a compute.Backend. It is used by the gobackend package to implement the "go" backend, but shared here as it could be used by other backends as well.

It includes helpers for:

  • Comparison: IsEqual, IsInDelta and IsInRelativeDelta, implemented for various types, including the half-precision float16 and bfloat16.
  • Buffer manipulation: ToBuffer and FromBuffer are not efficient, but convenient conversion of values to Go for testing.

Index

Constants

View Source
const DefaultReservoirSize = 16 * 1024

DefaultReservoirSize is the default capacity for the duration sampler (16K samples).

Variables

This section is empty.

Functions

func Exec

func Exec(backend compute.Backend, inputs []any,
	buildFn func(f compute.Function, params []compute.Value) ([]compute.Value, error),
) ([]any, error)

Exec builds, compiles, and executes graph with multiple inputs and outputs.

Each of the inputs are converted using ToBuffer, and the results are converted back to Go using FromBuffer.

func Exec1

func Exec1(backend compute.Backend, inputs []any,
	buildFn func(f compute.Function, params []compute.Value) (compute.Value, error),
) (any, error)

Exec1 is like Exec, but it has only 1 output.

func Exec2

func Exec2(backend compute.Backend, inputs []any,
	buildFn func(f compute.Function, params []compute.Value) (compute.Value, compute.Value, error),
) (any, any, error)

Exec2 is like Exec, but it has only 2 outputs.

func Exec3

func Exec3(backend compute.Backend, inputs []any,
	buildFn func(f compute.Function, params []compute.Value) (compute.Value, compute.Value, compute.Value, error),
) (any, any, any, error)

Exec3 is like Exec, but it has only 3 outputs.

func FlattenSlice

func FlattenSlice(v any) any

FlattenSlice is a helper to convert a nested slice of any into a flat slice.

func FromBuffer

func FromBuffer(backend compute.Backend, buf compute.Buffer) (any, error)

FromBuffer converts a compute.Buffer into a nested slice of the corresponding Go type.

func IsEqual

func IsEqual(want, got any) (ok bool, diff string)

IsEqual returns whether want and got are equal using go-cmp. If they are not equal, it returns the diff using the format "-want +got").

func IsInDelta

func IsInDelta(want, got any, delta float64) (ok bool, diff string)

IsInDelta reports if want and got are equal within a given absolute delta. If they are not equal, it returns the diff using the format "-want +got").

func IsInRelativeDelta

func IsInRelativeDelta(want, got any, relDelta float64) (ok bool, diff string)

IsInRelativeDelta reports if want and got are equal within a given relative delta. If they are not equal, it returns the diff using the format "-want +got").

func Must

func Must(err error)

Must panics if there is an error.

func Must1

func Must1[T any](value T, err error) T

Must1 panics if there is an error and returns the value.

func SkipIfMissing

func SkipIfMissing(t *testing.T, b compute.Backend, op compute.OpType)

SkipIfMissing skips the current test if the backend doesn't support the given operation.

func SkipIfMissingDType

func SkipIfMissingDType(t *testing.T, b compute.Backend, dtype dtypes.DType)

SkipIfMissingDType skips the current test if the backend doesn't support the given data type.

func SkipIfMissingDynamicShapes added in v0.1.5

func SkipIfMissingDynamicShapes(t *testing.T, b compute.Backend)

SkipIfMissingDynamicShapes skips the current test if the backend doesn't support dynamic shapes.

func SkipIfMissingFunctions

func SkipIfMissingFunctions(t *testing.T, b compute.Backend)

SkipIfMissingFunctions skips the current test if the backend doesn't support functions (closures).

func ToBuffer

func ToBuffer(backend compute.Backend, v any) (compute.Buffer, error)

ToBuffer converts a nested slice of any into a buffer for the given backend.

func ToFlatAndShape

func ToFlatAndShape(v any) (any, shapes.Shape)

ToFlatAndShape converts a nested slice of any into a flat slice and a shape that can be fed into testBackend (or a BufferFromFlatData).

func Try

func Try(f func()) (panicked bool, reason any)

Try whether f panics, and also returns the panic value.

Types

type DurationSampler added in v0.1.10

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

DurationSampler collects time.Duration observations using Algorithm R (Reservoir Sampling) with a fixed capacity to compute percentiles (like median) in constant space and O(1) sample addition.

func NewDurationSampler added in v0.1.10

func NewDurationSampler(capacity int) *DurationSampler

NewDurationSampler creates a sampler with the given maximum reservoir capacity. If capacity <= 0, DefaultReservoirSize (16384) is used.

func (*DurationSampler) Count added in v0.1.10

func (s *DurationSampler) Count() int

Count returns the total number of samples observed.

func (*DurationSampler) Max added in v0.1.10

func (s *DurationSampler) Max() time.Duration

Max returns the maximum duration recorded, or 0 if empty.

func (*DurationSampler) Mean added in v0.1.10

func (s *DurationSampler) Mean() time.Duration

Mean returns the mean duration of the retained samples, or 0 if empty.

func (*DurationSampler) Median added in v0.1.10

func (s *DurationSampler) Median() time.Duration

Median returns the median duration of the collected samples. If no samples were recorded, it returns 0.

func (*DurationSampler) Min added in v0.1.10

func (s *DurationSampler) Min() time.Duration

Min returns the minimum duration recorded, or 0 if empty.

func (*DurationSampler) Percentile added in v0.1.10

func (s *DurationSampler) Percentile(p float64) time.Duration

Percentile returns the p-th percentile duration (0.0 <= p <= 1.0). If no samples were recorded, it returns 0.

func (*DurationSampler) ReservoirCount added in v0.1.10

func (s *DurationSampler) ReservoirCount() int

ReservoirCount returns the number of samples currently retained in the reservoir.

func (*DurationSampler) Reset added in v0.1.10

func (s *DurationSampler) Reset()

Reset clears the samples and count, retaining the allocated reservoir buffer.

func (*DurationSampler) Sample added in v0.1.10

func (s *DurationSampler) Sample(d time.Duration)

Sample adds a duration observation. The first capacity samples are stored sequentially. Subsequent samples are retained with probability (capacity / count), replacing a randomly chosen sample in the reservoir.

Jump to

Keyboard shortcuts

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