| 1 |
// Copyright 2016-2023 The NATS Authors |
| 2 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
// you may not use this file except in compliance with the License. |
| 4 |
// You may obtain a copy of the License at |
| 5 |
// |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package bench |
| 15 |
|
| 16 |
import ( |
| 17 |
"bytes" |
| 18 |
"encoding/csv" |
| 19 |
"fmt" |
| 20 |
"log" |
| 21 |
"math" |
| 22 |
"strconv" |
| 23 |
"time" |
| 24 |
|
| 25 |
"github.com/nats-io/nats.go" |
| 26 |
"github.com/nats-io/nuid" |
| 27 |
) |
| 28 |
|
| 29 |
// A Sample for a particular client |
| 30 |
type Sample struct { |
| 31 |
JobMsgCnt int |
| 32 |
MsgCnt uint64 |
| 33 |
MsgBytes uint64 |
| 34 |
IOBytes uint64 |
| 35 |
Start time.Time |
| 36 |
End time.Time |
| 37 |
} |
| 38 |
|
| 39 |
// SampleGroup for a number of samples, the group is a Sample itself aggregating the values the Samples |
| 40 |
type SampleGroup struct { |
| 41 |
Sample |
| 42 |
Samples []*Sample |
| 43 |
} |
| 44 |
|
| 45 |
// Benchmark to hold the various Samples organized by publishers and subscribers |
| 46 |
type Benchmark struct { |
| 47 |
Sample |
| 48 |
Name string |
| 49 |
RunID string |
| 50 |
Pubs *SampleGroup |
| 51 |
Subs *SampleGroup |
| 52 |
subChannel chan *Sample |
| 53 |
pubChannel chan *Sample |
| 54 |
} |
| 55 |
|
| 56 |
// NewBenchmark initializes a Benchmark. After creating a bench call AddSubSample/AddPubSample. |
| 57 |
// When done collecting samples, call EndBenchmark |
| 58 |
func NewBenchmark(name string, subCnt, pubCnt int) *Benchmark { |
| 59 |
bm := Benchmark{Name: name, RunID: nuid.Next()} |
| 60 |
bm.Subs = NewSampleGroup() |
| 61 |
bm.Pubs = NewSampleGroup() |
| 62 |
bm.subChannel = make(chan *Sample, subCnt) |
| 63 |
bm.pubChannel = make(chan *Sample, pubCnt) |
| 64 |
return &bm |
| 65 |
} |
| 66 |
|
| 67 |
// Close organizes collected Samples and calculates aggregates. After Close(), no more samples can be added. |
| 68 |
func (bm *Benchmark) Close() { |
| 69 |
close(bm.subChannel) |
| 70 |
close(bm.pubChannel) |
| 71 |
|
| 72 |
for s := range bm.subChannel { |
| 73 |
bm.Subs.AddSample(s) |
| 74 |
} |
| 75 |
for s := range bm.pubChannel { |
| 76 |
bm.Pubs.AddSample(s) |
| 77 |
} |
| 78 |
|
| 79 |
if bm.Subs.HasSamples() { |
| 80 |
bm.Start = bm.Subs.Start |
| 81 |
bm.End = bm.Subs.End |
| 82 |
} else { |
| 83 |
bm.Start = bm.Pubs.Start |
| 84 |
bm.End = bm.Pubs.End |
| 85 |
} |
| 86 |
|
| 87 |
if bm.Subs.HasSamples() && bm.Pubs.HasSamples() { |
| 88 |
if bm.Start.After(bm.Subs.Start) { |
| 89 |
bm.Start = bm.Subs.Start |
| 90 |
} |
| 91 |
if bm.Start.After(bm.Pubs.Start) { |
| 92 |
bm.Start = bm.Pubs.Start |
| 93 |
} |
| 94 |
|
| 95 |
if bm.End.Before(bm.Subs.End) { |
| 96 |
bm.End = bm.Subs.End |
| 97 |
} |
| 98 |
if bm.End.Before(bm.Pubs.End) { |
| 99 |
bm.End = bm.Pubs.End |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
bm.MsgBytes = bm.Pubs.MsgBytes + bm.Subs.MsgBytes |
| 104 |
bm.IOBytes = bm.Pubs.IOBytes + bm.Subs.IOBytes |
| 105 |
bm.MsgCnt = bm.Pubs.MsgCnt + bm.Subs.MsgCnt |
| 106 |
bm.JobMsgCnt = bm.Pubs.JobMsgCnt + bm.Subs.JobMsgCnt |
| 107 |
} |
| 108 |
|
| 109 |
// AddSubSample to the benchmark |
| 110 |
func (bm *Benchmark) AddSubSample(s *Sample) { |
| 111 |
bm.subChannel <- s |
| 112 |
} |
| 113 |
|
| 114 |
// AddPubSample to the benchmark |
| 115 |
func (bm *Benchmark) AddPubSample(s *Sample) { |
| 116 |
bm.pubChannel <- s |
| 117 |
} |
| 118 |
|
| 119 |
// CSV generates a csv report of all the samples collected |
| 120 |
func (bm *Benchmark) CSV() string { |
| 121 |
var buffer bytes.Buffer |
| 122 |
writer := csv.NewWriter(&buffer) |
| 123 |
headers := []string{"#RunID", "ClientID", "MsgCount", "MsgBytes", "MsgsPerSec", "BytesPerSec", "DurationSecs"} |
| 124 |
if err := writer.Write(headers); err != nil { |
| 125 |
log.Fatalf("Error while serializing headers %q: %v", headers, err) |
| 126 |
} |
| 127 |
groups := []*SampleGroup{bm.Subs, bm.Pubs} |
| 128 |
pre := "S" |
| 129 |
for i, g := range groups { |
| 130 |
if i == 1 { |
| 131 |
pre = "P" |
| 132 |
} |
| 133 |
for j, c := range g.Samples { |
| 134 |
r := []string{bm.RunID, fmt.Sprintf("%s%d", pre, j), fmt.Sprintf("%d", c.MsgCnt), fmt.Sprintf("%d", c.MsgBytes), fmt.Sprintf("%d", c.Rate()), fmt.Sprintf("%f", c.Throughput()), fmt.Sprintf("%f", c.Duration().Seconds())} |
| 135 |
if err := writer.Write(r); err != nil { |
| 136 |
log.Fatalf("Error while serializing %v: %v", c, err) |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
writer.Flush() |
| 142 |
return buffer.String() |
| 143 |
} |
| 144 |
|
| 145 |
// NewSample creates a new Sample initialized to the provided values. The nats.Conn information captured |
| 146 |
func NewSample(jobCount int, msgSize int, start, end time.Time, nc *nats.Conn) *Sample { |
| 147 |
s := Sample{JobMsgCnt: jobCount, Start: start, End: end} |
| 148 |
s.MsgBytes = uint64(msgSize * jobCount) |
| 149 |
s.MsgCnt = nc.OutMsgs + nc.InMsgs |
| 150 |
s.IOBytes = nc.OutBytes + nc.InBytes |
| 151 |
return &s |
| 152 |
} |
| 153 |
|
| 154 |
// Throughput of bytes per second |
| 155 |
func (s *Sample) Throughput() float64 { |
| 156 |
return float64(s.MsgBytes) / s.Duration().Seconds() |
| 157 |
} |
| 158 |
|
| 159 |
// Rate of messages in the job per second |
| 160 |
func (s *Sample) Rate() int64 { |
| 161 |
return int64(float64(s.JobMsgCnt) / s.Duration().Seconds()) |
| 162 |
} |
| 163 |
|
| 164 |
func (s *Sample) String() string { |
| 165 |
rate := commaFormat(s.Rate()) |
| 166 |
throughput := HumanBytes(s.Throughput(), false) |
| 167 |
return fmt.Sprintf("%s msgs/sec ~ %s/sec", rate, throughput) |
| 168 |
} |
| 169 |
|
| 170 |
// Duration that the sample was active |
| 171 |
func (s *Sample) Duration() time.Duration { |
| 172 |
return s.End.Sub(s.Start) |
| 173 |
} |
| 174 |
|
| 175 |
// Seconds that the sample or samples were active |
| 176 |
func (s *Sample) Seconds() float64 { |
| 177 |
return s.Duration().Seconds() |
| 178 |
} |
| 179 |
|
| 180 |
// NewSampleGroup initializer |
| 181 |
func NewSampleGroup() *SampleGroup { |
| 182 |
s := new(SampleGroup) |
| 183 |
s.Samples = make([]*Sample, 0) |
| 184 |
return s |
| 185 |
} |
| 186 |
|
| 187 |
// Statistics information of the sample group (min, average, max and standard deviation) |
| 188 |
func (sg *SampleGroup) Statistics() string { |
| 189 |
return fmt.Sprintf("min %s | avg %s | max %s | stddev %s msgs", commaFormat(sg.MinRate()), commaFormat(sg.AvgRate()), commaFormat(sg.MaxRate()), commaFormat(int64(sg.StdDev()))) |
| 190 |
} |
| 191 |
|
| 192 |
// MinRate returns the smallest message rate in the SampleGroup |
| 193 |
func (sg *SampleGroup) MinRate() int64 { |
| 194 |
m := int64(0) |
| 195 |
for i, s := range sg.Samples { |
| 196 |
if i == 0 { |
| 197 |
m = s.Rate() |
| 198 |
} |
| 199 |
m = min(m, s.Rate()) |
| 200 |
} |
| 201 |
return m |
| 202 |
} |
| 203 |
|
| 204 |
// MaxRate returns the largest message rate in the SampleGroup |
| 205 |
func (sg *SampleGroup) MaxRate() int64 { |
| 206 |
m := int64(0) |
| 207 |
for i, s := range sg.Samples { |
| 208 |
if i == 0 { |
| 209 |
m = s.Rate() |
| 210 |
} |
| 211 |
m = max(m, s.Rate()) |
| 212 |
} |
| 213 |
return m |
| 214 |
} |
| 215 |
|
| 216 |
// AvgRate returns the average of all the message rates in the SampleGroup |
| 217 |
func (sg *SampleGroup) AvgRate() int64 { |
| 218 |
if !sg.HasSamples() { |
| 219 |
return 0 |
| 220 |
} |
| 221 |
sum := uint64(0) |
| 222 |
for _, s := range sg.Samples { |
| 223 |
sum += uint64(s.Rate()) |
| 224 |
} |
| 225 |
return int64(sum / uint64(len(sg.Samples))) |
| 226 |
} |
| 227 |
|
| 228 |
// StdDev returns the standard deviation the message rates in the SampleGroup |
| 229 |
func (sg *SampleGroup) StdDev() float64 { |
| 230 |
if !sg.HasSamples() { |
| 231 |
return 0 |
| 232 |
} |
| 233 |
avg := float64(sg.AvgRate()) |
| 234 |
sum := float64(0) |
| 235 |
for _, c := range sg.Samples { |
| 236 |
sum += math.Pow(float64(c.Rate())-avg, 2) |
| 237 |
} |
| 238 |
variance := sum / float64(len(sg.Samples)) |
| 239 |
return math.Sqrt(variance) |
| 240 |
} |
| 241 |
|
| 242 |
// AddSample adds a Sample to the SampleGroup. After adding a Sample it shouldn't be modified. |
| 243 |
func (sg *SampleGroup) AddSample(e *Sample) { |
| 244 |
sg.Samples = append(sg.Samples, e) |
| 245 |
|
| 246 |
if len(sg.Samples) == 1 { |
| 247 |
sg.Start = e.Start |
| 248 |
sg.End = e.End |
| 249 |
} |
| 250 |
sg.IOBytes += e.IOBytes |
| 251 |
sg.JobMsgCnt += e.JobMsgCnt |
| 252 |
sg.MsgCnt += e.MsgCnt |
| 253 |
sg.MsgBytes += e.MsgBytes |
| 254 |
|
| 255 |
if e.Start.Before(sg.Start) { |
| 256 |
sg.Start = e.Start |
| 257 |
} |
| 258 |
|
| 259 |
if e.End.After(sg.End) { |
| 260 |
sg.End = e.End |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
// HasSamples returns true if the group has samples |
| 265 |
func (sg *SampleGroup) HasSamples() bool { |
| 266 |
return len(sg.Samples) > 0 |
| 267 |
} |
| 268 |
|
| 269 |
// Report returns a human readable report of the samples taken in the Benchmark |
| 270 |
func (bm *Benchmark) Report() string { |
| 271 |
var buffer bytes.Buffer |
| 272 |
|
| 273 |
indent := "" |
| 274 |
if !bm.Pubs.HasSamples() && !bm.Subs.HasSamples() { |
| 275 |
return "No publisher or subscribers. Nothing to report." |
| 276 |
} |
| 277 |
|
| 278 |
if bm.Pubs.HasSamples() && bm.Subs.HasSamples() { |
| 279 |
buffer.WriteString(fmt.Sprintf("%s Pub/Sub stats: %s\n", bm.Name, bm)) |
| 280 |
indent += " " |
| 281 |
} |
| 282 |
if bm.Pubs.HasSamples() { |
| 283 |
buffer.WriteString(fmt.Sprintf("%sPub stats: %s\n", indent, bm.Pubs)) |
| 284 |
if len(bm.Pubs.Samples) > 1 { |
| 285 |
for i, stat := range bm.Pubs.Samples { |
| 286 |
buffer.WriteString(fmt.Sprintf("%s [%d] %v (%d msgs)\n", indent, i+1, stat, stat.JobMsgCnt)) |
| 287 |
} |
| 288 |
buffer.WriteString(fmt.Sprintf("%s %s\n", indent, bm.Pubs.Statistics())) |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
if bm.Subs.HasSamples() { |
| 293 |
buffer.WriteString(fmt.Sprintf("%sSub stats: %s\n", indent, bm.Subs)) |
| 294 |
if len(bm.Subs.Samples) > 1 { |
| 295 |
for i, stat := range bm.Subs.Samples { |
| 296 |
buffer.WriteString(fmt.Sprintf("%s [%d] %v (%d msgs)\n", indent, i+1, stat, stat.JobMsgCnt)) |
| 297 |
} |
| 298 |
buffer.WriteString(fmt.Sprintf("%s %s\n", indent, bm.Subs.Statistics())) |
| 299 |
} |
| 300 |
} |
| 301 |
return buffer.String() |
| 302 |
} |
| 303 |
|
| 304 |
func commaFormat(n int64) string { |
| 305 |
in := strconv.FormatInt(n, 10) |
| 306 |
out := make([]byte, len(in)+(len(in)-2+int(in[0]/'0'))/3) |
| 307 |
if in[0] == '-' { |
| 308 |
in, out[0] = in[1:], '-' |
| 309 |
} |
| 310 |
for i, j, k := len(in)-1, len(out)-1, 0; ; i, j = i-1, j-1 { |
| 311 |
out[j] = in[i] |
| 312 |
if i == 0 { |
| 313 |
return string(out) |
| 314 |
} |
| 315 |
if k++; k == 3 { |
| 316 |
j, k = j-1, 0 |
| 317 |
out[j] = ',' |
| 318 |
} |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
// HumanBytes formats bytes as a human readable string |
| 323 |
func HumanBytes(bytes float64, si bool) string { |
| 324 |
var base = 1024 |
| 325 |
pre := []string{"K", "M", "G", "T", "P", "E"} |
| 326 |
var post = "B" |
| 327 |
if si { |
| 328 |
base = 1000 |
| 329 |
pre = []string{"k", "M", "G", "T", "P", "E"} |
| 330 |
post = "iB" |
| 331 |
} |
| 332 |
if bytes < float64(base) { |
| 333 |
return fmt.Sprintf("%.2f B", bytes) |
| 334 |
} |
| 335 |
exp := int(math.Log(bytes) / math.Log(float64(base))) |
| 336 |
index := exp - 1 |
| 337 |
units := pre[index] + post |
| 338 |
return fmt.Sprintf("%.2f %s", bytes/math.Pow(float64(base), float64(exp)), units) |
| 339 |
} |
| 340 |
|
| 341 |
// MsgsPerClient divides the number of messages by the number of clients and tries to distribute them as evenly as possible |
| 342 |
func MsgsPerClient(numMsgs, numClients int) []int { |
| 343 |
var counts []int |
| 344 |
if numClients == 0 || numMsgs == 0 { |
| 345 |
return counts |
| 346 |
} |
| 347 |
counts = make([]int, numClients) |
| 348 |
mc := numMsgs / numClients |
| 349 |
for i := 0; i < numClients; i++ { |
| 350 |
counts[i] = mc |
| 351 |
} |
| 352 |
extra := numMsgs % numClients |
| 353 |
for i := 0; i < extra; i++ { |
| 354 |
counts[i]++ |
| 355 |
} |
| 356 |
return counts |
| 357 |
} |