-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
470 lines (407 loc) · 18.5 KB
/
Copy pathProgram.cs
File metadata and controls
470 lines (407 loc) · 18.5 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
using System.Diagnostics;
using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;
using MongoDB.Bson;
using MongoDB.Driver;
// Configuration - uses environment variables with sensible defaults for local dev
var rabbitManagementUrl = Environment.GetEnvironmentVariable("RABBIT_MANAGEMENT_URL") ?? "http://localhost:15672";
var rabbitUser = Environment.GetEnvironmentVariable("RABBIT_USER") ?? "guest";
var rabbitPass = Environment.GetEnvironmentVariable("RABBIT_PASS") ?? "guest";
var rabbitVhost = Environment.GetEnvironmentVariable("RABBIT_VHOST") ?? "%2F"; // URL-encoded "/"
var queueName = Environment.GetEnvironmentVariable("RABBIT_QUEUE") ?? "audit";
var mongoConnectionString = Environment.GetEnvironmentVariable("MONGO_CONNECTION_STRING") ?? "mongodb://localhost:27017";
var mongoDatabaseName = Environment.GetEnvironmentVariable("MONGO_DATABASE") ?? "audit";
var collectionName = Environment.GetEnvironmentVariable("MONGO_COLLECTION") ?? "processedMessages";
var pollIntervalMs = 2000;
var maxRunTime = TimeSpan.FromMinutes(60);
// Thresholds (matching CheckMongoDbCachePressure)
const double dirtyThreshold = 15.0;
const double latencyThreshold = 500.0;
// Setup RabbitMQ HTTP client
using var http = new HttpClient { BaseAddress = new Uri(rabbitManagementUrl) };
http.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
"Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes($"{rabbitUser}:{rabbitPass}")));
// Setup MongoDB client
var mongoClient = new MongoClient(mongoConnectionString);
var db = mongoClient.GetDatabase(mongoDatabaseName);
var collection = db.GetCollection<BsonDocument>(collectionName);
var bodiesCollection = db.GetCollection<BsonDocument>("messageBodies");
// State for delta calculations
long? prevMongoCount = null;
long? prevBodyCount = null;
long? prevTtlDeleted = null;
long prevTtlPasses = 0;
DateTime prevTime = DateTime.UtcNow;
int rowCount = 0;
const int headerRepeatInterval = 20;
// Queue depth trend tracking
const int trendWindowSize = 15; // 30 seconds of samples at 2s intervals
var queueDepthWindow = new Queue<long>();
long? prevQueueDepth = null;
int consecutiveGrowth = 0;
// Body lag smoothing (60s window = 30 samples at 2s intervals, matches TTL monitor cycle)
const int bodyLagSmoothingWindow = 30;
var bodyLagWindow = new Queue<long>();
// Summary stats accumulators
var summaryStats = new SummaryStats();
// CSV log file
var logPath = Path.Combine(AppContext.BaseDirectory, $"throughput-{DateTime.Now:yyyyMMdd-HHmmss}.csv");
await using var logWriter = new StreamWriter(logPath, append: false);
await logWriter.WriteLineAsync("Timestamp,RMQ_Publish_MsgSec,RMQ_Deliver_MsgSec,RMQ_Ack_MsgSec,Queue_Depth,Mongo_Docs,Mongo_MsgSec,Body_MsgSec,Body_Lag,TTL_Del_Sec,TTL_Pass,Dirty_Pct,Used_Pct,Ping_Ms,Data_MB,Storage_MB,Index_MB,Queue_Trend,Status");
Console.WriteLine($"Throughput Monitor - logging to {logPath}");
Console.WriteLine($"RabbitMQ: {rabbitManagementUrl} queue={queueName} | MongoDB: {mongoDatabaseName}.{collectionName}");
Console.WriteLine($"Thresholds: dirty >= {dirtyThreshold}%, latency >= {latencyThreshold}ms | Max run: {maxRunTime.TotalMinutes}min");
Console.WriteLine();
PrintHeaders();
var startTime = DateTime.UtcNow;
using var cts = new CancellationTokenSource(maxRunTime);
// Ensure Ctrl+C triggers graceful shutdown (summary will print)
Console.CancelKeyPress += (_, e) =>
{
e.Cancel = true; // Prevent immediate termination
cts.Cancel();
};
try
{
while (!cts.Token.IsCancellationRequested)
{
try
{
var now = DateTime.UtcNow;
var elapsed = (now - prevTime).TotalSeconds;
// --- RabbitMQ (use built-in smoothed rates from Management API) ---
long queueDepth = 0;
double publishRate = 0;
double deliverRate = 0;
double ackRate = 0;
try
{
var response = await http.GetAsync($"/api/queues/{rabbitVhost}/{queueName}");
response.EnsureSuccessStatusCode();
var json = await response.Content.ReadAsStringAsync();
using var doc = JsonDocument.Parse(json);
var root = doc.RootElement;
queueDepth = root.GetProperty("messages").GetInt64();
if (root.TryGetProperty("message_stats", out var stats))
{
publishRate = GetRate(stats, "publish_details");
deliverRate = GetRate(stats, "deliver_get_details");
ackRate = GetRate(stats, "ack_details");
}
}
catch (Exception ex)
{
Console.Error.WriteLine($" RabbitMQ error: {ex.Message}");
}
// --- MongoDB doc count ---
long mongoCount = 0;
try
{
mongoCount = await collection.EstimatedDocumentCountAsync();
}
catch (Exception ex)
{
Console.Error.WriteLine($" MongoDB error: {ex.Message}");
}
// --- MongoDB body count ---
long bodyCount = 0;
try
{
bodyCount = await bodiesCollection.EstimatedDocumentCountAsync();
}
catch (Exception ex)
{
Console.Error.WriteLine($" MongoDB bodies error: {ex.Message}");
}
// --- MongoDB health: WiredTiger cache + ping latency + storage ---
double dirtyPct = 0;
double usedPct = 0;
double pingMs = 0;
long ttlDeletedTotal = 0;
long ttlPasses = 0;
double dataMb = 0;
double storageMb = 0;
double indexMb = 0;
var status = "OK";
try
{
// Ping latency
var sw = Stopwatch.StartNew();
await db.RunCommandAsync<BsonDocument>(new BsonDocument("ping", 1));
sw.Stop();
pingMs = sw.Elapsed.TotalMilliseconds;
// serverStatus for WiredTiger + TTL metrics
var serverStatus = await db.RunCommandAsync<BsonDocument>(new BsonDocument("serverStatus", 1));
// WiredTiger cache metrics
if (serverStatus.Contains("wiredTiger"))
{
var cache = serverStatus["wiredTiger"].AsBsonDocument["cache"].AsBsonDocument;
var dirtyBytes = cache["tracked dirty bytes in the cache"].ToDouble();
var totalBytes = cache["bytes currently in the cache"].ToDouble();
var maxBytes = cache["maximum bytes configured"].ToDouble();
dirtyPct = maxBytes > 0 ? dirtyBytes / maxBytes * 100 : 0;
usedPct = maxBytes > 0 ? totalBytes / maxBytes * 100 : 0;
if (dirtyPct >= dirtyThreshold)
{
status = "DIRTY!";
}
}
// TTL metrics
if (serverStatus.Contains("metrics"))
{
var ttl = serverStatus["metrics"].AsBsonDocument["ttl"].AsBsonDocument;
ttlDeletedTotal = ttl["deletedDocuments"].ToInt64();
ttlPasses = ttl["passes"].ToInt64();
}
if (pingMs >= latencyThreshold)
{
status = "SLOW!";
}
// Database storage size
var dbStats = await db.RunCommandAsync<BsonDocument>(new BsonDocument("dbStats", 1));
dataMb = dbStats["dataSize"].ToDouble() / (1024 * 1024);
storageMb = dbStats["storageSize"].ToDouble() / (1024 * 1024);
indexMb = dbStats["indexSize"].ToDouble() / (1024 * 1024);
}
catch (Exception ex)
{
Console.Error.WriteLine($" MongoDB health error: {ex.Message}");
status = "ERR";
}
// --- Output ---
if (prevMongoCount.HasValue && elapsed > 0)
{
var ttlDeletedDelta = prevTtlDeleted.HasValue ? (ttlDeletedTotal - prevTtlDeleted.Value) : 0;
var ttlDelPerSec = ttlDeletedDelta / elapsed;
// Split TTL deletes proportionally between collections (TTL metric is server-wide)
double mongoTtlShare, bodyTtlShare;
if (bodyCount > 0 && mongoCount > 0)
{
var totalDocs = (double)(mongoCount + bodyCount);
mongoTtlShare = ttlDeletedDelta * (mongoCount / totalDocs);
bodyTtlShare = ttlDeletedDelta * (bodyCount / totalDocs);
}
else
{
mongoTtlShare = ttlDeletedDelta;
bodyTtlShare = 0;
}
var mongoRate = (mongoCount - prevMongoCount.Value + mongoTtlShare) / elapsed;
var bodyRate = prevBodyCount.HasValue ? (bodyCount - prevBodyCount.Value + bodyTtlShare) / elapsed : 0;
// Smooth body lag over 60s window to dampen TTL timing skew between collections
var rawBodyLag = bodyCount == 0 && bodyRate == 0 ? 0 : mongoCount - bodyCount;
bodyLagWindow.Enqueue(rawBodyLag);
while (bodyLagWindow.Count > bodyLagSmoothingWindow) bodyLagWindow.Dequeue();
var bodyLag = (long)bodyLagWindow.Average();
var ttlPassDelta = ttlPasses - prevTtlPasses;
var ttlFlag = ttlPassDelta > 0 ? "TTL" : "";
var timestamp = DateTime.Now.ToString("HH:mm:ss.f");
// Queue depth trend detection
if (prevQueueDepth.HasValue)
{
if (queueDepth > prevQueueDepth.Value)
consecutiveGrowth++;
else if (queueDepth < prevQueueDepth.Value)
consecutiveGrowth = Math.Min(consecutiveGrowth - 1, 0); // decay towards negative
else
consecutiveGrowth = 0; // stable resets
}
prevQueueDepth = queueDepth;
// Sliding window for trend slope
queueDepthWindow.Enqueue(queueDepth);
while (queueDepthWindow.Count > trendWindowSize) queueDepthWindow.Dequeue();
// Calculate net queue growth rate (msgs/sec) over the window
double queueGrowthRate = 0;
if (queueDepthWindow.Count >= 2)
{
var windowItems = queueDepthWindow.ToArray();
queueGrowthRate = (windowItems[^1] - windowItems[0]) / ((queueDepthWindow.Count - 1) * (pollIntervalMs / 1000.0));
}
// Trend indicator
var trend = consecutiveGrowth >= 5 ? "BACKLOG"
: consecutiveGrowth >= 2 ? "growing"
: consecutiveGrowth <= -2 ? "drain"
: "stable";
// Override status for backlog
if (consecutiveGrowth >= 5 && status == "OK")
{
status = "BACKLOG";
}
// Track summary stats
summaryStats.Record(publishRate, deliverRate, ackRate, queueDepth, mongoRate,
bodyRate, bodyLag, ttlDelPerSec, dirtyPct, usedPct, pingMs, dataMb, storageMb, indexMb, queueGrowthRate, status);
if (rowCount > 0 && rowCount % headerRepeatInterval == 0)
{
PrintHeaders();
}
rowCount++;
Console.WriteLine(
$"{timestamp,-12} | {publishRate,8:F1} | {deliverRate,8:F1} | {ackRate,8:F1} | {queueDepth,8:N0} | {mongoCount,10:N0} | {mongoRate,8:F1} | {bodyRate,8:F1} | {bodyLag,8:N0} | {ttlDelPerSec,8:F0} | {ttlFlag,4} | {dirtyPct,6:F1}% | {usedPct,6:F1}% | {pingMs,6:F0}ms | {storageMb,6:F0}MB | {trend,7} | {status}");
await logWriter.WriteLineAsync(
$"{DateTime.Now:O},{publishRate:F1},{deliverRate:F1},{ackRate:F1},{queueDepth},{mongoCount},{mongoRate:F1},{bodyRate:F1},{bodyLag},{ttlDelPerSec:F0},{ttlPasses},{dirtyPct:F1},{usedPct:F1},{pingMs:F0},{dataMb:F1},{storageMb:F1},{indexMb:F1},{queueGrowthRate:F1},{status}");
await logWriter.FlushAsync();
}
else
{
Console.WriteLine($"{"Warming up...",-12} | {"---",8} | {"---",8} | {"---",8} | {queueDepth,8:N0} | {mongoCount,10:N0} | {"---",8} | {"---",8} | {"---",8} | {"---",8} | {"",4} | {dirtyPct,6:F1}% | {usedPct,6:F1}% | {pingMs,6:F0}ms | {storageMb,6:F0}MB | {status}");
}
prevMongoCount = mongoCount;
prevBodyCount = bodyCount;
prevTtlDeleted = ttlDeletedTotal;
prevTtlPasses = ttlPasses;
prevTime = now;
await Task.Delay(pollIntervalMs, cts.Token);
}
catch (OperationCanceledException)
{
break;
}
catch (Exception ex)
{
Console.Error.WriteLine($"Error: {ex.Message}");
await Task.Delay(pollIntervalMs);
}
}
}
finally
{
// --- Summary ---
var totalElapsed = DateTime.UtcNow - startTime;
Console.WriteLine();
Console.WriteLine(new string('=', 80));
Console.WriteLine($" SUMMARY | Duration: {totalElapsed:m\\:ss} | Samples: {summaryStats.Count} | Log: {logPath}");
Console.WriteLine(new string('=', 80));
if (summaryStats.Count > 0)
{
Console.WriteLine();
Console.WriteLine($" {"Metric",-24} | {"Avg",10} | {"Min",10} | {"Max",10} | {"P95",10}");
Console.WriteLine($" {new string('-', 22)} | {new string('-', 10)} | {new string('-', 10)} | {new string('-', 10)} | {new string('-', 10)}");
PrintStat("RMQ Publish (msg/s)", summaryStats.Publish);
PrintStat("RMQ Deliver (msg/s)", summaryStats.Deliver);
PrintStat("RMQ Ack (msg/s)", summaryStats.Ack);
PrintStat("Queue Depth", summaryStats.QueueDepth);
PrintStat("Mongo Stored (msg/s)", summaryStats.MongoRate);
PrintStat("Body Written (msg/s)", summaryStats.BodyRate);
PrintStat("Body Lag (docs)", summaryStats.BodyLag);
PrintStat("TTL Deletes (del/s)", summaryStats.TtlDel);
PrintStat("Dirty Cache (%)", summaryStats.Dirty);
PrintStat("Used Cache (%)", summaryStats.Used);
PrintStat("Ping Latency (ms)", summaryStats.Ping);
PrintStat("Queue Growth (msg/s)", summaryStats.QueueGrowthRate);
Console.WriteLine();
Console.WriteLine($" Storage: Data={summaryStats.DataMb.Max:F1}MB Storage={summaryStats.StorageMb.Max:F1}MB Indexes={summaryStats.IndexMb.Max:F1}MB");
Console.WriteLine($" Threshold breaches: DIRTY={summaryStats.DirtyCount} SLOW={summaryStats.SlowCount} ERR={summaryStats.ErrorCount} BACKLOG={summaryStats.BacklogCount}");
// Verdict
if (summaryStats.BacklogCount > 0)
{
var backlogPct = (double)summaryStats.BacklogCount / summaryStats.Count * 100;
Console.WriteLine();
Console.WriteLine($" ** ServiceControl could NOT keep up for {backlogPct:F0}% of the test ({summaryStats.BacklogCount}/{summaryStats.Count} samples)");
Console.WriteLine($" ** Peak queue growth: {summaryStats.QueueGrowthRate.Max:F1} msg/s faster than consumption");
}
else if (summaryStats.QueueDepth.Max > 100 && summaryStats.QueueDepth.Avg > 50)
{
Console.WriteLine();
Console.WriteLine($" ** ServiceControl kept up but queue depth was elevated (avg={summaryStats.QueueDepth.Avg:F0}, max={summaryStats.QueueDepth.Max:F0})");
}
else
{
Console.WriteLine();
Console.WriteLine($" ** ServiceControl kept up with the load");
}
}
Console.WriteLine();
} // finally
static void PrintStat(string name, MetricAccumulator m)
{
Console.WriteLine($" {name,-24} | {m.Avg,10:F1} | {m.Min,10:F1} | {m.Max,10:F1} | {m.P95,10:F1}");
}
static void PrintHeaders()
{
Console.WriteLine($"{"Time",-12} | {"Publish",8} | {"Deliver",8} | {"Ack",8} | {"Queue",8} | {"Mongo",10} | {"Stored",8} | {"Body",8} | {"BLag",8} | {"TTL Del",8} | {"TTL",4} | {"Dirty",8} | {"Used",8} | {"Ping",8} | {"Disk",8} | {"Trend",7} |");
Console.WriteLine($"{"",12} | {"msg/s",8} | {"msg/s",8} | {"msg/s",8} | {"depth",8} | {"docs",10} | {"msg/s",8} | {"msg/s",8} | {"docs",8} | {"del/s",8} | {"pass",4} | {"cache",8} | {"cache",8} | {"",8} | {"",8} | {"",7} |");
Console.WriteLine(new string('-', 180));
}
static double GetRate(JsonElement stats, string detailsProperty)
{
if (stats.TryGetProperty(detailsProperty, out var details) &&
details.TryGetProperty("rate", out var rate))
{
return rate.GetDouble();
}
return 0;
}
class MetricAccumulator
{
readonly List<double> values = [];
public double Min { get; private set; } = double.MaxValue;
public double Max { get; private set; } = double.MinValue;
double sum;
public void Add(double value)
{
values.Add(value);
sum += value;
if (value < Min) Min = value;
if (value > Max) Max = value;
}
public double Avg => values.Count > 0 ? sum / values.Count : 0;
public double P95
{
get
{
if (values.Count == 0) return 0;
var sorted = values.OrderBy(v => v).ToList();
var index = (int)Math.Ceiling(sorted.Count * 0.95) - 1;
return sorted[Math.Max(0, index)];
}
}
}
class SummaryStats
{
public int Count { get; private set; }
public MetricAccumulator Publish { get; } = new();
public MetricAccumulator Deliver { get; } = new();
public MetricAccumulator Ack { get; } = new();
public MetricAccumulator QueueDepth { get; } = new();
public MetricAccumulator MongoRate { get; } = new();
public MetricAccumulator BodyRate { get; } = new();
public MetricAccumulator BodyLag { get; } = new();
public MetricAccumulator TtlDel { get; } = new();
public MetricAccumulator Dirty { get; } = new();
public MetricAccumulator Used { get; } = new();
public MetricAccumulator Ping { get; } = new();
public MetricAccumulator DataMb { get; } = new();
public MetricAccumulator StorageMb { get; } = new();
public MetricAccumulator IndexMb { get; } = new();
public MetricAccumulator QueueGrowthRate { get; } = new();
public int DirtyCount { get; private set; }
public int SlowCount { get; private set; }
public int ErrorCount { get; private set; }
public int BacklogCount { get; private set; }
public void Record(double publish, double deliver, double ack, long queueDepth,
double mongoRate, double bodyRate, long bodyLag, double ttlDel, double dirty, double used, double ping,
double dataMb, double storageMb, double indexMb, double queueGrowthRate, string status)
{
Count++;
Publish.Add(publish);
Deliver.Add(deliver);
Ack.Add(ack);
QueueDepth.Add(queueDepth);
MongoRate.Add(mongoRate);
BodyRate.Add(bodyRate);
BodyLag.Add(bodyLag);
TtlDel.Add(ttlDel);
Dirty.Add(dirty);
Used.Add(used);
Ping.Add(ping);
DataMb.Add(dataMb);
StorageMb.Add(storageMb);
IndexMb.Add(indexMb);
QueueGrowthRate.Add(queueGrowthRate);
if (status == "DIRTY!") DirtyCount++;
if (status == "SLOW!") SlowCount++;
if (status == "ERR") ErrorCount++;
if (status == "BACKLOG") BacklogCount++;
}
}