Skip to content

Commit

Permalink
plot: Ensure timestamp monotonicity
Browse files Browse the repository at this point in the history
  • Loading branch information
tsenart committed Aug 12, 2018
1 parent f3fb801 commit eacb7a8
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
20 changes: 13 additions & 7 deletions lib/plot.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package vegeta

import (
"encoding/json"
"fmt"
"html/template"
"io"
"math"
Expand Down Expand Up @@ -47,7 +48,7 @@ func newLabeledSeries(label func(*Result) string) *labeledSeries {
}
}

func (ls *labeledSeries) add(r *Result) {
func (ls *labeledSeries) add(r *Result) (err error) {
label := ls.label(r)

ts, ok := ls.series[label]
Expand All @@ -64,7 +65,7 @@ func (ls *labeledSeries) add(r *Result) {
}

if ls.buf[p.seq] = p; p.seq != ls.seq {
return // buffer
return nil // buffer
} else if ls.seq == 0 {
ls.began = r.Timestamp // first point in attack
}
Expand All @@ -73,11 +74,16 @@ func (ls *labeledSeries) add(r *Result) {
for {
p, ok := ls.buf[ls.seq]
if !ok {
return
return nil
}

delete(ls.buf, ls.seq)
p.ts.add(p.seq, uint64(p.t.Sub(ls.began))/1e6, p.v) // timestamp in ms precision

// timestamp in ms precision
err = p.ts.add(uint64(p.t.Sub(ls.began))/1e6, p.v)
if err != nil {
return fmt.Errorf("point with sequence number %d in %v", p.seq, err)
}

ls.seq++
}
}
Expand All @@ -94,13 +100,13 @@ func NewHTMLPlot(title string, threshold int, label func(*Result) string) *HTMLP
}

// Add adds the given Result to the HTMLPlot time series.
func (p *HTMLPlot) Add(r *Result) {
func (p *HTMLPlot) Add(r *Result) error {
s, ok := p.series[r.Attack]
if !ok {
s = newLabeledSeries(p.label)
p.series[r.Attack] = s
}
s.add(r)
return s.add(r)
}

// Close closes the HTML plot for writing.
Expand Down
15 changes: 14 additions & 1 deletion lib/timeseries.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package vegeta

import (
"errors"
"time"

tsz "github.com/dgryski/go-tsz"
"github.com/tsenart/vegeta/lib/lttb"
)

// An in-memory timeSeries of points with high compression of
// both timestamps and values. It's not safe for concurrent use.
type timeSeries struct {
attack string
label string
prev uint64
data *tsz.Series
len int
}
Expand All @@ -22,9 +26,18 @@ func newTimeSeries(attack, label string) *timeSeries {
}
}

func (ts *timeSeries) add(seq, t uint64, v float64) {
var errMonotonicTimestamp = errors.New("timeseries: non monotonically increasing timestamp.")

func (ts *timeSeries) add(t uint64, v float64) error {
if ts.prev > t {
return errMonotonicTimestamp
}

ts.data.Push(t, v)
ts.prev = t
ts.len++

return nil
}

func (ts *timeSeries) iter() lttb.Iter {
Expand Down
5 changes: 4 additions & 1 deletion plot.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ decode:
}
return err
}
plot.Add(&r)

if err = plot.Add(&r); err != nil {
return err
}
}
}

Expand Down

0 comments on commit eacb7a8

Please sign in to comment.