| 1 |
// Copyright 2012-2019 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 test |
| 15 |
|
| 16 |
import ( |
| 17 |
"fmt" |
| 18 |
"math/rand/v2" |
| 19 |
"sync/atomic" |
| 20 |
"testing" |
| 21 |
"time" |
| 22 |
|
| 23 |
"github.com/nats-io/nats.go" |
| 24 |
) |
| 25 |
|
| 26 |
func BenchmarkPublishSpeed(b *testing.B) { |
| 27 |
withServerB(b, func(b *testing.B, nc *nats.Conn) { |
| 28 |
msg := []byte("Hello World") |
| 29 |
b.ResetTimer() |
| 30 |
for range b.N { |
| 31 |
if err := nc.Publish("foo", msg); err != nil { |
| 32 |
b.Fatalf("Error in benchmark during Publish: %v\n", err) |
| 33 |
} |
| 34 |
} |
| 35 |
nc.Flush() |
| 36 |
b.StopTimer() |
| 37 |
}) |
| 38 |
} |
| 39 |
|
| 40 |
func BenchmarkPublishSpeedHeaders(b *testing.B) { |
| 41 |
withServerB(b, func(b *testing.B, nc *nats.Conn) { |
| 42 |
headers := make(nats.Header, 10) |
| 43 |
for i := range 10 { |
| 44 |
headers.Add( |
| 45 |
fmt.Sprintf("header_%d", i), |
| 46 |
generateRandomString(32), |
| 47 |
) |
| 48 |
} |
| 49 |
msg := &nats.Msg{ |
| 50 |
Subject: "foo", |
| 51 |
Header: headers, |
| 52 |
Data: []byte("Hello World"), |
| 53 |
} |
| 54 |
|
| 55 |
b.ResetTimer() |
| 56 |
for range b.N { |
| 57 |
if err := nc.PublishMsg(msg); err != nil { |
| 58 |
b.Fatalf("Error in benchmark during PublishMsg: %v\n", err) |
| 59 |
} |
| 60 |
} |
| 61 |
nc.Flush() |
| 62 |
b.StopTimer() |
| 63 |
}) |
| 64 |
} |
| 65 |
|
| 66 |
func generateRandomString(length int) string { |
| 67 |
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" |
| 68 |
b := make([]byte, length) |
| 69 |
for i := range b { |
| 70 |
b[i] = charset[rand.IntN(len(charset))] |
| 71 |
} |
| 72 |
return string(b) |
| 73 |
} |
| 74 |
|
| 75 |
func BenchmarkPubSubSpeed(b *testing.B) { |
| 76 |
withServerB(b, func(b *testing.B, nc *nats.Conn) { |
| 77 |
ch := make(chan bool) |
| 78 |
|
| 79 |
nc.SetErrorHandler(func(nc *nats.Conn, s *nats.Subscription, err error) { |
| 80 |
b.Fatalf("Error : %v\n", err) |
| 81 |
}) |
| 82 |
|
| 83 |
received := int32(0) |
| 84 |
|
| 85 |
nc.Subscribe("foo", func(m *nats.Msg) { |
| 86 |
if nr := atomic.AddInt32(&received, 1); nr >= int32(b.N) { |
| 87 |
ch <- true |
| 88 |
} |
| 89 |
}) |
| 90 |
|
| 91 |
msg := []byte("Hello World") |
| 92 |
|
| 93 |
b.ResetTimer() |
| 94 |
for range b.N { |
| 95 |
if err := nc.Publish("foo", msg); err != nil { |
| 96 |
b.Fatalf("Error in benchmark during Publish: %v\n", err) |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
err := WaitTime(ch, 10*time.Second) |
| 101 |
if err != nil { |
| 102 |
b.Fatal("Timed out waiting for messages") |
| 103 |
} else if atomic.LoadInt32(&received) != int32(b.N) { |
| 104 |
b.Fatalf("Received: %d, err:%v", received, nc.LastError()) |
| 105 |
} |
| 106 |
b.StopTimer() |
| 107 |
}) |
| 108 |
} |
| 109 |
|
| 110 |
func BenchmarkAsyncSubscriptionCreationSpeed(b *testing.B) { |
| 111 |
withServerB(b, func(b *testing.B, nc *nats.Conn) { |
| 112 |
b.ResetTimer() |
| 113 |
b.ReportAllocs() |
| 114 |
for range b.N { |
| 115 |
nc.Subscribe("foo", func(m *nats.Msg) {}) |
| 116 |
} |
| 117 |
}) |
| 118 |
} |
| 119 |
|
| 120 |
func BenchmarkSyncSubscriptionCreationSpeed(b *testing.B) { |
| 121 |
withServerB(b, func(b *testing.B, nc *nats.Conn) { |
| 122 |
b.ResetTimer() |
| 123 |
b.ReportAllocs() |
| 124 |
for range b.N { |
| 125 |
nc.SubscribeSync("foo") |
| 126 |
} |
| 127 |
}) |
| 128 |
} |
| 129 |
|
| 130 |
func BenchmarkInboxCreation(b *testing.B) { |
| 131 |
for range b.N { |
| 132 |
nats.NewInbox() |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
func BenchmarkNewInboxCreation(b *testing.B) { |
| 137 |
withServerB(b, func(b *testing.B, nc *nats.Conn) { |
| 138 |
b.ResetTimer() |
| 139 |
for range b.N { |
| 140 |
nc.NewRespInbox() |
| 141 |
} |
| 142 |
}) |
| 143 |
} |
| 144 |
|
| 145 |
func BenchmarkRequest(b *testing.B) { |
| 146 |
withServerB(b, func(b *testing.B, nc *nats.Conn) { |
| 147 |
ok := []byte("ok") |
| 148 |
nc.Subscribe("req", func(m *nats.Msg) { |
| 149 |
nc.Publish(m.Reply, ok) |
| 150 |
}) |
| 151 |
b.ResetTimer() |
| 152 |
b.ReportAllocs() |
| 153 |
q := []byte("q") |
| 154 |
for range b.N { |
| 155 |
_, err := nc.Request("req", q, 1*time.Second) |
| 156 |
if err != nil { |
| 157 |
b.Fatalf("Err %v\n", err) |
| 158 |
} |
| 159 |
} |
| 160 |
}) |
| 161 |
} |
| 162 |
|
| 163 |
func BenchmarkOldRequest(b *testing.B) { |
| 164 |
c := newTester(b) |
| 165 |
inst := c.CreateServer(b, false) |
| 166 |
b.Cleanup(func() { inst.Destroy(b) }) |
| 167 |
nc := dialInstance(b, inst, nats.UseOldRequestStyle()) |
| 168 |
|
| 169 |
ok := []byte("ok") |
| 170 |
nc.Subscribe("req", func(m *nats.Msg) { |
| 171 |
nc.Publish(m.Reply, ok) |
| 172 |
}) |
| 173 |
b.ResetTimer() |
| 174 |
b.ReportAllocs() |
| 175 |
q := []byte("q") |
| 176 |
for range b.N { |
| 177 |
_, err := nc.Request("req", q, 1*time.Second) |
| 178 |
if err != nil { |
| 179 |
b.Fatalf("Err %v\n", err) |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
func BenchmarkPublishValidation(b *testing.B) { |
| 185 |
msgPayload := []byte("test") |
| 186 |
shortSubject := "foo.bar" // 7 chars |
| 187 |
longSubject := "metrics.production.server01.cpu.usage.percent" // 45 chars |
| 188 |
|
| 189 |
run := func(name string, subject string, opts ...nats.Option) { |
| 190 |
b.Run(name, func(b *testing.B) { |
| 191 |
c := newTester(b) |
| 192 |
inst := c.CreateServer(b, false) |
| 193 |
b.Cleanup(func() { inst.Destroy(b) }) |
| 194 |
nc := dialInstance(b, inst, opts...) |
| 195 |
|
| 196 |
b.ResetTimer() |
| 197 |
for b.Loop() { |
| 198 |
if err := nc.Publish(subject, msgPayload); err != nil { |
| 199 |
b.Fatalf("Error publishing message: %v", err) |
| 200 |
} |
| 201 |
} |
| 202 |
nc.Flush() |
| 203 |
b.StopTimer() |
| 204 |
}) |
| 205 |
} |
| 206 |
|
| 207 |
run("skip validation, short subject", shortSubject, nats.SkipSubjectValidation()) |
| 208 |
run("skip validation, long subject", longSubject, nats.SkipSubjectValidation()) |
| 209 |
run("with validation, short subject", shortSubject) |
| 210 |
run("with validation, long subject", longSubject) |
| 211 |
} |
| 212 |
|
| 213 |
func BenchmarkPublishWithWriteBufferSize(b *testing.B) { |
| 214 |
payloads := []struct { |
| 215 |
name string |
| 216 |
size int |
| 217 |
}{ |
| 218 |
{"16B", 16}, |
| 219 |
{"128B", 128}, |
| 220 |
{"512B", 512}, |
| 221 |
} |
| 222 |
bufSizes := []int{512, 4096, 8192, 16384, 32768, 65536, 131072} |
| 223 |
|
| 224 |
for _, p := range payloads { |
| 225 |
msg := make([]byte, p.size) |
| 226 |
for i := range msg { |
| 227 |
msg[i] = byte(i) |
| 228 |
} |
| 229 |
for _, sz := range bufSizes { |
| 230 |
b.Run(fmt.Sprintf("payload_%s/buf_%d", p.name, sz), func(b *testing.B) { |
| 231 |
c := newTester(b) |
| 232 |
inst := c.CreateServer(b, false) |
| 233 |
b.Cleanup(func() { inst.Destroy(b) }) |
| 234 |
nc := dialInstance(b, inst, nats.WriteBufferSize(sz)) |
| 235 |
|
| 236 |
b.ResetTimer() |
| 237 |
for range b.N { |
| 238 |
if err := nc.Publish("foo", msg); err != nil { |
| 239 |
b.Fatalf("Error publishing: %v", err) |
| 240 |
} |
| 241 |
} |
| 242 |
b.StopTimer() |
| 243 |
nc.Flush() |
| 244 |
}) |
| 245 |
} |
| 246 |
} |
| 247 |
} |