forked from grafana/carbon-relay-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdummyPackets_test.go
71 lines (63 loc) · 1.84 KB
/
dummyPackets_test.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
package main
// this generates, and provides acces to a bunch of generated metrics/packets
// it does this using a single backing array to save memory allocations
import (
"bytes"
"fmt"
)
// so that whatever the timestamp is, if we add it to this, it will always use same amount
// of chars. in other words, don't use that many DP's that this statement would no longer be true!
const tsTpl = 1000000000
type dummyPackets struct {
key string
amount int
packetLen int
scratch *bytes.Buffer
}
func NewDummyPackets(key string, amount int) *dummyPackets {
tpl := "%s.dummyPacket 123 %d"
packetLen := 17 + 10 + len(key)
scratchBuf := make([]byte, 0, packetLen*amount)
scratch := bytes.NewBuffer(scratchBuf)
for i := 1; i <= amount; i++ {
ts := tsTpl + i
l, err := fmt.Fprintf(scratch, tpl, key, ts)
if err != nil {
panic(err)
}
if packetLen != l {
panic(fmt.Sprintf("bad packet length (or bad write) at index %d. supposed len: %d, real len: %d", i, packetLen, l))
}
}
return &dummyPackets{key, amount, packetLen, scratch}
}
func (dp *dummyPackets) Get(i int) []byte {
if i >= dp.amount {
panic("can't ask for higher index then what we have in dummyPackets")
}
sliceFull := dp.scratch.Bytes()
return sliceFull[dp.packetLen*i : dp.packetLen*(i+1)]
}
func (dp *dummyPackets) All() chan []byte {
ret := make(chan []byte, 10000) // pretty arbitrary, but seems to help perf
go func(dp *dummyPackets, ret chan []byte) {
sliceFull := dp.scratch.Bytes()
for i := 0; i < dp.amount; i++ {
ret <- sliceFull[dp.packetLen*i : dp.packetLen*(i+1)]
}
close(ret)
}(dp, ret)
return ret
}
func mergeAll(in ...chan []byte) chan []byte {
ret := make(chan []byte, 10000) // pretty arbitrary, but seems to help perf
go func() {
for _, inChan := range in {
for val := range inChan {
ret <- val
}
}
close(ret)
}()
return ret
}