sim

package
v0.22.0 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2023 License: Apache-2.0 Imports: 34 Imported by: 0

Documentation

Overview

Package sim...

TODO(mwhittaker): Write comprehensive package documentation with examples. We also probably want to put some of this documentation on the website, and we might also want to write a blog.

TODO(mwhittaker): Move things to the weavertest package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Event

type Event interface {
	// contains filtered or unexported methods
}

An Event represents an atomic step of a execution.

type EventCall added in v0.22.0

type EventCall struct {
	TraceID   int      // trace id
	SpanID    int      // span id
	Caller    string   // calling component (or "op")
	Replica   int      // calling component replica (or op number)
	Component string   // component being called
	Method    string   // method being called
	Args      []string // method arguments
}

EventCall represents a component method call.

type EventDeliverCall added in v0.22.0

type EventDeliverCall struct {
	TraceID   int    // trace id
	SpanID    int    // span id
	Component string // component being called
	Replica   int    // component replica being called
}

EventDeliverCall represents a component method call being delivered.

type EventDeliverError added in v0.22.0

type EventDeliverError struct {
	TraceID int // trace id
	SpanID  int // span id
}

EventDeliverError represents the injection of an error.

type EventDeliverReturn added in v0.22.0

type EventDeliverReturn struct {
	TraceID int // trace id
	SpanID  int // span id
}

EventDeliverReturn represents the delivery of a method return.

type EventOpFinish added in v0.22.0

type EventOpFinish struct {
	TraceID int    // trace id
	SpanID  int    // span id
	Error   string // returned error message
}

EventOpFinish represents the finish of an op.

type EventOpStart added in v0.22.0

type EventOpStart struct {
	TraceID int      // trace id
	SpanID  int      // span id
	Name    string   // op name
	Args    []string // op arguments
}

EventOpStart represents the start of an op.

type EventPanic added in v0.22.0

type EventPanic struct {
	TraceID  int    // trace id
	SpanID   int    // span id
	Panicker string // panicking component (or "op")
	Replica  int    // panicking component replica (or op number)
	Error    string // panic error
	Stack    string // stack trace
}

EventPanic represents a panic.

type EventReturn added in v0.22.0

type EventReturn struct {
	TraceID   int      // trace id
	SpanID    int      // span id
	Component string   // component returning
	Replica   int      // component replica returning
	Returns   []string // return values
}

EventReturn represents a component method call returning.

type FakeComponent

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

FakeComponent is a copy of weavertest.FakeComponent. It's needed to access the unexported fields.

TODO(mwhittaker): Remove this once we merge with weavertest.

func Fake

func Fake[T any](impl any) FakeComponent

Fake is a copy of weavertest.Fake.

TODO(mwhittaker): Remove this once we merge with the weavertest package.

type Generator

type Generator[T any] interface {
	// Generate returns a randomly generated value of type T. While Generate is
	// "random", it must be deterministic. That is, given the same instance of
	// *rand.Rand, Generate must always return the same value.
	//
	// TODO(mwhittaker): Generate should maybe take something other than a
	// *rand.Rand?
	Generate(*rand.Rand) T
}

A Generator[T] generates random values of type T.

func Byte added in v0.22.0

func Byte() Generator[byte]

Byte returns a Generator that returns bytes equiprobably.

func Filter added in v0.22.0

func Filter[T any](gen Generator[T], predicate func(T) bool) Generator[T]

Filter returns a Generator that returns values from the provided generator that satisfy the provided predicate.

func Flip added in v0.22.0

func Flip(p float64) Generator[bool]

Flip returns a Generator that returns true with probability p. Flip panics if p is not in the range [0, 1].

func Float64 added in v0.22.0

func Float64() Generator[float64]

Float64 returns a Generator that returns 64-bit floats. Note that Float64 does not return all floats equiprobably. Instead, it biases towards numbers closer to zero and other pathological numbers that are more likely to induce bugs (e.g., NaN, infinity, -infinity, -0).

func Int added in v0.22.0

func Int() Generator[int]

Int returns a Generator that returns integers. Note that Int does not return all integers equiprobably. Instead, it biases towards numbers closer to zero and other pathological numbers that are more likely to induce bugs (e.g., math.MaxInt, math.MinInt).

func Map added in v0.22.0

func Map[K comparable, V any](size Generator[int], keys Generator[K], values Generator[V]) Generator[map[K]V]

Map returns a Generator that returns maps from K to V. The size and contents of the the generated maps are determined by the provided generators.

func NonNegativeInt added in v0.22.0

func NonNegativeInt() Generator[int]

