forked from tsenart/vegeta
/
timeseries.go
56 lines (47 loc) · 1.06 KB
/
timeseries.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package plot
import (
"errors"
"time"
"github.com/NoneTirex/vegeta/v12/lib/lttb"
tsz "github.com/tsenart/go-tsz"
)
// 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
}
func newTimeSeries(attack, label string) *timeSeries {
return &timeSeries{
attack: attack,
label: label,
data: tsz.New(0),
}
}
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 {
it := ts.data.Iter()
return func(count int) ([]lttb.Point, error) {
ps := make([]lttb.Point, 0, count)
for i := 0; i < count && it.Next(); i++ {
t, v := it.Values()
ps = append(ps, lttb.Point{
X: time.Duration(t * 1e6).Seconds(),
Y: v,
})
}
return ps, it.Err()
}
}