-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from rogerwelin/histogram
histogram generation
- Loading branch information
Showing
8 changed files
with
198 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package client | ||
|
||
import ( | ||
"image/color" | ||
"math" | ||
|
||
"gonum.org/v1/plot" | ||
"gonum.org/v1/plot/plotter" | ||
) | ||
|
||
func getBins(dataPoints int) int { | ||
b := int(math.RoundToEven(math.Sqrt(float64(dataPoints)))) | ||
return b | ||
} | ||
|
||
// PlotHistogram blabla | ||
func (c *Cassowary) PlotHistogram(durations []float64) error { | ||
|
||
bins := getBins(len(durations)) | ||
if bins == 0 { | ||
return nil | ||
} | ||
|
||
// remove outliers | ||
avg := calcMean(durations) | ||
stddev := calcStdDev(durations) | ||
outliers := avg + 1.95*stddev | ||
|
||
for i := len(durations) - 1; i >= 0; i-- { | ||
if durations[i] > outliers { | ||
durations = append(durations[:i], durations[i+1:]...) | ||
} | ||
} | ||
|
||
vs := make(plotter.Values, len(durations)) | ||
for i, d := range durations { | ||
vs[i] = d | ||
} | ||
|
||
h, err := plotter.NewHist(vs, bins) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
p, err := plot.New() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
p.Add(h) | ||
p.Title.Text = "Distribution" | ||
p.Y.Label.Text = "Requests" | ||
p.X.Label.Text = "ms" | ||
h.FillColor = color.RGBA{R: 70, G: 130, B: 180, A: 1} | ||
h.Color = color.Opaque | ||
|
||
if err := p.Save(512, 512, "hist.png"); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package client | ||
|
||
import ( | ||
"math/rand" | ||
"os" | ||
"testing" | ||
"time" | ||
) | ||
|
||
var testBins = []struct { | ||
in int | ||
expectedBins int | ||
}{ | ||
{10, 3}, | ||
{100, 10}, | ||
{250, 16}, | ||
{500, 22}, | ||
{750, 27}, | ||
{1000, 32}, | ||
} | ||
|
||
func TestBins(t *testing.T) { | ||
for i, tt := range testBins { | ||
actual := getBins(tt.in) | ||
if actual != tt.expectedBins { | ||
t.Errorf("test: %d, getBins(%d): expected %d, actual %d", i+1, tt.in, tt.expectedBins, actual) | ||
} | ||
} | ||
} | ||
|
||
func TestHistOutput(t *testing.T) { | ||
num := 100 | ||
res := make([]float64, num) | ||
filename := "hist.png" | ||
cass := &Cassowary{} | ||
rand.Seed(time.Now().UnixNano()) | ||
|
||
for i := 0; i < num; i++ { | ||
res = append(res, rand.Float64()*(200.0-10.0)) | ||
} | ||
|
||
err := cass.PlotHistogram(res) | ||
if err != nil { | ||
t.Errorf("Expected ok but got: %v", err) | ||
} | ||
_, err = os.Stat(filename) | ||
if os.IsNotExist(err) { | ||
t.Errorf("Expected %s in current dir but got: %v", filename, err) | ||
} | ||
|
||
_ = os.Remove(filename) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters