sample

package module
v0.0.0-...-c42269b Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2015 License: MIT Imports: 5 Imported by: 0

README

Build Status Coverage Status Go Report Card
GoDoc License

Package sample

Go package sample implements sampling algorithms for 1 in n sampling for a random value (probe):

  • Modulo, using modulo-operation
  • PowerOf2, using bitwise AND-operation, only usable if the sampling rate is a power of 2
  • LowerThan, checking if the probe is lower than a pre calculated boundary (maximum value for probe divided by sampling rate)
  • Reciprocal, using a multiplication by the reciprocal value of the sampling rate (Details)
  • Decrement, implementation variant, where the random value is only calculated after a successful sampling

Install

go get github.com/breml/sample

Go generate

To generate the reciprocal_uint files use:

go generate *.go

or

go generate -tags generate .

Documentation

GoDoc

Documentation

Overview

Package sample implements sampling algorithms for 1 in n sampling for a random value (probe):

  • Modulo, using modulo-operation
  • PowerOf2, using bitwise AND-operation, only usable if the sampling rate is a power of 2
  • LowerThan, checking if the probe is lower than a pre calculated boundary (maximum value for probe divided by sampling rate)
  • Reciprocal, using a multiplication by the reciprocal value of the sampling rate (Details: https://breml.github.io/blog/2015/10/22/dividable-without-remainder/)
  • Decrement, implementation variant, where the random value is only calculated after a successful sampling

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Deviation

func Deviation(state State) (deviation float64)

Deviation returns the deviation between the number of time the sampler return true to the mathematically correct count

func Stats

func Stats(state State) string

Stats returns statistical values of the Sampler as String

Types

type Sampler

type Sampler interface {
	// Sample returns a sampling decision based on a random number.
	Sample() bool
	// SampleFrom returns a sampling decision based on probe.
	SampleFrom(probe uint64) bool
	State
}

A Sampler is a source of sampling decisions.

func NewDecrement

func NewDecrement(rate uint64) (Sampler, error)

NewDecrement returns a Decrement Sampler, random number generator is seeded with UnixNano

func NewDecrementSeeded

func NewDecrementSeeded(rate uint64, seed int64) (Sampler, error)

NewDecrementSeeded returns a Decrement Sampler, allow for manual seeding of the random number generator

Example
s, err := NewDecrementSeeded(10, 0)
if err != nil {
	fmt.Println("Unable to initialize sampler", err)
}
for i := 0; i < 100; i++ {
	if s.Sample() {
		fmt.Println(i, "got sampled by Decrement sampler")
	}
}
fmt.Println(Stats(s))
Output:

5 got sampled by Decrement sampler
23 got sampled by Decrement sampler
31 got sampled by Decrement sampler
37 got sampled by Decrement sampler
45 got sampled by Decrement sampler
63 got sampled by Decrement sampler
68 got sampled by Decrement sampler
79 got sampled by Decrement sampler
87 got sampled by Decrement sampler
Rate: 10, SampleCount: 100, TrueCount: 9, Deviation: -11.1111%

func NewLessThan

func NewLessThan(rate uint64) (Sampler, error)

NewLessThan returns a LessThan Sampler, random number generator is seeded with UnixNano

func NewLessThanSeeded

func NewLessThanSeeded(rate uint64, seed int64) (Sampler, error)

NewLessThanSeeded returns a LessThan Sampler, allow for manual seeding of the random number generator

Example
s, err := NewLessThanSeeded(10, 0)
if err != nil {
	fmt.Println("Unable to initialize sampler", err)
}
for i := 0; i < 100; i++ {
	if s.Sample() {
		fmt.Println(i, "got sampled by LessThan sampler")
	}
}
fmt.Println(Stats(s))
Output:

25 got sampled by LessThan sampler
27 got sampled by LessThan sampler
29 got sampled by LessThan sampler
43 got sampled by LessThan sampler
54 got sampled by LessThan sampler
73 got sampled by LessThan sampler
Rate: 10, SampleCount: 100, TrueCount: 6, Deviation: -66.6667%

func NewModulo

func NewModulo(rate uint64) (Sampler, error)

NewModulo returns a Modulo Sampler, random number generator is seeded with UnixNano

func NewModuloSeeded

func NewModuloSeeded(rate uint64, seed int64) (Sampler, error)

NewModuloSeeded returns a Modulo Sampler, allow for manual seeding of the random number generator

Example
s, err := NewModuloSeeded(10, 0)
if err != nil {
	fmt.Println("Unable to initialize sampler", err)
}
for i := 0; i < 100; i++ {
	if s.Sample() {
		fmt.Println(i, "got sampled by Modulo sampler")
	}
}
fmt.Println(Stats(s))
Output:

12 got sampled by Modulo sampler
31 got sampled by Modulo sampler
34 got sampled by Modulo sampler
60 got sampled by Modulo sampler
Rate: 10, SampleCount: 100, TrueCount: 4, Deviation: -150.0000%

func NewPowerOf2

func NewPowerOf2(rate uint64) (Sampler, error)

NewPowerOf2 returns a PowerOf2 Sampler, random number generator is seeded with UnixNano

func NewPowerOf2Seeded

func NewPowerOf2Seeded(rate uint64, seed int64) (Sampler, error)

NewPowerOf2Seeded returns a PowerOf2 Sampler, allow for manual seeding of the random number generator

Example
s, err := NewPowerOf2Seeded(16, 0)
if err != nil {
	fmt.Println("Unable to initialize sampler", err)
}
for i := 0; i < 100; i++ {
	if s.Sample() {
		fmt.Println(i, "got sampled by PowerOf2 sampler")
	}
}
fmt.Println(Stats(s))
Output:

26 got sampled by PowerOf2 sampler
45 got sampled by PowerOf2 sampler
Rate: 16, SampleCount: 100, TrueCount: 2, Deviation: -212.5000%

func NewReciprocalUint16

func NewReciprocalUint16(rate uint64) (Sampler, error)

NewReciprocalUint16 returns a Modulo Sampler, random number generator is seeded with UnixNano

func NewReciprocalUint16Seeded

func NewReciprocalUint16Seeded(rate uint64, seed int64) (Sampler, error)

NewReciprocalUint16Seeded returns a ReciprocalUint16 Sampler, allow for manual seeding of the random number generator

Example
s, err := NewReciprocalUint16Seeded(10, 0)
if err != nil {
	fmt.Println("Unable to initialize sampler", err)
}
for i := 0; i < 100; i++ {
	if s.Sample() {
		fmt.Println(i, "got sampled by ReciprocalUint16 sampler")
	}
}
fmt.Println(Stats(s))
Output:

func NewReciprocalUint32

func NewReciprocalUint32(rate uint64) (Sampler, error)

NewReciprocalUint32 returns a Modulo Sampler, random number generator is seeded with UnixNano

func NewReciprocalUint32Seeded

func NewReciprocalUint32Seeded(rate uint64, seed int64) (Sampler, error)

NewReciprocalUint32Seeded returns a ReciprocalUint32 Sampler, allow for manual seeding of the random number generator

Example
s, err := NewReciprocalUint32Seeded(10, 0)
if err != nil {
	fmt.Println("Unable to initialize sampler", err)
}
for i := 0; i < 100; i++ {
	if s.Sample() {
		fmt.Println(i, "got sampled by ReciprocalUint32 sampler")
	}
}
fmt.Println(Stats(s))
Output:

func NewReciprocalUint64

func NewReciprocalUint64(rate uint64) (Sampler, error)

NewReciprocalUint64 returns a Modulo Sampler, random number generator is seeded with UnixNano

func NewReciprocalUint64Seeded

func NewReciprocalUint64Seeded(rate uint64, seed int64) (Sampler, error)

NewReciprocalUint64Seeded returns a ReciprocalUint64 Sampler, allow for manual seeding of the random number generator

Example
s, err := NewReciprocalUint64Seeded(10, 0)
if err != nil {
	fmt.Println("Unable to initialize sampler", err)
}
for i := 0; i < 100; i++ {
	if s.Sample() {
		fmt.Println(i, "got sampled by ReciprocalUint64 sampler")
	}
}
fmt.Println(Stats(s))
Output:

func NewReciprocalUint8

func NewReciprocalUint8(rate uint64) (Sampler, error)

NewReciprocalUint8 returns a Modulo Sampler, random number generator is seeded with UnixNano

func NewReciprocalUint8Seeded

func NewReciprocalUint8Seeded(rate uint64, seed int64) (Sampler, error)

NewReciprocalUint8Seeded returns a ReciprocalUint8 Sampler, allow for manual seeding of the random number generator

Example
s, err := NewReciprocalUint8Seeded(10, 0)
if err != nil {
	fmt.Println("Unable to initialize sampler", err)
}
for i := 0; i < 100; i++ {
	if s.Sample() {
		fmt.Println(i, "got sampled by ReciprocalUint8 sampler")
	}
}
fmt.Println(Stats(s))
Output:

type State

type State interface {
	// Reset the internal state of the sampler to the initial values
	// This also resets the random number generator to the initial state with the initial seed value
	Reset()
	// String returns the internal state of the sampler as string
	String() string
	// Returns the rate
	Rate() uint64
	// Calls returns how many times the sampler was asked to sample
	Calls() uint64
	// Count returns how many times the sampler sampled (returned true)
	Count() uint64
}

A State is the internal State of a Sampler.

Jump to

Keyboard shortcuts

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