-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Request reply baseline benchmark (#453)
* Request reply benchmarks * wip * wip * Revert experimental code * Revert experimental code * Revert MicroBenchmark project * Simplified req-rep benchmark * Check return value
- Loading branch information
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |