forked from redpanda-data/connect
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bench.go
209 lines (175 loc) · 5.94 KB
/
bench.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
/*
Copyright (c) 2014 Ashley Jeffs
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package test
import (
"encoding/json"
"fmt"
"math"
"sort"
"time"
"github.com/jeffail/benthos/lib/types"
)
//--------------------------------------------------------------------------------------------------
func bytesToIndex(b []byte) (index int32) {
if len(b) <= 3 {
return index
}
index = int32(b[0])<<24 |
int32(b[1])<<16 |
int32(b[2])<<8 |
int32(b[3])
return index
}
func indexToBytes(index int32) (b [4]byte) {
b[0] = byte(index >> 24)
b[1] = byte(index >> 16)
b[2] = byte(index >> 8)
b[3] = byte(index)
return b
}
//--------------------------------------------------------------------------------------------------
// Bench - A struct carrying message specific benchmarking statistics.
type Bench struct {
Latency int // Time taken (ns) for a message to be received by the consumer.
NBytes int // Number of bytes carried in the message.
Index int32 // The index carried by the message, can be used to detect loss.
}
/*
NewBenchMessage - Create a message carrying information used to calc benchmarks on the other end of
a transport.
*/
func NewBenchMessage(index int32, dataBlob []byte) types.Message {
msg := types.Message{
Parts: make([][]byte, 3),
}
indexBytes := indexToBytes(index)
msg.Parts[1] = indexBytes[0:4]
msg.Parts[2] = dataBlob
var err error
msg.Parts[0], err = time.Now().MarshalBinary()
if err != nil {
panic(err)
}
return msg
}
// BenchFromMessage - Returns the benchmarking stats from a message received.
func BenchFromMessage(msg types.Message) (Bench, error) {
var b Bench
if len(msg.Parts) < 2 {
return b, fmt.Errorf("Benchmark requires at least 2 message parts, received: %v", len(msg.Parts))
}
t := time.Time{}
if err := t.UnmarshalBinary(msg.Parts[0]); err != nil {
return b, err
}
b.Latency = int(time.Since(t))
b.Index = bytesToIndex(msg.Parts[1])
for _, part := range msg.Parts {
b.NBytes = b.NBytes + int(len(part))
}
return b, nil
}
//--------------------------------------------------------------------------------------------------
/*
StartPrintingBenchmarks - Starts a goroutine that will periodically print any statistics/benchmarks
that are accumulated through messages, and also any lost interactions as per the startIndex. If you
want to disable data loss detection then set the startIndex to -1. You must provide the messages via
the write channel returned by the function.
*/
func StartPrintingBenchmarks(period time.Duration, startIndex int32) chan<- Bench {
c := make(chan Bench, 100)
currentIndex := startIndex
dataMissing := map[int32]struct{}{}
type statTally struct {
startedAt time.Time
latencies []int
totalLatency int
totalBytes int
Tally int `json:"total"`
MeanLatency int `json:"mean_latency_ns"`
MeanLatencyStr string `json:"mean_latency"`
PercentileLatency int `json:"99%_latency_ns"`
PercentileLatencyStr string `json:"99%_latency"`
ByteRate float64 `json:"bytes_per_s"`
MessageRate float64 `json:"msgs_per_s"`
}
pStats := statTally{startedAt: time.Now()}
updateStats := func(bench Bench) {
pStats.Tally++
pStats.latencies = append(pStats.latencies, bench.Latency)
pStats.totalLatency = pStats.totalLatency + bench.Latency
pStats.totalBytes = pStats.totalBytes + bench.NBytes
}
refreshPStats := func() {
pStats = statTally{startedAt: time.Now()}
}
calcStats := func() {
if pStats.Tally > 0 {
pStats.MeanLatency = pStats.totalLatency / pStats.Tally
pStats.MeanLatencyStr = time.Duration(pStats.MeanLatency).String()
pStats.ByteRate = float64(pStats.totalBytes) / time.Since(pStats.startedAt).Seconds()
pStats.MessageRate = float64(pStats.Tally) / time.Since(pStats.startedAt).Seconds()
// Calc 99th percentile
index := int(math.Ceil(0.99 * float64(pStats.Tally)))
sort.Ints(pStats.latencies)
if index < len(pStats.latencies) {
pStats.PercentileLatency = pStats.latencies[index]
pStats.PercentileLatencyStr = time.Duration(pStats.PercentileLatency).String()
}
}
}
go func() {
timer := time.NewTicker(period)
defer timer.Stop()
for {
select {
case bench, open := <-c:
if !open {
return
}
if startIndex != -1 {
if startIndex == bench.Index {
// Indicates that the producer index has been restarted.
dataMissing = map[int32]struct{}{}
currentIndex = bench.Index
}
delete(dataMissing, bench.Index)
for i := currentIndex; i < bench.Index; i++ {
dataMissing[i] = struct{}{}
}
currentIndex = bench.Index + 1
}
updateStats(bench)
case <-timer.C:
calcStats()
blob, err := json.Marshal(pStats)
if err != nil {
fmt.Println(err)
}
fmt.Println(string(blob))
if nLost := len(dataMissing); nLost > 0 {
fmt.Printf("{\"data_lost\":%v}\n", nLost)
}
refreshPStats()
}
}
}()
return c
}
//--------------------------------------------------------------------------------------------------