-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPerformanceApp.cs
177 lines (159 loc) · 8.31 KB
/
PerformanceApp.cs
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
//---------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES
// OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
//---------------------------------------------------------------------------------
using Azure;
using Azure.Messaging.ServiceBus;
using Azure.Messaging.ServiceBus.Administration;
namespace ThroughputTest
{
using LinqStatistics;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reactive.Linq;
using System.Threading;
using System.Threading.Tasks;
sealed class ServiceBusPerformanceApp
{
readonly Settings settings;
readonly Metrics metrics;
readonly CancellationTokenSource cancellationTokenSource;
readonly List<PerformanceTask> tasks;
private IDisposable sendMetrics;
private IDisposable receiveMetrics;
public ServiceBusPerformanceApp(Settings settings, Metrics metrics)
{
this.settings = settings;
this.metrics = metrics;
this.cancellationTokenSource = new CancellationTokenSource();
this.tasks = new List<PerformanceTask>();
}
public async Task Run(params Experiment[] experiments)
{
this.settings.PrintSettings();
await CreateTopic();
tasks.Add(new ReceiverTask(settings, this.metrics, this.cancellationTokenSource.Token));
tasks.Add(new SenderTask(settings, this.metrics, this.cancellationTokenSource.Token));
Console.WriteLine("Starting...");
Console.WriteLine();
long sendTotal = 0, receiveTotal = 0;
int windowLengthSecs = (int)this.settings.MetricsDisplayFrequency*2;
if (this.settings.SenderCount > 0)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write("S|{0,10:0.00}|{1,10:0.00}|{2,5}|{3,5}|", "pstart", "pend", "sbc", "mifs");
Console.Write("{0,10:0.00}|{1,10:0.00}|{2,10:0.00}|{3,10:0.00}|{4,10:0.00}|", "snd.avg", "snd.med", "snd.dev", "snd.min", "snd.max");
Console.Write("{0,10:0.00}|{1,10:0.00}|{2,10:0.00}|{3,10:0.00}|{4,10:0.00}|", "gld.avg", "gld.med", "gld.dev", "gld.min", "gld.max");
Console.WriteLine("{0,10}|{1,10}|{2,10}|{3,10}|{4,10}|{5,10}|", "msg/s", "total", "sndop", "errs", "busy", "overall");
this.sendMetrics = ((IObservable<SendMetrics>)metrics)
.Buffer(TimeSpan.FromSeconds(windowLengthSecs), TimeSpan.FromSeconds(windowLengthSecs/2))
.Subscribe((list) =>
{
if (list.Count == 0)
return;
lock (Console.Out)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write("S|{0,10}|{1,10}|{2,5}|{3,5}|", list.First().Tick / Stopwatch.Frequency+1, list.Last().Tick / Stopwatch.Frequency+1, this.settings.SendBatchCount, this.settings.MaxInflightSends.Value);
WriteStat(list, i => i.SendDuration100ns, Stopwatch.Frequency / 1000.0);
WriteStat(list, i => i.GateLockDuration100ns, Stopwatch.Frequency / 10000.0);
var msgs = list.Sum(i => i.Messages);
sendTotal += msgs;
Console.WriteLine("{0,10:0.00}|{1,10}|{2,10}|{3,10}|{4,10}|{5,10}|", list.Sum(i => i.Messages) / (double)windowLengthSecs, msgs, list.Sum(i => i.Sends), list.Sum(i => i.Errors), list.Sum(i => i.BusyErrors), sendTotal);
}
});
}
if (this.settings.ReceiverCount > 0)
{
Console.ForegroundColor = ConsoleColor.Cyan;
Console.Write("R|{0,10:0.00}|{1,10:0.00}|{2,5}|{3,5}|", "pstart", "pend", "rbc", "mifr");
Console.Write("{0,10:0.00}|{1,10:0.00}|{2,10:0.00}|{3,10:0.00}|{4,10:0.00}|", "rcv.avg", "rcv.med", "rcv.dev", "rcv.min", "rcv.max");
Console.Write("{0,10:0.00}|{1,10:0.00}|{2,10:0.00}|{3,10:0.00}|{4,10:0.00}|", "cpl.avg", "cpl.med", "cpl.dev", "cpl.min", "cpl.max");
Console.WriteLine("{0,10}|{1,10}|{2,10}|{3,10}|{4,10}|{5,10}|", "msg/s", "total", "rcvop", "errs", "busy", "overall");
this.receiveMetrics = ((IObservable<ReceiveMetrics>)metrics)
.Buffer(TimeSpan.FromSeconds(windowLengthSecs), TimeSpan.FromSeconds(windowLengthSecs/2))
.Subscribe((list) =>
{
if (list.Count == 0)
return;
lock (Console.Out)
{
Console.ForegroundColor = ConsoleColor.Cyan;
Console.Write("R|{0,10}|{1,10}|{2,5}|{3,5}|", list.First().Tick / Stopwatch.Frequency+1, list.Last().Tick / Stopwatch.Frequency+1, this.settings.ReceiveBatchCount, this.settings.MaxInflightReceives.Value);
WriteStat(list, i => i.ReceiveDuration100ns, Stopwatch.Frequency / 1000.0);
WriteStat(list, i => i.CompleteDuration100ns, Stopwatch.Frequency / 1000.0);
var msgs = list.Sum(i => i.Messages);
receiveTotal += msgs;
Console.WriteLine("{0,10:0.00}|{1,10}|{2,10}|{3,10}|{4,10}|{5,10}|", list.Sum(i => i.Messages) / (double)windowLengthSecs, msgs, list.Sum(i => i.Receives), list.Sum(i => i.Errors), list.Sum(i => i.BusyErrors), receiveTotal);
}
});
}
Task runTasks = tasks.ParallelForEachAsync((t) => t.StartAsync());
if (experiments != null && experiments.Length > 0)
{
var experimentTask = RunExperiments(experiments, this.cancellationTokenSource.Token);
await Task.WhenAll(new Task[] { experimentTask, runTasks });
}
else
{
await runTasks;
}
this.cancellationTokenSource.Cancel();
this.tasks.ForEach((t) => t.Close());
Console.ForegroundColor = ConsoleColor.White;
}
private async Task CreateTopic()
{
var client = new ServiceBusAdministrationClient(settings.ConnectionString);
var topicFactory = new Func<Task>(
() => client.CreateTopicAsync(
new CreateTopicOptions(settings.SendPath)
{
EnableBatchedOperations = !settings.DisableBatchStoreAccess
}));
try
{
await topicFactory();
}
catch (ServiceBusException e) when (
e.Reason == ServiceBusFailureReason.MessagingEntityAlreadyExists)
{
await client.DeleteTopicAsync(settings.SendPath);
await topicFactory();
}
}
private Task RunExperiments(IEnumerable<Experiment> experiments, CancellationToken ct)
{
return Task.Run(async () =>
{
foreach (var experiment in experiments)
{
await Task.Delay(TimeSpan.FromSeconds(10)).ContinueWith((t) => StartExperiment(experiment)).ConfigureAwait(false);
}
});
}
private Task StartExperiment(Experiment experiment)
{
return experiment.Run().ContinueWith(t =>
{
if (t.Result != null)
{
return Task.Delay(TimeSpan.FromSeconds(10)).ContinueWith(t1 => StartExperiment(t.Result));
}
return Task.CompletedTask;
});
}
void WriteStat<T>(IList<T> list, Func<T, long> f, double scale)
{
if (list.Count > 1)
{
Console.Write("{0,10:0.00}|{1,10:0.00}|{2,10:0.00}|{3,10:0.00}|{4,10:0.00}|", list.Average(f) / scale, list.Median(f) / scale, list.StandardDeviationP(f) / scale, list.Min(f) / scale, list.Max(f) / scale);
}
}
}
}