Skip to content
This repository has been archived by the owner on Jun 16, 2022. It is now read-only.

Commit

Permalink
DtronixMessageQueue.Tests.Performance back to the 1.0 version.
Browse files Browse the repository at this point in the history
Updated code for the new standards.
Simplified throughput test.
  • Loading branch information
DJGosnell committed Jul 16, 2017
1 parent 0303fe8 commit 2b54168
Show file tree
Hide file tree
Showing 26 changed files with 608 additions and 937 deletions.
7 changes: 3 additions & 4 deletions src/DtronixMessageQueue.Tests.Performance/App.config
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>

<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
Expand Down
127 changes: 60 additions & 67 deletions src/DtronixMessageQueue.Tests.Performance/ConsoleCopy.cs
Original file line number Diff line number Diff line change
@@ -1,81 +1,74 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DtronixMessageQueue.Tests.Performance
{
class ConsoleCopy : IDisposable
{
FileStream _fileStream;
StreamWriter _fileWriter;
TextWriter _doubleWriter;
TextWriter _oldOut;
namespace DtronixMessageQueue.Tests.Performance {
class ConsoleCopy : IDisposable {

class DoubleWriter : TextWriter
{
TextWriter _one;
TextWriter _two;
FileStream fileStream;
StreamWriter fileWriter;
TextWriter doubleWriter;
TextWriter oldOut;

public DoubleWriter(TextWriter one, TextWriter two)
{
_one = one;
_two = two;
}
class DoubleWriter : TextWriter {

public override Encoding Encoding
{
get { return _one.Encoding; }
}
TextWriter one;
TextWriter two;

public override void Flush()
{
_one.Flush();
_two.Flush();
}
public DoubleWriter(TextWriter one, TextWriter two) {
this.one = one;
this.two = two;
}

public override void Write(char value)
{
_one.Write(value);
_two.Write(value);
}
}
public override Encoding Encoding {
get { return one.Encoding; }
}

public ConsoleCopy(string path)
{
_oldOut = Console.Out;
public override void Flush() {
one.Flush();
two.Flush();
}

try
{
_fileStream = File.Create(path);
public override void Write(char value) {
one.Write(value);
two.Write(value);
}

_fileWriter = new StreamWriter(_fileStream);
_fileWriter.AutoFlush = true;
}

_doubleWriter = new DoubleWriter(_fileWriter, _oldOut);
}
catch (Exception e)
{
Console.WriteLine("Cannot open file for writing");
Console.WriteLine(e.Message);
return;
}
Console.SetOut(_doubleWriter);
}
public ConsoleCopy(string path) {
oldOut = Console.Out;

public void Dispose()
{
Console.SetOut(_oldOut);
if (_fileWriter != null)
{
_fileWriter.Flush();
_fileWriter.Close();
_fileWriter = null;
}
if (_fileStream != null)
{
_fileStream.Close();
_fileStream = null;
}
}
}
}
try {
fileStream = File.Create(path);

fileWriter = new StreamWriter(fileStream);
fileWriter.AutoFlush = true;

doubleWriter = new DoubleWriter(fileWriter, oldOut);
} catch (Exception e) {
Console.WriteLine("Cannot open file for writing");
Console.WriteLine(e.Message);
return;
}
Console.SetOut(doubleWriter);
}

public void Dispose() {
Console.SetOut(oldOut);
if (fileWriter != null) {
fileWriter.Flush();
fileWriter.Close();
fileWriter = null;
}
if (fileStream != null) {
fileStream.Close();
fileStream = null;
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Nuget|AnyCPU'">
<OutputPath>bin\Nuget\</OutputPath>
</PropertyGroup>
<PropertyGroup>
<StartupObject />
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Configuration" />
Expand All @@ -51,21 +48,16 @@
</ItemGroup>
<ItemGroup>
<Compile Include="ConsoleCopy.cs" />
<Compile Include="MqThroughputTest.cs" />
<Compile Include="MqPerformanceTest.cs" />
<Compile Include="PerformanceTestBase.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RpcPerformanceTest.cs" />
<Compile Include="ServerMessageType.cs" />
<Compile Include="ServerMqPerformanceTests.cs" />
<Compile Include="Services\Server\CalculatorService.cs" />
<Compile Include="Services\Server\ICalculatorService.cs" />
<Compile Include="Services\Server\TestService.cs" />
<Compile Include="TestSessions\MqBaseTestSession.cs" />
<Compile Include="TestSessions\MqThroughputTestSession.cs" />
<Compile Include="SimpleMqSession.cs" />
<Compile Include="SimpleRpcSession.cs" />
<Compile Include="TestMode.cs" />
<Compile Include="TestStartArgs.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
Expand All @@ -75,9 +67,7 @@
<Content Include="run-mq-multi-client.bat">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="run-mq-server.bat">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DtronixMessageQueue\DtronixMessageQueue.csproj">
Expand All @@ -86,7 +76,7 @@
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Content Include="run-mq-client.bat">
<Content Include="run-mq.bat">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
Expand Down
176 changes: 176 additions & 0 deletions src/DtronixMessageQueue.Tests.Performance/MqPerformanceTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Management;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace DtronixMessageQueue.Tests.Performance {
class MqPerformanceTest : PerformanceTestBase {

private readonly MqConfig _config;
private readonly MqMessage _smallMessage;
private readonly MqMessage _medimumMessage;
private readonly MqMessage _largeMessage;
private Stopwatch _sw;
private Semaphore _loopSemaphore;
private int count;
private double[] totalValues;
private Semaphore _testCompleteSemaphore;
private MqServer<SimpleMqSession, MqConfig> _server;
private MqClient<SimpleMqSession, MqConfig> _client;

public MqPerformanceTest()
{

_config = new MqConfig
{
Ip = "127.0.0.1",
Port = 2828
};

_smallMessage = new MqMessage {
new MqFrame(SequentialBytes(50), MqFrameType.More, _config),
new MqFrame(SequentialBytes(50), MqFrameType.More, _config),
new MqFrame(SequentialBytes(50), MqFrameType.More, _config),
new MqFrame(SequentialBytes(50), MqFrameType.Last, _config)
};

_medimumMessage = new MqMessage {
new MqFrame(SequentialBytes(500), MqFrameType.More, _config),
new MqFrame(SequentialBytes(500), MqFrameType.More, _config),
new MqFrame(SequentialBytes(500), MqFrameType.More, _config),
new MqFrame(SequentialBytes(500), MqFrameType.Last, _config)
};

_largeMessage = new MqMessage();

for (int i = 0; i < 20; i++)
{
_largeMessage.Add(new MqFrame(SequentialBytes(3000), MqFrameType.More, _config));
}

_sw = new Stopwatch();
_loopSemaphore = new Semaphore(0, 1);
_testCompleteSemaphore = new Semaphore(0, 1);

count = 0;
}


public override void StartTest() {

Console.WriteLine("FrameBufferSize: {0}; SendAndReceiveBufferSize: {1}\r\n", _config.FrameBufferSize,
_config.SendAndReceiveBufferSize);


MqInProcessPerformanceTests(1000000, 5, _smallMessage, _config);


MqInProcessPerformanceTests(100000, 5, _medimumMessage, _config);


MqInProcessPerformanceTests(10000, 5, _largeMessage, _config);

Console.WriteLine("Performance complete");
}

private void MqInProcessPerformanceTests(int totalMessages, int loops, MqMessage message, MqConfig config) {
_server = new MqServer<SimpleMqSession, MqConfig>(config);
_server.Start();

totalValues = new [] {0d, 0d, 0d};


_client = new MqClient<SimpleMqSession, MqConfig>(config);

Console.WriteLine("| Build | Messages | Msg Bytes | Milliseconds | Msg/sec | MBps |");
Console.WriteLine("|---------|------------|-----------|--------------|------------|----------|");



_server.IncomingMessage += (sender, args2) => {
count += args2.Messages.Count;


if (count == totalMessages)
{
ReportResults(totalMessages, _sw.ElapsedMilliseconds, message.Size);
_loopSemaphore.Release();

}
};



_client.Connected += (sender, args) => {
for (var i = 0; i < loops; i++)
{
SendMessages(args.Session, message, totalMessages, 5000);
}

Console.WriteLine("| | | AVERAGES | {0,12:N0} | {1,10:N0} | {2,8:N2} |", totalValues[0]/loops,
totalValues[1]/loops, totalValues[2]/loops);
Console.WriteLine();

_server.Stop();
_client.Close();

_testCompleteSemaphore.Release();

};

_client.Connect();

_testCompleteSemaphore.WaitOne(30000);


}

public void SendMessages(SimpleMqSession client, MqMessage message, int totalMessages, int timeout)
{

count = 0;
_sw.Restart();

for (var i = 0; i < totalMessages; i++)
{
client.Send(message);
}

if (!_loopSemaphore.WaitOne(timeout))
{
Console.WriteLine($"Test failed to complete with {count} of {totalMessages} loops performed.");

ReportResults(totalMessages, _sw.ElapsedMilliseconds, message.Size);
}



}

private void ReportResults(int totalMessages, long milliseconds, int messageSize)
{
var mode = "Release";

#if DEBUG
mode = "Debug";
#endif

var messagesPerSecond = (int)((double)totalMessages / milliseconds * 1000);
var msgSizeNoHeader = messageSize - 12;
var mbps = totalMessages * (double)(msgSizeNoHeader) / milliseconds / 1000;
Console.WriteLine("| {0,7} | {1,10:N0} | {2,9:N0} | {3,12:N0} | {4,10:N0} | {5,8:N2} |", mode, totalMessages,
msgSizeNoHeader, milliseconds, messagesPerSecond, mbps);
totalValues[0] += milliseconds;
totalValues[1] += messagesPerSecond;
totalValues[2] += mbps;
}
}

}
Loading

0 comments on commit 2b54168

Please sign in to comment.