simulation

package
v0.0.0-...-55d9bb5 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const (
	PAUSED  = false
	RUNNING = true
)

Variables

This section is empty.

Functions

func IPBase64

func IPBase64(ip net.IP) string

Types

type BlockClock

type BlockClock struct {
	Done <-chan struct{}
	// contains filtered or unexported fields
}

BlockClock - this simulation element waits until manually released.

This can be used to pause and resume a simulation.

func (*BlockClock) Pause

func (s *BlockClock) Pause()

func (*BlockClock) Resume

func (s *BlockClock) Resume()

func (*BlockClock) Run

func (s *BlockClock) Run(simulation *Clock, ready chan<- struct{}, end <-chan struct{})

func (*BlockClock) Set

func (s *BlockClock) Set(running bool)

type BoundedNormalIntDistribution

type BoundedNormalIntDistribution struct {
	distuv.Normal
	IntRange
}

Normal distribution of integers with bounds

Integers are generated based on µ and σ, but clipped to the range [min,max].

func (*BoundedNormalIntDistribution) Int

type Clock

type Clock struct {
	Span       SimulationSpan
	WindowSize time.Duration
	Done       <-chan struct{}
	// contains filtered or unexported fields
}

Simulation Clock - used to receive simulation epochs so as to coordinate a complete simulation that spans many elements.

Each epoch defines a given window of simulation time. The simulation element must generate all its events for the given window, and then close the epoch.

Once all simulation elements have closed their epoch the clock will release the next window.

The simulation clock can also choose to tie the simulation clock to wall clock, should the simulation be expected to run at a realistic pace.

Similarly, the simulation clock can choose to only release epochs when other coordinating simulation instances have also concluded the epoch on their side. In this way we will be able to run a distributed simulation if needed.

func (*Clock) Attach

func (c *Clock) Attach() *Session

Attach - used to register an element with the simulation clock.

Each simulation element attaches to the clock and is provided a session via which it coordinates with the clock and other elements. The simulation clock will publish an `Epoch` to each of the elements Attached at the start of the new epoch. Any elements that attach during a given epoch will only be enrolled into the next epoch.

All sessions must complete and close their respective `Epoch` before the simulation clock can proceed with generate an new `Epoch`.

func (*Clock) Logger

func (c *Clock) Logger(l *log.Logger)

Logger sets the logger used by the simulation

func (*Clock) Run

func (c *Clock) Run(ready chan<- struct{}, end <-chan struct{})

type Direction

type Direction bool

Direction designates the flow direction of a unidirectional flow from the point of view of the device processing the packet.

The direction is either egress (an uplink packet leaving the session device), or ingress (a downlink packet entering the session device).

const (
	// Uplink/Egress - packets being emitted from the device.
	EGRESS Direction = true
	// Downlink/Ingress - packets being absorbed by the device.
	INGRESS Direction = false
)

type Epoch

type Epoch struct {

	// Log provides a logger to be used by a simulation element.
	Log *log.Logger

	// Tick is incremented each time a new epoch starts.
	Tick int64

	// The window of time spanning the entire simulation.
	// (Note, it an unbounded simulation is indicated by a zero duration value.)
	SimulationWindow SimulationSpan

	// The window of time spanning this epoch in the simulation.
	// (Note, is all epochs must have a finite non-zero duration value.)
	EpochWindow SimulationSpan
	// contains filtered or unexported fields
}

Simulation Epoch - defines a window in time for which the simulation element must generate simulation events. Once all events are generated the element must close the epoch.

func (*Epoch) Close

func (e *Epoch) Close() error

Close - signal that the element has finished its computation for the epoch.

func (Epoch) String

func (e Epoch) String() string

String - return a user friendly string for the epoch

type FixedRate

type FixedRate struct {
	OpsPerSecond float64
}

FixedRate defines a fixed number of operations per second.

func (*FixedRate) Ops

func (r *FixedRate) Ops() float64

type GenericPool

type GenericPool[T comparable] struct {
	// The total number of available values set for the pool.
	//
	// Note, if zero, then the pool is unlimited.
	Limit int

	// Kind managed by the pool
	Kind string
	// contains filtered or unexported fields
}

Provides access to a pool of values.

Values can be acquired or leased.

func (*GenericPool[T]) AcquireWithGenerator

func (p *GenericPool[T]) AcquireWithGenerator(generator RandomGeneric[T]) (T, error)

Acquire provide a valid entry from the pool.

The pool will return an error if the size limit is reached. The pool will generate a new random value if needed. The pool will never issue the same value twice at the same time. The pool may reissue the same value if it was previously released.

Acquire is thread-safe.

func (*GenericPool[T]) Cap

func (p *GenericPool[T]) Cap() int

func (*GenericPool[T]) Issued

func (p *GenericPool[T]) Issued() int

Issued provides the number of unique values that are currently issued by the pool.

func (*GenericPool[T]) Release

func (p *GenericPool[T]) Release(v T) error

