Skip to content

Commit

Permalink
Added benchmark.
Browse files Browse the repository at this point in the history
  • Loading branch information
numinnex committed Jul 28, 2023
1 parent e12fd72 commit 890bd65
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 0 deletions.
14 changes: 14 additions & 0 deletions Benchmarks/Benchmarks.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Iggy_SDK\Iggy_SDK.csproj" />
</ItemGroup>

</Project>
115 changes: 115 additions & 0 deletions Benchmarks/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
using System.Diagnostics;
using System.Text;
using Iggy_SDK.Contracts.Http;
using Iggy_SDK.Enums;
using Iggy_SDK.Factory;
using Iggy_SDK.Messages;

const int messagesCount = 1000;
const int messagesBatch = 1000;
const int messageSize = 69;
const int producerCount = 7;
const int streamId = 14;
const int topicId = 1;
ulong totalMessages = messagesBatch * messagesCount;
ulong totalMessagesBytes = totalMessages * messageSize;


var bus = MessageStreamFactory.CreateMessageStream(options =>
{
options.BaseAdress = "127.0.0.1:8090";
options.Protocol = Protocol.Tcp;
options.ReceiveBufferSize = Int32.MaxValue;
options.SendBufferSize = Int32.MaxValue;
});

try
{
var stream = await bus.GetStreamByIdAsync(streamId);
}
catch
{
await bus.CreateStreamAsync(new StreamRequest
{
Name = "Test bench stream",
StreamId = streamId
});
await bus.CreateTopicAsync(streamId, new TopicRequest
{
Name = "Test bench topic",
PartitionsCount = 1,
TopicId = topicId
});
}

async Task SendMessages(int producerNumber)
{
List<TimeSpan> latencies = new();
long memoryBefore = GC.GetTotalMemory(true);

for (int i = 0; i < messagesBatch; i++)
{
var startTime = Stopwatch.GetTimestamp();
await bus.SendMessagesAsync(streamId, topicId, new MessageSendRequest
{
Key = new Key
{
Kind = KeyKind.PartitionId,
Length = 4,
Value = BitConverter.GetBytes(1),
},
Messages = CreateMessages(),
});
var diff = Stopwatch.GetElapsedTime(startTime);
latencies.Add(diff);
}

long memoryAfter = GC.GetTotalMemory(true);
long memoryUsed = memoryAfter - memoryBefore;
GC.Collect();

var totalLatencies = latencies.Sum(x => x.TotalSeconds);
var avgLatency = (totalLatencies * 1000) / (producerCount * latencies.Count);
var duration = totalLatencies / producerCount;

Console.WriteLine($"Total message bytes: {totalMessagesBytes}, average latency: {avgLatency} ms");
var avgThroughput = totalMessagesBytes / duration / 1024.0 / 1024.0;
Console.WriteLine(
$"Producer number: {producerNumber} send Messages: {messagesCount} in {messagesBatch} batches, with average throughput {avgThroughput} MB/s, allocated {memoryUsed} bytes of memory ");

}

Parallel.For(1, producerCount + 1, async iter =>
{
Console.WriteLine($"Executing producer number: {iter}");
await SendMessages(iter);
});
Console.ReadLine();

static List<Message> CreateMessages()
{
var messages = new List<Message>();
for (int i = 0; i < messagesCount; i++)
{
messages.Add(new Message
{
Id = Guid.NewGuid(),
Payload = CreatePayload(messageSize)
});
}

return messages;
}

static byte[] CreatePayload(uint size)
{
StringBuilder payloadBuilder = new StringBuilder((int)size);
for (uint i = 0; i < size; i++)
{
char character = (char)((i % 26) + 97);
payloadBuilder.Append(character);
}

string payloadString = payloadBuilder.ToString();
return Encoding.ASCII.GetBytes(payloadString);
}
6 changes: 6 additions & 0 deletions Iggy_SDK.sln
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Iggy_Sample_Consumer", "Igg
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Shared", "Shared\Shared.csproj", "{A9D91A9B-606D-469E-BAD9-B220A4AB31CA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Benchmarks", "Benchmarks\Benchmarks.csproj", "{466C54AD-A5FA-401C-9300-8FE21D3C355A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -36,5 +38,9 @@ Global
{A9D91A9B-606D-469E-BAD9-B220A4AB31CA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A9D91A9B-606D-469E-BAD9-B220A4AB31CA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A9D91A9B-606D-469E-BAD9-B220A4AB31CA}.Release|Any CPU.Build.0 = Release|Any CPU
{466C54AD-A5FA-401C-9300-8FE21D3C355A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{466C54AD-A5FA-401C-9300-8FE21D3C355A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{466C54AD-A5FA-401C-9300-8FE21D3C355A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{466C54AD-A5FA-401C-9300-8FE21D3C355A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

0 comments on commit 890bd65

Please sign in to comment.