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

Request reply baseline benchmark #453

Merged
merged 9 commits into from May 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 48 additions & 0 deletions sandbox/MicroBenchmark/RequestReplyBench.cs
@@ -0,0 +1,48 @@
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 RequestReplyBench
{
private NatsConnection _nats;
private CancellationTokenSource _cts;
private Task _subscription;

[GlobalSetup]
public async Task SetupAsync()
{
_nats = new NatsConnection();
await _nats.ConnectAsync();
_cts = new CancellationTokenSource();
_subscription = Task.Run(async () =>
{
await foreach (var msg in _nats.SubscribeAsync<int>("req_rep_bench", cancellationToken: _cts.Token))
{
await msg.ReplyAsync(0xBEEF);
}
});
}

[GlobalCleanup]
public async Task CleanupAsync()
{
await _cts.CancelAsync();
await _subscription;
await _nats.DisposeAsync();
}

[Benchmark]
public async Task<int> RequestReplyAsync()
{
var reply = await _nats.RequestAsync<int, int>("req_rep_bench", 0xDEAD);
var result = reply.Data;
ArgumentOutOfRangeException.ThrowIfNotEqual(0xBEEF, result);
return result;
}
}