-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
219 lines (194 loc) · 6.95 KB
/
MainWindow.xaml.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
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
using MQTTnet.Client;
using MQTTnet;
using System.Text;
using System.Windows;
using System.Timers;
using System.Collections.Concurrent;
using MQTTnet.Server;
using System.ComponentModel;
namespace MQTTStressTest
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged
{
public event PropertyChangedEventHandler? PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public MainWindow()
{
InitializeComponent();
DataContext = this;
testTimer = new System.Timers.Timer();
testTimer.Elapsed += TestTimerTick;
}
private readonly MqttFactory mqttFactory = new MqttFactory();
private IMqttClient? mqttClient;
private ConcurrentDictionary<string, long> testMsgs = new ConcurrentDictionary<string, long>();
private const string TEST_TOPIC = "test/t1";
private readonly System.Timers.Timer testTimer;
private int testCount = 0;
private long totalTime = 0;
private MQTTnet.Protocol.MqttQualityOfServiceLevel qos = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce;
bool connected = false;
public bool IsConnected { get { return connected; } }
public bool IsNotConnected { get { return !connected; } }
private void SetConnected(bool connected)
{
this.connected = connected;
OnPropertyChanged("IsConnected");
OnPropertyChanged("IsNotConnected");
}
bool running = false;
public bool IsRunning { get { return running; } }
public bool IsNotRunning { get { return !running; } }
private void SetRunning(bool running)
{
this.running = running;
OnPropertyChanged("IsRunning");
OnPropertyChanged("IsNotRunning");
}
private async void TestTimerTick(object? sender, ElapsedEventArgs e)
{
if (mqttClient != null)
{
testMsgs.TryAdd(testCount.ToString(), DateTime.Now.Ticks);
await mqttClient.PublishStringAsync(TEST_TOPIC, testCount.ToString(), qos);
R.Slog($"publish {testCount.ToString()} with qos: {(int)qos}");
testCount++;
}
}
private async Task MessageReceivedHandler(MqttApplicationMessageReceivedEventArgs e)
{
var topic = e.ApplicationMessage.Topic;
if (!TEST_TOPIC.Equals(topic)) {
return;
}
var segment = e.ApplicationMessage.PayloadSegment;
string payload = string.Empty;
if (segment.Array != null)
{
payload = Encoding.UTF8.GetString(segment.Array, segment.Offset, segment.Count);
}
if (testMsgs.TryRemove(payload, out long value)) {
var leftCount = testMsgs.Count;
var allCount = testCount;
var ms = (DateTime.Now.Ticks - value) / 10000;
totalTime += ms;
R.Slog($"已发送 {allCount} 未收到回复 {leftCount} 当次延时{ms}ms 平均延时 {totalTime / (allCount - leftCount)}ms");
}
}
private async Task DisconnectedHandler(MqttClientDisconnectedEventArgs args)
{
_ = Dispatcher.BeginInvoke(new Action(() =>
{
R.Slog("连接断开");
SetConnected(false);
testTimer.Stop();
SetRunning(false);
}));
}
private void btnDisconnect_Click(object sender, RoutedEventArgs e)
{
if (connected)
{
if (mqttClient != null)
{
mqttClient.TryDisconnectAsync();
}
}
else
{
R.ShowError("未连接,不需要断开");
return;
}
}
private async void btnConnect_Click(object sender, RoutedEventArgs e)
{
var address = txtAddress.Text;
if (string.IsNullOrEmpty(address)) {
R.ShowError("地址不能为空");
return;
}
if (connected)
{
R.ShowError($"不需要再次连接");
return;
}
if (mqttClient == null)
{
mqttClient = mqttFactory.CreateMqttClient();
mqttClient.ApplicationMessageReceivedAsync += MessageReceivedHandler;
mqttClient.DisconnectedAsync += DisconnectedHandler;
}
var mqttClientOptions = new MqttClientOptionsBuilder()
.WithTcpServer(address, 1883)
.WithClientId("StressTest.NET")
.WithCleanSession(false)
.Build();
try
{
var connectResult = await mqttClient.ConnectAsync(mqttClientOptions);
if (connectResult.ResultCode == MqttClientConnectResultCode.Success)
{
await mqttClient.SubscribeAsync(TEST_TOPIC);
R.Slog("连接成功");
SetConnected(true);
}
else
{
R.ShowError("连接失败");
}
}
catch
{
R.ShowError("连接失败");
}
}
private void btnStart_Click(object sender, RoutedEventArgs e)
{
int interval = 0;
if (!int.TryParse(txtInterval.Text, out interval))
{
R.ShowError("间隔时间输入错误");
return;
}
if (!connected) {
R.ShowError("未连接");
return;
}
switch (cbQos.SelectedIndex)
{
case 0:
qos = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce;
break;
case 1:
qos = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtLeastOnce;
break;
case 2:
qos = MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce;
break;
default:
break;
}
totalTime = 0;
testCount = 0;
testTimer.Interval = interval;
testTimer.Start();
SetRunning(true);
}
private void btnStop_Click(object sender, RoutedEventArgs e)
{
testTimer.Stop();
SetRunning(false);
}
private void ckLogOutput_Checked(object sender, RoutedEventArgs e)
{
var c = ckLogOutput.IsChecked ?? true;
R.IsSlog = c;
}
}
}