| 1 |
// Copyright 2024-2025 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 |
//go:build !skip_js_tests && !skip_js_cluster_tests && !skip_js_cluster_tests_2 |
| 15 |
|
| 16 |
package server |
| 17 |
|
| 18 |
import ( |
| 19 |
"fmt" |
| 20 |
"sync" |
| 21 |
"sync/atomic" |
| 22 |
"testing" |
| 23 |
|
| 24 |
"github.com/nats-io/nats.go" |
| 25 |
) |
| 26 |
|
| 27 |
func BenchmarkJetStreamCreate(b *testing.B) { |
| 28 |
|
| 29 |
const ( |
| 30 |
verbose = false |
| 31 |
resourcePrefix = "S" |
| 32 |
concurrency = 12 |
| 33 |
) |
| 34 |
|
| 35 |
// Types of resource that this benchmark creates |
| 36 |
type ResourceType string |
| 37 |
const ( |
| 38 |
Stream ResourceType = "Stream" |
| 39 |
KVBucket ResourceType = "KVBucket" |
| 40 |
ObjectStore ResourceType = "ObjStore" |
| 41 |
) |
| 42 |
|
| 43 |
resourceTypeCases := []ResourceType{ |
| 44 |
Stream, |
| 45 |
KVBucket, |
| 46 |
ObjectStore, |
| 47 |
} |
| 48 |
|
| 49 |
benchmarksCases := []struct { |
| 50 |
clusterSize int |
| 51 |
replicas int |
| 52 |
storage nats.StorageType |
| 53 |
}{ |
| 54 |
{1, 1, nats.MemoryStorage}, |
| 55 |
{3, 3, nats.MemoryStorage}, |
| 56 |
{3, 3, nats.FileStorage}, |
| 57 |
} |
| 58 |
|
| 59 |
for _, bc := range benchmarksCases { |
| 60 |
bName := fmt.Sprintf( |
| 61 |
"N=%d,R=%d,storage=%s,C=%d", |
| 62 |
bc.clusterSize, |
| 63 |
bc.replicas, |
| 64 |
bc.storage.String(), |
| 65 |
concurrency, |
| 66 |
) |
| 67 |
|
| 68 |
b.Run( |
| 69 |
bName, |
| 70 |
func(b *testing.B) { |
| 71 |
for _, rt := range resourceTypeCases { |
| 72 |
//for _, bc := range benchmarksCases { |
| 73 |
rName := fmt.Sprintf("resource=%s", rt) |
| 74 |
b.Run( |
| 75 |
rName, |
| 76 |
func(b *testing.B) { |
| 77 |
|
| 78 |
if verbose { |
| 79 |
b.Logf( |
| 80 |
"Creating %d %s resources in cluster with %d nodes, R=%d, %s storage", |
| 81 |
b.N, |
| 82 |
string(rt), |
| 83 |
bc.clusterSize, |
| 84 |
bc.replicas, |
| 85 |
bc.storage, |
| 86 |
) |
| 87 |
} |
| 88 |
|
| 89 |
// Setup server or cluster |
| 90 |
_, leaderServer, shutdown, nc, _ := startJSClusterAndConnect(b, bc.clusterSize) |
| 91 |
defer shutdown() |
| 92 |
defer nc.Close() |
| 93 |
|
| 94 |
// All clients connect to cluster (meta) leader for lower variability |
| 95 |
connectURL := leaderServer.ClientURL() |
| 96 |
|
| 97 |
// Wait for all clients and main routine to be ready |
| 98 |
wgReady := sync.WaitGroup{} |
| 99 |
wgReady.Add(concurrency + 1) |
| 100 |
// Wait for all routines to complete |
| 101 |
wgComplete := sync.WaitGroup{} |
| 102 |
wgComplete.Add(concurrency) |
| 103 |
|
| 104 |
// Number of operations (divided amongst clients) |
| 105 |
opsLeft := atomic.Int64{} |
| 106 |
opsLeft.Store(int64(b.N)) |
| 107 |
totalErrors := atomic.Int64{} |
| 108 |
|
| 109 |
// Pre-create connections and JS contexts |
| 110 |
for i := 1; i <= concurrency; i++ { |
| 111 |
nc, js := jsClientConnectURL(b, connectURL) |
| 112 |
defer nc.Close() |
| 113 |
go func(clientId int, nc *nats.Conn, js nats.JetStreamContext) { |
| 114 |
defer wgComplete.Done() |
| 115 |
|
| 116 |
// Config struct (reused and modified in place for each call) |
| 117 |
streamConfig := nats.StreamConfig{ |
| 118 |
Name: "?", |
| 119 |
Storage: bc.storage, |
| 120 |
Replicas: bc.replicas, |
| 121 |
} |
| 122 |
kvConfig := nats.KeyValueConfig{ |
| 123 |
Bucket: "?", |
| 124 |
Storage: bc.storage, |
| 125 |
Replicas: bc.replicas, |
| 126 |
} |
| 127 |
objConfig := nats.ObjectStoreConfig{ |
| 128 |
Bucket: "?", |
| 129 |
Storage: bc.storage, |
| 130 |
Replicas: bc.replicas, |
| 131 |
} |
| 132 |
|
| 133 |
// Block until everyone is ready |
| 134 |
wgReady.Done() |
| 135 |
wgReady.Wait() |
| 136 |
|
| 137 |
errCount := int64(0) |
| 138 |
defer func() { |
| 139 |
// Roll up error count on completion |
| 140 |
totalErrors.Add(errCount) |
| 141 |
}() |
| 142 |
|
| 143 |
// Track per-client opCount (just for logging/debugging) |
| 144 |
opCount := 0 |
| 145 |
for opsLeft.Add(-1) >= 0 { |
| 146 |
var err error |
| 147 |
// Create unique resource name |
| 148 |
resourceName := fmt.Sprintf("%s_%d_%d", resourcePrefix, clientId, opCount) |
| 149 |
switch rt { |
| 150 |
case Stream: |
| 151 |
streamConfig.Name = resourceName |
| 152 |
_, err = js.AddStream(&streamConfig) |
| 153 |
case KVBucket: |
| 154 |
kvConfig.Bucket = resourceName |
| 155 |
_, err = js.CreateKeyValue(&kvConfig) |
| 156 |
case ObjectStore: |
| 157 |
objConfig.Bucket = resourceName |
| 158 |
_, err = js.CreateObjectStore(&objConfig) |
| 159 |
} |
| 160 |
opCount += 1 |
| 161 |
if err != nil { |
| 162 |
b.Logf("Error creating %s (%s): %s", rt, resourceName, err) |
| 163 |
errCount += 1 |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
if verbose { |
| 168 |
b.Logf("Client %d completed %d operations", clientId, opCount) |
| 169 |
} |
| 170 |
|
| 171 |
}(i, nc, js) |
| 172 |
} |
| 173 |
|
| 174 |
// Wait for all clients to be ready |
| 175 |
wgReady.Done() |
| 176 |
wgReady.Wait() |
| 177 |
|
| 178 |
// Start benchmark clock |
| 179 |
b.ResetTimer() |
| 180 |
|
| 181 |
wgComplete.Wait() |
| 182 |
b.StopTimer() |
| 183 |
|
| 184 |
b.ReportMetric(float64(100*(totalErrors.Load()))/float64(b.N), "%error") |
| 185 |
}, |
| 186 |
) |
| 187 |
} |
| 188 |
}, |
| 189 |
) |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
func BenchmarkJetStreamCreateConsumers(b *testing.B) { |
| 194 |
|
| 195 |
const ( |
| 196 |
verbose = false |
| 197 |
streamName = "S" |
| 198 |
consumerPrefix = "C" |
| 199 |
concurrency = 12 |
| 200 |
) |
| 201 |
|
| 202 |
benchmarksCases := []struct { |
| 203 |
clusterSize int |
| 204 |
consumerReplicas int |
| 205 |
consumerStorage nats.StorageType |
| 206 |
}{ |
| 207 |
{1, 1, nats.MemoryStorage}, |
| 208 |
{3, 3, nats.MemoryStorage}, |
| 209 |
{3, 3, nats.FileStorage}, |
| 210 |
} |
| 211 |
|
| 212 |
type ConsumerType string |
| 213 |
const ( |
| 214 |
Ephemeral ConsumerType = "Ephemeral" |
| 215 |
Durable ConsumerType = "Durable" |
| 216 |
) |
| 217 |
|
| 218 |
consumerTypeCases := []ConsumerType{ |
| 219 |
Ephemeral, |
| 220 |
Durable, |
| 221 |
} |
| 222 |
|
| 223 |
for _, bc := range benchmarksCases { |
| 224 |
|
| 225 |
bName := fmt.Sprintf( |
| 226 |
"N=%d,R=%d,storage=%s,C=%d", |
| 227 |
bc.clusterSize, |
| 228 |
bc.consumerReplicas, |
| 229 |
bc.consumerStorage.String(), |
| 230 |
concurrency, |
| 231 |
) |
| 232 |
|
| 233 |
b.Run( |
| 234 |
bName, |
| 235 |
func(b *testing.B) { |
| 236 |
|
| 237 |
for _, ct := range consumerTypeCases { |
| 238 |
|
| 239 |
cName := fmt.Sprintf("Consumer=%s", ct) |
| 240 |
|
| 241 |
b.Run( |
| 242 |
cName, |
| 243 |
func(b *testing.B) { |
| 244 |
if verbose { |
| 245 |
b.Logf( |
| 246 |
"Creating %d consumers in cluster with %d nodes, R=%d, %s storage", |
| 247 |
b.N, |
| 248 |
bc.clusterSize, |
| 249 |
bc.consumerReplicas, |
| 250 |
bc.consumerStorage, |
| 251 |
) |
| 252 |
} |
| 253 |
|
| 254 |
// Setup server or cluster |
| 255 |
_, leaderServer, shutdown, nc, js := startJSClusterAndConnect(b, bc.clusterSize) |
| 256 |
defer shutdown() |
| 257 |
defer nc.Close() |
| 258 |
|
| 259 |
// All clients connect to cluster (meta) leader for lower variability |
| 260 |
connectURL := leaderServer.ClientURL() |
| 261 |
|
| 262 |
// Create stream |
| 263 |
streamConfig := nats.StreamConfig{ |
| 264 |
Name: streamName, |
| 265 |
Storage: nats.FileStorage, |
| 266 |
Replicas: bc.clusterSize, |
| 267 |
} |
| 268 |
|
| 269 |
_, err := js.AddStream(&streamConfig) |
| 270 |
if err != nil { |
| 271 |
b.Fatalf("Failed to create stream: %s", err) |
| 272 |
} |
| 273 |
|
| 274 |
// Wait for all clients and main routine to be ready |
| 275 |
wgReady := sync.WaitGroup{} |
| 276 |
wgReady.Add(concurrency + 1) |
| 277 |
// Wait for all routines to complete |
| 278 |
wgComplete := sync.WaitGroup{} |
| 279 |
wgComplete.Add(concurrency) |
| 280 |
|
| 281 |
// Number of operations (divided amongst clients) |
| 282 |
opsLeft := atomic.Int64{} |
| 283 |
opsLeft.Store(int64(b.N)) |
| 284 |
// Total number of errors |
| 285 |
totalErrors := atomic.Int64{} |
| 286 |
|
| 287 |
// Pre-create connections and JS contexts |
| 288 |
for i := 1; i <= concurrency; i++ { |
| 289 |
nc, js := jsClientConnectURL(b, connectURL) |
| 290 |
defer nc.Close() |
| 291 |
|
| 292 |
go func(clientId int, nc *nats.Conn, js nats.JetStreamContext) { |
| 293 |
defer wgComplete.Done() |
| 294 |
|
| 295 |
// Config struct (reused and modified in place for each call) |
| 296 |
cfg := nats.ConsumerConfig{ |
| 297 |
Durable: "", |
| 298 |
Name: "", |
| 299 |
Replicas: bc.consumerReplicas, |
| 300 |
MemoryStorage: bc.consumerStorage == nats.MemoryStorage, |
| 301 |
} |
| 302 |
|
| 303 |
// Block until everyone is ready |
| 304 |
wgReady.Done() |
| 305 |
wgReady.Wait() |
| 306 |
|
| 307 |
errCount := int64(0) |
| 308 |
opCount := 0 |
| 309 |
for opsLeft.Add(-1) >= 0 { |
| 310 |
var err error |
| 311 |
// Set unique consumer name |
| 312 |
cfg.Name = fmt.Sprintf("%s_%d_%d", consumerPrefix, clientId, opCount) |
| 313 |
if ct == Durable { |
| 314 |
cfg.Durable = cfg.Name |
| 315 |
} |
| 316 |
_, err = js.AddConsumer(streamName, &cfg) |
| 317 |
if err != nil { |
| 318 |
b.Logf("Failed to add consumer: %s", err) |
| 319 |
errCount += 1 |
| 320 |
} |
| 321 |
opCount += 1 |
| 322 |
} |
| 323 |
|
| 324 |
if verbose { |
| 325 |
b.Logf("Client %d completed %d operations", clientId, opCount) |
| 326 |
} |
| 327 |
|
| 328 |
totalErrors.Add(errCount) |
| 329 |
|
| 330 |
}(i, nc, js) |
| 331 |
} |
| 332 |
|
| 333 |
// Wait for all clients to be ready |
| 334 |
wgReady.Done() |
| 335 |
wgReady.Wait() |
| 336 |
|
| 337 |
// Start benchmark clock |
| 338 |
b.ResetTimer() |
| 339 |
|
| 340 |
wgComplete.Wait() |
| 341 |
b.StopTimer() |
| 342 |
|
| 343 |
b.ReportMetric(float64(100*(totalErrors.Load()))/float64(b.N), "%error") |
| 344 |
}, |
| 345 |
) |
| 346 |
} |
| 347 |
}, |
| 348 |
) |
| 349 |
} |
| 350 |
} |