NonNegativeInt returns a Generator that returns non-negative integers. Note that NonNegativeInt does not return all numbers. Instead, it biases towards numbers closer to zero and other pathological numbers that are more likely to induce bugs (e.g., math.MaxInt).

func OneOf added in v0.22.0

func OneOf[T any](xs ...T) Generator[T]

OneOf returns a Generator that returns one of the provided values equiprobably. OneOf panics if no values are provided.

func Range added in v0.22.0

func Range(low, high int) Generator[int]

Range returns a Generator that returns integers equiprobably in the range [low, high). Range panics if low >= high.

func Rune added in v0.22.0

func Rune() Generator[rune]

Rune returns a Generator that returns runes equiprobably.

func Slice added in v0.22.0

func Slice[T any](size Generator[int], values Generator[T]) Generator[[]T]

Slice returns a Generator that returns slices of T. The size and contents of the generated slices are determined by the provided generators.

func String added in v0.22.0

func String() Generator[string]

String returns a Generator that returns moderately sized readable strings, with a bias towards smaller strings.

func Weight added in v0.22.0

func Weight[T any](choices []Weighted[T]) Generator[T]

Weight returns a Generator that generates values using the provided generators. A generator is chosen with probability proportional to its weight. For example, given the following choices:

  • Weighted{1.0, OneOf("a")}
  • Weighted{2.0, OneOf("b")}

Weight returns "b" twice as often as it returns "a". Note that the provided weights do not have to sum to 1.

Weight panics if no choices are provided, if any weight is negative, or if the sum of all weight is 0.

type Options

type Options struct {
	// TOML config file contents.
	Config string

	// The number of executions to run in parallel. If Parallelism is 0, the
	// simulator picks the degree of parallelism.
	Parallelism int
}

Options configure a Simulator.

type Registrar

type Registrar interface {
	// RegisterFake registers a fake implementation of a component.
	RegisterFake(FakeComponent)

	// RegisterGenerators registers generators for a workload method, one
	// generator per method argument. The number and type of the registered
	// generators must match the method. For example, given the method:
	//
	//     Foo(context.Context, int, bool) error
	//
	// we must register a Generator[int] and a Generator[bool]:
	//
	//     var r Registrar = ...
	//     var i Generator[int] = ...
	//     var b Generator[bool] = ...
	//     r.RegisterGenerators("Foo", i, b)
	//
	// TODO(mwhittaker): Allow people to register a func(*rand.Rand) T instead
	// of a Generator[T] for convenience.
	RegisterGenerators(method string, generators ...any)
}

A Registrar is used to register fakes and generators with a Simulator.

type Results

type Results struct {
	Err           error         // first non-nil error returned by an op
	History       []Event       // a history of the error inducing run, if Err is not nil
	NumExecutions int           // number of executions ran
	NumOps        int           // number of ops ran
	Duration      time.Duration // duration of simulation
}

Results are the results of simulating a workload.

func (*Results) Mermaid

func (r *Results) Mermaid() string

Mermaid returns a mermaid diagram that illustrates an execution history.

type Simulator

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

A Simulator deterministically simulates a Service Weaver application. See the package documentation for instructions on how to use a Simulator.

func New

func New(t testing.TB, x Workload, opts Options) *Simulator

New returns a new Simulator that simulates the provided workload.

func (*Simulator) Run

func (s *Simulator) Run(duration time.Duration) Results

Run runs a simulation for the provided duration.

type Weighted added in v0.22.0

type Weighted[T any] struct {
	Weight float64
	Gen    Generator[T]
}

type Workload

type Workload interface {
	// Init initializes a workload. The Init method must also register
	// generators for every exported method.
	Init(Registrar) error
}

A Workload defines the set of operations to run as part of a simulation. Every workload is defined as a named struct. To execute a workload, a simulator constructs an instance of the struct, calls the struct's Init method, and then randomly calls the struct's exported methods. For example, the following is a simple workload:

type myWorkload struct {}
func (w *myWorkload) Init(r sim.Registrar) {...}
func (w *myWorkload) Foo(context.Context, int) error {...}
func (w *myWorkload) Bar(context.Context, bool, string) error {...}
func (w *myWorkload) baz(context.Context) error {...}

When this workload is executed, its Foo and Bar methods will be called with random values generated by the generators registered in the Init method (see Registrar for details). Note that unexported methods, like baz, are ignored.

Note that every exported workload method must receive a context.Context as its first argument and must return a single error value. A simulation is aborted when a method returns a non-nil error.

TODO(mwhittaker): For now, the Init method is required. In the future, we could make it optional and use default generators for methods.

Jump to

Keyboard shortcuts

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