Files
natsdotnet/tests/NATS.Server.Benchmark.Tests/RequestReply/SingleClientLatencyTests.cs
Joseph Doherty 1d4b87e5f9 docs: refresh benchmark comparison with increased message counts
Increase message counts across all 14 benchmark test files to reduce
run-to-run variance (e.g. PubSub 16B: 10K→50K, FanOut: 10K→15K,
SinglePub: 100K→500K, JS tests: 5K→25K). Rewrite benchmarks_comparison.md
with fresh numbers from two-batch runs. Key changes: multi 4x4 reached
parity (1.01x), fan-out improved to 0.84x, TLS pub/sub shows 4.70x .NET
advantage, previous small-count anomalies corrected.
2026-03-13 17:52:03 -04:00

65 lines
2.2 KiB
C#

using NATS.Client.Core;
using NATS.Server.Benchmark.Tests.Harness;
using NATS.Server.Benchmark.Tests.Infrastructure;
using Xunit.Abstractions;
namespace NATS.Server.Benchmark.Tests.RequestReply;
[Collection("Benchmark-Core")]
public class SingleClientLatencyTests(CoreServerPairFixture fixture, ITestOutputHelper output)
{
private readonly BenchmarkRunner _runner = new() { WarmupCount = 1_000, MeasurementCount = 50_000 };
[Fact]
[Trait("Category", "Benchmark")]
public async Task RequestReply_SingleClient_128B()
{
const int payloadSize = 128;
const string subject = "bench.reqrep.single";
var dotnetResult = await RunLatency("Request-Reply Single (128B)", "DotNet", subject, payloadSize, fixture.CreateDotNetClient);
if (fixture.GoAvailable)
{
var goResult = await RunLatency("Request-Reply Single (128B)", "Go", subject, payloadSize, fixture.CreateGoClient);
BenchmarkResultWriter.WriteComparison(output, goResult, dotnetResult);
}
else
{
BenchmarkResultWriter.WriteSingle(output, dotnetResult);
}
}
private async Task<BenchmarkResult> RunLatency(string name, string serverType, string subject, int payloadSize, Func<NatsConnection> createClient)
{
var payload = new byte[payloadSize];
await using var serviceClient = createClient();
await using var requestClient = createClient();
await serviceClient.ConnectAsync();
await requestClient.ConnectAsync();
// Start service responder
var sub = await serviceClient.SubscribeCoreAsync<byte[]>(subject);
var responderTask = Task.Run(async () =>
{
await foreach (var msg in sub.Msgs.ReadAllAsync())
{
if (msg.ReplyTo is not null)
await serviceClient.PublishAsync(msg.ReplyTo, payload);
}
});
await Task.Delay(50);
var result = await _runner.MeasureLatencyAsync(name, serverType, payloadSize,
async _ =>
{
await requestClient.RequestAsync<byte[], byte[]>(subject, payload);
});
await sub.UnsubscribeAsync();
return result;
}
}