1package plot 2 3import ( 4 "errors" 5 "time" 6 7 tsz "github.com/tsenart/go-tsz" 8 "github.com/tsenart/vegeta/v12/lib/lttb" 9) 10 11// An in-memory timeSeries of points with high compression of 12// both timestamps and values. It's not safe for concurrent use. 13type timeSeries struct { 14 attack string 15 label string 16 prev uint64 17 data *tsz.Series 18 len int 19} 20 21func newTimeSeries(attack, label string) *timeSeries { 22 return &timeSeries{ 23 attack: attack, 24 label: label, 25 data: tsz.New(0), 26 } 27} 28 29var errMonotonicTimestamp = errors.New("timeseries: non monotonically increasing timestamp") 30 31func (ts *timeSeries) add(t uint64, v float64) error { 32 if ts.prev > t { 33 return errMonotonicTimestamp 34 } 35 36 ts.data.Push(t, v) 37 ts.prev = t 38 ts.len++ 39 40 return nil 41} 42 43func (ts *timeSeries) iter() lttb.Iter { 44 it := ts.data.Iter() 45 return func(count int) ([]lttb.Point, error) { 46 ps := make([]lttb.Point, 0, count) 47 for i := 0; i < count && it.Next(); i++ { 48 t, v := it.Values() 49 ps = append(ps, lttb.Point{ 50 X: time.Duration(t * 1e6).Seconds(), 51 Y: v, 52 }) 53 } 54 return ps, it.Err() 55 } 56} 57