-
Notifications
You must be signed in to change notification settings - Fork 6
/
sparse.go
217 lines (176 loc) · 4.19 KB
/
sparse.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
package quantile
import (
"math"
"strings"
"unsafe"
"github.com/DataDog/opentelemetry-mapping-go/pkg/quantile/summary"
)
var _ memSized = (*Sketch)(nil)
// A Sketch for tracking quantiles
// The serialized JSON of Sketch contains the summary only
// Bins are not included.
type Sketch struct {
sparseStore
Basic summary.Summary `json:"summary"`
}
func (s *Sketch) String() string {
var b strings.Builder
printSketch(&b, s, Default())
return b.String()
}
// MemSize returns memory use in bytes:
//
// used: uses len(bins)
// allocated: uses cap(bins)
func (s *Sketch) MemSize() (used, allocated int) {
const (
basicSize = int(unsafe.Sizeof(summary.Summary{}))
)
used, allocated = s.sparseStore.MemSize()
used += basicSize
allocated += basicSize
return
}
// InsertMany values into the sketch.
func (s *Sketch) InsertMany(c *Config, values []float64) {
keys := getKeyList()
for _, v := range values {
s.Basic.Insert(v)
keys = append(keys, c.key(v))
}
s.insert(c, keys)
putKeyList(keys)
}
// Reset sketch to its empty state.
func (s *Sketch) Reset() {
s.Basic.Reset()
s.count = 0
s.bins = s.bins[:0] // TODO: just release to a size tiered pool.
}
// GetRawBins return raw bins information as string
func (s *Sketch) GetRawBins() (int, string) {
return s.count, strings.Replace(s.bins.String(), "\n", "", -1)
}
// Insert a single value into the sketch.
// NOTE: InsertMany is much more efficient.
func (s *Sketch) Insert(c *Config, vals ...float64) {
// TODO: remove this
s.InsertMany(c, vals)
}
// Merge o into s, without mutating o.
func (s *Sketch) Merge(c *Config, o *Sketch) {
s.Basic.Merge(o.Basic)
s.merge(c, &o.sparseStore)
}
// Quantile returns v such that s.count*q items are <= v.
//
// Special cases are:
//
// Quantile(c, q <= 0) = min
// Quantile(c, q >= 1) = max
func (s *Sketch) Quantile(c *Config, q float64) float64 {
switch {
case s.count == 0:
return 0
case q <= 0:
return s.Basic.Min
case q >= 1:
return s.Basic.Max
}
var (
n float64
rWant = rank(s.count, q)
)
for i, b := range s.bins {
n += float64(b.n)
if n <= rWant {
continue
}
weight := (n - rWant) / float64(b.n)
vLow := c.f64(b.k)
vHigh := vLow * c.gamma.v
switch i {
case s.bins.Len():
vHigh = s.Basic.Max
case 0:
vLow = s.Basic.Min
}
// TODO|PROD: Interpolate between bucket boundaries, correctly handling min, max,
// negative numbers.
// with a gamma of 1.02, interpolating to the center gives us a 1% abs
// error bound.
return (vLow*weight + vHigh*(1-weight))
// return vLow
}
// this can happen if count is greater than sum of bins
return s.Basic.Max
}
func rank(count int, q float64) float64 {
return math.RoundToEven(q * float64(count-1))
}
// CopyTo makes a deep copy of this sketch into dst.
func (s *Sketch) CopyTo(dst *Sketch) {
// TODO: pool slices here?
dst.bins = dst.bins.ensureLen(s.bins.Len())
copy(dst.bins, s.bins)
dst.count = s.count
dst.Basic = s.Basic
}
// Copy returns a deep copy
func (s *Sketch) Copy() *Sketch {
dst := &Sketch{}
s.CopyTo(dst)
return dst
}
// Equals returns true if s and o are equivalent.
func (s *Sketch) Equals(o *Sketch) bool {
if s.Basic != o.Basic {
return false
}
if s.count != o.count {
return false
}
if len(s.bins) != len(o.bins) {
return false
}
for i := range s.bins {
if o.bins[i] != s.bins[i] {
return false
}
}
return true
}
// ApproxEquals checks if s and o are equivalent, with e error allowed for Sum and Average
func (s *Sketch) ApproxEquals(o *Sketch, e float64) bool {
if math.Abs(s.Basic.Sum-o.Basic.Sum) > e {
return false
}
if math.Abs(s.Basic.Avg-o.Basic.Avg) > e {
return false
}
if s.Basic.Min != o.Basic.Min {
return false
}
if s.Basic.Max != o.Basic.Max {
return false
}
if s.Basic.Cnt != o.Basic.Cnt {
return false
}
if s.count != o.count {
return false
}
if len(s.bins) != len(o.bins) {
return false
}
for i := range s.bins {
if o.bins[i] != s.bins[i] {
return false
}
}
return true
}