-
Notifications
You must be signed in to change notification settings - Fork 315
/
SocketTests.cs
141 lines (113 loc) · 4.23 KB
/
SocketTests.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.Diagnostics;
using System.Net;
using System.Threading;
using FluentAssertions;
using FluentAssertions.Extensions;
using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities;
using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.Interfaces;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Microsoft.TestPlatform.PerformanceTests;
[TestClass]
[Ignore("The timing can vary significantly based on the system running the test. Convert them to report the results and not fail.")]
public class SocketTests
{
[TestMethod]
public void SocketThroughput2()
{
// Measure the throughput with socket communication v2 (SocketServer, SocketClient)
// implementation.
var server = new SocketServer();
var client = new SocketClient();
ICommunicationChannel? serverChannel = null;
ICommunicationChannel? clientChannel = null;
ManualResetEventSlim dataTransferred = new(false);
ManualResetEventSlim clientConnected = new(false);
ManualResetEventSlim serverConnected = new(false);
int dataReceived = 0;
var watch = new Stopwatch();
var thread = new Thread(() => SendData(clientChannel, watch));
// Setup server
server.Connected += (sender, args) =>
{
serverChannel = args.Channel;
serverChannel.MessageReceived += (channel, messageReceived) =>
{
// Keep count of bytes
dataReceived += messageReceived.Data.Length;
if (dataReceived >= 65536 * 20000)
{
dataTransferred.Set();
watch.Stop();
}
};
clientConnected.Set();
};
client.Connected += (sender, args) =>
{
clientChannel = args.Channel;
thread.Start();
serverConnected.Set();
};
var port = server.Start(IPAddress.Loopback.ToString() + ":0");
client.Start(port);
clientConnected.Wait();
serverConnected.Wait();
thread.Join();
dataTransferred.Wait();
watch.Elapsed.Should().BeLessOrEqualTo(15.Seconds());
}
[TestMethod]
public void SocketThroughput1()
{
// Measure the throughput with socket communication v1 (SocketCommunicationManager)
// implementation.
var server = new SocketCommunicationManager();
var client = new SocketCommunicationManager();
var watch = new Stopwatch();
int port = server.HostServer(new IPEndPoint(IPAddress.Loopback, 0)).Port;
client.SetupClientAsync(new IPEndPoint(IPAddress.Loopback, port)).Wait();
server.AcceptClientAsync().Wait();
server.WaitForClientConnection(1000);
client.WaitForServerConnection(1000);
var clientThread = new Thread(() => SendData2(client, watch));
clientThread.Start();
var dataReceived = 0;
while (dataReceived < 65536 * 20000)
{
dataReceived += server.ReceiveRawMessage().Length;
}
watch.Stop();
clientThread.Join();
watch.Elapsed.Should().BeLessOrEqualTo(20.Seconds());
}
private static void SendData(ICommunicationChannel? channel, Stopwatch watch)
{
var dataBytes = new byte[65536];
for (int i = 0; i < dataBytes.Length; i++)
{
dataBytes[i] = 0x65;
}
var dataBytesStr = System.Text.Encoding.UTF8.GetString(dataBytes);
watch.Start();
for (int i = 0; i < 20000; i++)
{
channel!.Send(dataBytesStr);
}
}
private static void SendData2(ICommunicationManager communicationManager, Stopwatch watch)
{
var dataBytes = new byte[65536];
for (int i = 0; i < dataBytes.Length; i++)
{
dataBytes[i] = 0x65;
}
var dataBytesStr = System.Text.Encoding.UTF8.GetString(dataBytes);
watch.Start();
for (int i = 0; i < 20000; i++)
{
communicationManager.SendRawMessage(dataBytesStr);
}
}
}