Skip to content

Commit

Permalink
MicroBenchmark for PublishParallel (#367)
Browse files Browse the repository at this point in the history
* MicroBenchmark for PublishParallel

Signed-off-by: Caleb Lloyd <[email protected]>

* increase to 1M msgs to reduce error

Signed-off-by: Caleb Lloyd <[email protected]>

---------

Signed-off-by: Caleb Lloyd <[email protected]>
  • Loading branch information
caleblloyd authored Jan 31, 2024
1 parent 9b049d6 commit 70f37ef
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 4 deletions.
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();
}
}

0 comments on commit 70f37ef

Please sign in to comment.