Skip to content

Commit

Permalink
Request reply baseline benchmark (#453)
Browse files Browse the repository at this point in the history
* Request reply benchmarks

* wip

* wip

* Revert experimental code

* Revert experimental code

* Revert MicroBenchmark project

* Simplified req-rep benchmark

* Check return value
  • Loading branch information
mtmk committed May 15, 2024
1 parent 0f0c45e commit 4386478
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions sandbox/MicroBenchmark/RequestReplyBench.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 4386478

Please sign in to comment.