Release previously acquired value back to the pool.

The pool will return an error the value was not previous issued.

Release is thread-safe.

func (*GenericPool[T]) Size

func (p *GenericPool[T]) Size() int

type IP

type IP string

IP is a string representation of net.IP

func (IP) ToNetIP

func (ip IP) ToNetIP() net.IP

type IntDistribution

type IntDistribution interface {
	// Number chosen from the distribution.
	Int() int
}

type IntRange

type IntRange struct {
	Min int
	Max int
}

Range defines the [min, max)

type Pool

type Pool interface {
	Size() int
	Issued() int
	Cap() int
}

type Port

type Port uint16

IP port (TCP or UDP)

type ProbabilityDistribution

type ProbabilityDistribution interface {
	// Probability chosen from the distribution over [0,1].
	Probability() float64
}

type Protocol

type Protocol string
const (
	// User Datagram Protocol
	UDP Protocol = "udp"
	// Transmission Control Protocol
	TCP Protocol = "tcp"
	// Stream Control Transmission Protocol
	SCTP Protocol = "sctp"
	// Datagram Delivery Protocol
	DDP Protocol = "ddp"
	// Internet Control Message Protocol
	ICMP Protocol = "icmp"
)

type RandomGeneric

type RandomGeneric[T comparable] func() T

type Rate

type Rate interface {
	// Number of operations per second chosen from the distribution.
	Ops() float64
}

type Session

type Session struct {
	// Logger for simulation elements
	Log *log.Logger

	// E is the channel on which the clock sends epochs to the session.
	E <-chan Epoch
	// contains filtered or unexported fields
}

Simulation Session - provides a source of simulation epochs, and the mechanisms by which to coordinate with the rest of the simulation.

func (*Session) Close

func (s *Session) Close() error

Close - explicitly end the session and detach from the simulation clock.

type SimulationSpan

type SimulationSpan struct {
	// The simulation time at the start of the window.
	Start time.Time
	// The size of the simulation window (zero if unbounded).
	Duration time.Duration
}

SimulationSpan represents a span of time in the simulation.

Generally this will be interpreted as a half-open range [start, end).

func (*SimulationSpan) End

func (s *SimulationSpan) End() time.Time

End the first point in time after the given span.

type TimeDistribution

type TimeDistribution interface {
	// Duration chosen from the distribution.
	Duration() time.Duration
}

type UniformIntDistribution

type UniformIntDistribution struct {
	IntRange
}

Range defines the [min, max] value.

func (*UniformIntDistribution) Int

func (r *UniformIntDistribution) Int() int

type UniformProbabilityDistribution

type UniformProbabilityDistribution struct {
	Min float64
	Max float64
}

Range defines subset of [0, 1).

func (*UniformProbabilityDistribution) Probability

func (r *UniformProbabilityDistribution) Probability() float64

type UniformRate

type UniformRate struct {
	IntRange
}

Rate defines the [min, max) rate of operations per second.

func (*UniformRate) Ops

func (r *UniformRate) Ops() int

type UniformTimeDistribution

type UniformTimeDistribution struct {
	Min time.Duration
	Max time.Duration
}

Range defines the [min, max) time range.

func (*UniformTimeDistribution) Duration

func (r *UniformTimeDistribution) Duration() time.Duration

type UnitRange

type UnitRange struct {
}

Range defines the [0,1)

func (*UnitRange) Max

func (r *UnitRange) Max() int

func (*UnitRange) Min

func (r *UnitRange) Min() int

type WallClock

type WallClock struct {
	// Values greater than 1.0 will cause the simulation to run faster than wall-clock time if possible.
	// Values less than 1.0 will cause the simulation to run slower than wall-clock time if possible.
	// For convenience, a value of zero 0.0 will set the speed to 1.0.
	Speed float64
}

WallClock - this simulation element always waits for a fraction of wall-clock time.

This can be used to speed up, or slow down your simulation.

Example
// Po(X = λ) = \frac{λ^y·e^{-y}}{y!}
// With the expected value E(y) having a mean and variance of λ i.e. µ = σ² = λ

poisson := distuv.Poisson{
	Lambda: 5.0,
	Src:    rand.NewPCG(1, 1),
}

fmt.Println("Generating 10 random numbers from a Poisson distribution (Lambda = 5.0):")

for i := range 10 {
	// The Rand() method returns a random sample from the distribution.
	sample := poisson.Rand()
	fmt.Printf("Sample #%d: %d\n", i+1, int(sample))
}
Output:

Generating 10 random numbers from a Poisson distribution (Lambda = 5.0):
Sample #1: 4
Sample #2: 4
Sample #3: 7
Sample #4: 1
Sample #5: 7
Sample #6: 7
Sample #7: 7
Sample #8: 3
Sample #9: 2
Sample #10: 1

func (*WallClock) Run

func (s *WallClock) Run(simulation *Clock, ready chan<- struct{}, end <-chan struct{})

Jump to

Keyboard shortcuts

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