Skip to content

Latest commit

 

History

History
120 lines (97 loc) · 3.44 KB

README.md

File metadata and controls

120 lines (97 loc) · 3.44 KB

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