bigo

package module
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2019 License: MIT Imports: 11 Imported by: 0

README

Big-O Run & Plot

Library that helps to run Big-O Experiments and plot the output

Go Report Card godoc License

Example comparing two variants

examples/ex1/main.go

package main

import (
	"time"

	"github.com/Oppodelldog/bigo"
)

func main() {
	for testName, testRunner := range map[string]Runner{
		"VariantA": {Sleep: 100},
		"VariantB": {Sleep: 200},
	} {
		bigo.
			New(
				testName,
				testRunner,
				bigo.NewArrayStepper([]float64{1, 2, 3}),
			).
			Run().
			WriteResultsToJson().
			PlotResults()
	}
}

// Runner implements TestRunner
type Runner struct {
	Sleep int
}

// Step simulated to test some logic. For simplicity it simply waits N*r.Sleep milliseconds.
func (r Runner) Step(n float64) bigo.OMeasures {
	timeStart := time.Now()

	// TODO: put your code under test here
	time.Sleep(time.Millisecond * time.Duration(r.Sleep) * time.Duration(n))

	return bigo.OMeasures{{O: float64(time.Since(timeStart).Milliseconds())}}
}
Variant A Variant B

Example extended capturing, N-2d

Let's assume you want to test every N with another subset of test values.
For example N would represent the number of Records in your database.
Now you want to test how your algorithm reacts on different user inputs.
This is why Step returns a list of measures bigo.OMeasures.
This allows to capture multiple Os for every N.
The plot the will reflect that in min, max, mean, all

Here's a sample examples/ex2/main.go

// Step simulated 3 additional scales to the given N. In this case
func (r Runner) Step(n float64) bigo.OMeasures {
   var measures bigo.OMeasures
   for i := 1; i <= 3; i++ {
   	timeStart := time.Now()
   	time.Sleep(time.Millisecond * time.Duration(r.Sleep) * time.Duration(n) * time.Duration(i*r.Factor))
   	measures = append(measures, bigo.OMeasure{O: float64(time.Since(timeStart).Milliseconds())})
   }

   return measures
}

Variant A Variant B

Example combining multiple Results in one plot

To combine mutliple capture results in one plot you have to collect the Results into a bigo.PlotSeriesList, which then can be passed to bigo.PlotTestResults to generate one plot file.

Here's a sample examples/ex3/main.go

func main() {
   seriesList := bigo.PlotSeriesList{}
   for testName, testRunner := range map[string]Runner{
   	"VariantA": {Sleep: 100, Factor: 1},
   	"VariantB": {Sleep: 200, Factor: 2},
   } {
   	seriesList = append(seriesList, bigo.PlotSeries{Name: testName, Results: bigo.
   		New(
   			testName,
   			testRunner,
   			bigo.NewArrayStepper([]float64{1, 2, 3}),
   		).
   		Run().GetResults(),
   	})
   }

   // plot the collected result data and create one plot out of the data
   bigo.PlotTestResults("A/B", seriesList)
}
Combined Plot

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultPlotConfig = PlotConfig{
	ReferencePlots:       false,
	PlotWidth:            6 * vg.Inch,
	PlotHeight:           6 * vg.Inch,
	LegendThumbNailWidth: 0.5 * vg.Inch,
}

DefaultPlotConfig is used when calling PlotTestResults

Functions

func NewArrayStepper

func NewArrayStepper(steps []float64) *arrayStepper

NewRangeStepper returns a Stepper that steps from the beginning to the end of the provided array.

func NewRangeStepper

func NewRangeStepper(min, max, stepSize float64) *rangeStepper

NewRangeStepper returns a Stepper that steps from min to max incremented by stepSize

func PlotTestResults

func PlotTestResults(name string, plotSeries PlotSeriesList)

PlotTestResults plots the given results to a file prefixed with the given name

func PlotTestResultsWithConfig

func PlotTestResultsWithConfig(name string, plotSeries PlotSeriesList, plotConfig PlotConfig)

PlotTestResultsWithConfig allows to plot with custom configuration

func WriteResultsToJsonFile

func WriteResultsToJsonFile(name string, results Results)

WriteResultsToJson writes the captured results to a json file prefixed with the given name.

Types

type BigO

type BigO struct {
	Name      string
	Runner    TestRunner
	Results   Results
	NStepper  Stepper
	RunConfig RunConfig
}

BigO holds values and methods to run tests

func New

func New(name string, testRunner TestRunner, stepper Stepper) *BigO

New *BigO

func NewWithConfig

func NewWithConfig(name string, testRunner TestRunner, stepper Stepper, runConfig RunConfig) *BigO

New *BigO with config

func (*BigO) GetResults added in v0.0.3

func (r *BigO) GetResults() Results

GetResults returns captured results

func (*BigO) PlotResults

func (r *BigO) PlotResults() *BigO

PlotResults plots a graph from the captured results to a png file.

func (*BigO) PlotResultsWithConfig

func (r *BigO) PlotResultsWithConfig(plotConfig PlotConfig) *BigO

PlotResultsWithConfig plots a graph from the captured results to a png file.

func (*BigO) Run

func (r *BigO) Run() *BigO

Run starts a test run, calls the given TestRunner for every N consumed from the given Stepper.

func (*BigO) WriteResultsToJson

func (r *BigO) WriteResultsToJson() *BigO

WriteResultsToJson writes the captured results to a json file.

type OMeasure

type OMeasure struct {
	// ResultValue may be used to store result values of the tested logic.
	// It is not used by this library, it's intention is debugging:
	// If those are deterministic they could be used to cross check that the tested code worked as expected.
	ResultValue interface{} `json:"V"`
	// O represents the number of operations used by the tested program.
	// Determining this value could be from tricky to impossible.
	// For a per machine comparison this could also be the duration of the logic under test.
	O float64 `json:"O"`
}

OMeasure is a single capture of the O value

type OMeasures

type OMeasures []OMeasure

OMeasures contains multiple instances of OMeasure

type PlotConfig

type PlotConfig struct {
	ReferencePlots       bool
	PlotWidth            vg.Length
	PlotHeight           vg.Length
	LegendThumbNailWidth vg.Length
}

PlotConfig enables to configure the plot

type PlotSeries

type PlotSeries struct {
	Name    string
	Results Results
}

PlotSeries a list of result values assigned to a name

type PlotSeriesList added in v0.0.3

type PlotSeriesList []PlotSeries

PlotSeriesList a list PlotSeries

type Result

type Result struct {
	N         float64   `json:"N"`
	OMeasures OMeasures `json:"M"`
}

Result is used by the library to accumulate OMeasure results per N

type Results

type Results []Result

Results contains multiple instances of Result

type RunConfig

type RunConfig struct {
	Speed float64
}

RunConfig gives detailed configuration to BigO

type Stepper

type Stepper interface {
	Next() (float64, bool)
}

Stepper implements the Next method. With every call it returned the next N and true. The boolean return parameter will be set to false when there are more steps to process.

type TestRunner

type TestRunner interface {
	Step(n float64) OMeasures
}

TestRunner runs big o time complexity tests. BigO.Run() calls the Step method for every N and expects OMeasures to be returned.

Directories

Path Synopsis
examples
ex1
ex2
ex3

Jump to

Keyboard shortcuts

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