Skip to content

Commit

Permalink
plot: Golden test for plot output
Browse files Browse the repository at this point in the history
  • Loading branch information
tsenart committed Aug 13, 2018
1 parent df544bc commit 24703ba
Show file tree
Hide file tree
Showing 3 changed files with 1,316 additions and 6 deletions.
17 changes: 11 additions & 6 deletions lib/plot/plot.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,10 @@ func (ls *labeledSeries) add(r *vegeta.Result) (err error) {
ls.began = r.Timestamp // first point in attack
}

// found successor
for {
for len(ls.buf) > 0 {
p, ok := ls.buf[ls.seq]
if !ok {
return nil
break
}
delete(ls.buf, ls.seq)

Expand All @@ -104,6 +103,8 @@ func (ls *labeledSeries) add(r *vegeta.Result) (err error) {

ls.seq++
}

return nil
}

// Opt is a functional option type for Plot.
Expand All @@ -127,11 +128,17 @@ func Label(l Labeler) Opt {
}

// New returns a Plot with the given Opts applied.
// If no Label opt is given, ErrorLabeler will be used as default.
func New(opts ...Opt) *Plot {
p := &Plot{series: map[string]*labeledSeries{}}
for _, opt := range opts {
opt(p)
}

if p.label == nil {
p.label = ErrorLabeler
}

return p
}

Expand All @@ -149,9 +156,7 @@ func (p *Plot) Add(r *vegeta.Result) error {
func (p *Plot) Close() {
for _, as := range p.series {
for _, ts := range as.series {
if ts != nil {
ts.data.Finish()
}
ts.data.Finish()
}
}
}
Expand Down
55 changes: 55 additions & 0 deletions lib/plot/plot_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,70 @@
package plot

import (
"bytes"
"flag"
"io/ioutil"
"math/rand"
"path/filepath"
"testing"
"time"

vegeta "github.com/tsenart/vegeta/lib"
)

var update = flag.Bool("update", false, "Update .golden files")

func TestPlot(t *testing.T) {
p := New(Title("TestPlot"), Downsample(400))

rng := rand.New(rand.NewSource(0))
zf := rand.NewZipf(rng, 3, 2, 1000)
attacks := []string{"500QPS", "1000QPS", "2000QPS"}
began := time.Now()
for i := 0; i < 1e5; i++ {
for _, attack := range attacks {
r := vegeta.Result{
Attack: attack,
Seq: uint64(i),
Timestamp: began.Add(time.Duration(i) * time.Millisecond),
Latency: time.Duration(zf.Uint64()) * time.Millisecond,
}

if err := p.Add(&r); err != nil {
t.Fatal(err)
}
}
}

p.Close()

var b bytes.Buffer
if _, err := p.WriteTo(&b); err != nil {
t.Fatal(err)
}

gp := filepath.Join("testdata", filepath.FromSlash(t.Name())+".golden.html")
if *update {
t.Logf("updating %q", gp)
if err := ioutil.WriteFile(gp, b.Bytes(), 0644); err != nil {
t.Fatalf("failed to update %q: %s", gp, err)
}
}

g, err := ioutil.ReadFile(gp)
if err != nil {
t.Fatalf("failed reading %q: %s", gp, err)
}

if !bytes.Equal(b.Bytes(), g) {
t.Log(string(b.Bytes()))
t.Errorf("bytes do not match %q", gp)
}
}

func TestLabeledSeries(t *testing.T) {
t.Parallel()

s := newLabeledSeries(func(r *vegeta.Result) string {
return r.Attack
})
Expand Down
Loading

0 comments on commit 24703ba

Please sign in to comment.