Skip to content

Commit

Permalink
added perf test
Browse files Browse the repository at this point in the history
  • Loading branch information
kerryjiang committed Dec 8, 2024
1 parent 836ee52 commit f4d97ba
Showing 1 changed file with 86 additions and 0 deletions.
86 changes: 86 additions & 0 deletions test/SuperSocket.Tests/PerfTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using System;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
using SuperSocket.ProtoBase;
using SuperSocket.Server;
using SuperSocket.Server.Host;
using Xunit;
using Xunit.Abstractions;

namespace SuperSocket.Tests
{
public class PerfTest : TestClassBase
{
public PerfTest(ITestOutputHelper outputHelper)
: base(outputHelper)
{
}

[Theory]
[InlineData(typeof(RegularHostConfigurator))]
[InlineData(typeof(SecureHostConfigurator))]
[InlineData(typeof(KestralConnectionHostConfigurator))]
public async Task ConcurrentSendReceive(Type hostConfiguratorType)
{
var hostConfigurator = CreateObject<IHostConfigurator>(hostConfiguratorType);
var connectionCount = 10;
var runTime = TimeSpan.FromMinutes(1);

using (var server = CreateSocketServerBuilder<TextPackageInfo, LinePipelineFilter>(hostConfigurator)
.UsePackageHandler(async (s, p) =>
{
await s.SendAsync(Utf8Encoding.GetBytes(p.Text + "\r\n"));
}).BuildAsServer())
{
Assert.Equal("TestServer", server.Name);

Assert.True(await server.StartAsync());
OutputHelper.WriteLine("Started.");

using var cancellationTokenSource = new CancellationTokenSource();

var tasks = Enumerable
.Range(0, connectionCount)
.Select(_ => RunConnectionAsync(hostConfigurator, cancellationTokenSource.Token))
.ToArray();

cancellationTokenSource.CancelAfter(runTime);

await Task.Delay(runTime);

var rounds = await Task.WhenAll(tasks).WaitAsync(TimeSpan.FromSeconds(10));

OutputHelper.WriteLine($"Total rounds: {rounds.Sum()}.");
}
}

private async Task<int> RunConnectionAsync(IHostConfigurator hostConfigurator, CancellationToken cancellationToken)
{
var round = 0;

using (var socket = hostConfigurator.CreateClient())
using (var clientStream = await hostConfigurator.GetClientStream(socket))
using (var streamReader = new StreamReader(clientStream, Utf8Encoding, true))
using (var streamWriter = new StreamWriter(clientStream, Utf8Encoding, 1024 * 1024 * 4))
{
while (!cancellationToken.IsCancellationRequested)
{
var text = Guid.NewGuid().ToString();

await streamWriter.WriteAsync($"{text}\r\n".AsMemory());
await streamWriter.FlushAsync();

var line = await streamReader.ReadLineAsync();
Assert.Equal(text, line);

round++;
}
}

return round;
}
}
}

0 comments on commit f4d97ba

Please sign in to comment.