Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MicroBenchmark for PublishParallel #367

Merged
merged 2 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions sandbox/MicroBenchmark/PublishParallelBench.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
using System.Text.Json.Serialization;
using BenchmarkDotNet.Attributes;
using NATS.Client.Core;

#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.

namespace MicroBenchmark;

[MemoryDiagnoser]
[ShortRunJob]
[PlainExporter]
public class PublishParallelBench
{
public const int TotalMsgs = 1_000_000;

private static readonly PublishParallelObj Data = new PublishParallelObj
{
String = new('a', 32),
Bool = true,
Int = 42,
Dictionary = new Dictionary<string, string>
{
{ new('a', 32), new('a', 32) },
{ new('b', 32), new('b', 32) },
{ new('c', 32), new('c', 32) },
},
List = new List<string>
{
new('a', 32),
new('b', 32),
new('c', 32),
},
};

private NatsConnection _nats;

[Params(1, 2, 4)]
public int Concurrency { get; set; }

[GlobalSetup]
public async Task Setup()
{
var registry = new NatsJsonContextSerializerRegistry(PublishParallelJsonContext.Default);
_nats = new NatsConnection(NatsOpts.Default with { SerializerRegistry = registry });
await _nats.ConnectAsync();
}

[Benchmark]
public async Task PublishParallelAsync()
{
var tasks = new List<Task>();
for (var i = 0; i < Concurrency; i++)
{
tasks.Add(Task.Run(async () =>
{
for (var j = 0; j < TotalMsgs / Concurrency; j++)
{
await _nats.PublishAsync("test", Data);
}
}));
}

await Task.WhenAll(tasks);
await _nats.PingAsync();
}
}

internal record PublishParallelObj
{
[JsonPropertyName("string")]
public string String { get; set; }

[JsonPropertyName("bool")]
public bool Bool { get; set; }

[JsonPropertyName("int")]
public int Int { get; set; }

[JsonPropertyName("dictionary")]
public Dictionary<string, string> Dictionary { get; set; }

[JsonPropertyName("list")]
public List<string> List { get; set; }
}

[JsonSerializable(typeof(PublishParallelObj))]
internal partial class PublishParallelJsonContext : JsonSerializerContext
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace MicroBenchmark;
[MemoryDiagnoser]
[ShortRunJob]
[PlainExporter]
public class SerializationBuffersBench
public class PublishSerialBench
{
private static readonly string Data = new('0', 126);

Expand All @@ -17,16 +17,20 @@ public class SerializationBuffersBench
public int Iter { get; set; }

[GlobalSetup]
public void Setup() => _nats = new NatsConnection();
public async Task SetupAsync()
{
_nats = new NatsConnection();
await _nats.ConnectAsync();
}

[Benchmark]
public async ValueTask<TimeSpan> PublishAsync()
public async Task PublishAsync()
{
for (var i = 0; i < Iter; i++)
{
await _nats.PublishAsync("foo", Data);
}

return await _nats.PingAsync();
await _nats.PingAsync();
}
}
Loading