Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
</ItemGroup>

<ItemGroup Label="Transports">
<PackageVersion Include="Dekaf" Version="1.27.0" />
<PackageVersion Include="Dekaf.SchemaRegistry" Version="1.27.0" />
<PackageVersion Include="Dekaf" Version="1.28.0" />
<PackageVersion Include="Dekaf.SchemaRegistry" Version="1.28.0" />
<PackageVersion Include="Azure.Messaging.ServiceBus" Version="7.21.0" />
<PackageVersion Include="AWSSDK.SQS" Version="4.1.0" />
<PackageVersion Include="AWSSDK.SimpleNotificationService" Version="4.1.0" />
Expand All @@ -57,7 +57,7 @@
</ItemGroup>

<ItemGroup Label="Serialization">
<PackageVersion Include="Dekaf.SchemaRegistry.Avro" Version="1.27.0" />
<PackageVersion Include="Dekaf.SchemaRegistry.Avro" Version="1.28.0" />
<PackageVersion Include="MessagePack" Version="3.1.12" />
<PackageVersion Include="Google.Protobuf" Version="3.36.2" />
</ItemGroup>
Expand Down
14 changes: 0 additions & 14 deletions benchmarks/Relayer.Benchmarks/ConsumerCircuitBenchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,11 @@ namespace Relayer.Benchmarks;
public class ConsumerCircuitBenchmarks
{
private ConsumerCircuit _circuit = null!;
private ConsumerCircuitDelivery _delivery = null!;
private RelayerInstrumentation _instruments = null!;

[GlobalSetup]
public void Setup()
{
_circuit = new ConsumerCircuit(.5, 10, TimeSpan.FromMinutes(1), TimeSpan.FromSeconds(5), TimeProvider.System);
_instruments = new();
_delivery = new(new(), _instruments.CreateEndpoint(new() { System = "kafka", Destination = "benchmark" }),
static () => { }, TimeProvider.System);
}

[Benchmark]
Expand All @@ -27,18 +22,9 @@ public bool ClosedOutcome()
return _circuit.Complete(probe, false);
}

[Benchmark]
public bool SerialDeliveryOutcome()
{
_delivery.TryEnter();
return _delivery.Complete(false);
}

[GlobalCleanup]
public void Cleanup()
{
_delivery.Dispose();
_instruments.Dispose();
_circuit.Dispose();
}
}
96 changes: 52 additions & 44 deletions benchmarks/Relayer.Benchmarks/KafkaReceiveBenchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@

namespace Relayer.Benchmarks;

/// <summary>Broker-backed native fetch batches, default JSON and completed-offset commits, versus raw Dekaf.</summary>
/// <remarks>Each invocation replays one fixed 4096-record workload. Iteration setup seeks outside the measurement.</remarks>
/// <summary>Broker-backed Dekaf partitioned runtime, default JSON and runtime-managed commits, versus the same runtime with a raw handler.</summary>
/// <remarks>Each invocation replays one fixed 4096-record workload and stops the run once every record completes,
/// including the stop commit. Iteration setup seeks outside the measurement.</remarks>
[MemoryDiagnoser]
public class KafkaReceiveBenchmarks
{
Expand All @@ -23,17 +24,17 @@ public class KafkaReceiveBenchmarks
private IKafkaConsumer<byte[], byte[]> _raw = null!;
private IKafkaConsumer<byte[], byte[]> _native = null!;
private KafkaConsumer _adapter = null!;
private KafkaWorkerHealth? _health;
private ServiceProvider _provider = null!;
private readonly TopicPartitionOffset[] _offset = new TopicPartitionOffset[1];
private MessageTracing _tracing = null!;
private ReceivePipeline<KafkaDelivery, KafkaMessageBinding> _receive = null!;
private PartitionedProcessingOptions _rawOptions = null!;
private PartitionRecordProcessor<byte[], byte[]> _rawProcessor = null!;
private CancellationTokenSource _run = null!;
private int _count;
private string _topic = null!;

[Params(false, true)]
public bool HealthEnabled { get; set; }

// Exercise native checkpoint ownership with both small and large poll windows;
// the adapter must not add a second assignment lookup for every record.
[Params(32, 500)]
public int MaxPollRecords { get; set; }

Expand Down Expand Up @@ -71,7 +72,7 @@ public async Task Setup()
});
});
_provider = services.BuildServiceProvider(new ServiceProviderOptions { ValidateScopes = true });
_tracing = _provider.GetRequiredService<MessageTracing>();
var tracing = _provider.GetRequiredService<MessageTracing>();
var endpoint = new MessagingEndpoint { System = "kafka", Destination = _topic, ConsumerGroup = _topic + "relayer" };
var telemetry = _provider.GetRequiredService<RelayerInstrumentation>().CreateEndpoint(new()
{
Expand All @@ -82,11 +83,19 @@ public async Task Setup()
var pipeline = new ConsumePipelineBuilder(_provider.GetRequiredService<IServiceScopeFactory>(),
_provider.GetRequiredService<MessageTypeRegistry>(), _provider.GetRequiredService<IMessageSerializer>(),
failures, _provider.GetService<IFailureClassifier>())
.Tracing(_tracing, endpoint).Telemetry(telemetry)
.Tracing(tracing, endpoint).Telemetry(telemetry)
.Handler(_provider.GetRequiredService<HandlerRegistry>().GetHandler<KafkaBenchmarkPayload, KafkaBenchmarkHandler>()).Build(1);
var receive = new ReceivePipeline<KafkaDelivery, KafkaMessageBinding>(pipeline, new(), 1);
_health = HealthEnabled ? new KafkaWorkerHealth(TimeProvider.System) : null;
_adapter = new(_native, receive.ProcessAsync, _tracing, endpoint, new(), _health);
_receive = new ReceivePipeline<KafkaDelivery, KafkaMessageBinding>(pipeline, new(), 1);
var health = HealthEnabled ? new KafkaWorkerHealth(TimeProvider.System) : null;
_adapter = new(_native, ReceiveAsync, tracing, endpoint, KafkaProcessing.Defaults(1), 1, health);
// The raw run uses the same native ordering, limits and commit policy without Relayer's observer.
_rawOptions = new()
{
Ordering = PartitionedProcessingOrder.Key, MaxConcurrentHandlers = 1,
CommitPolicy = PartitionCommitPolicy.CommitCompletedPeriodically,
Monitor = HealthEnabled ? new PartitionedProcessingMonitor() : null,
};
_rawProcessor = RawAsync;
}

private async Task<IKafkaConsumer<byte[], byte[]>> BuildConsumer(string bootstrap, string suffix)
Expand All @@ -106,42 +115,41 @@ public void Rewind()
}

[Benchmark(Baseline = true, OperationsPerInvoke = Records)]
public async Task<int> RawDekaf()
public Task<int> RawDekaf() => RunAsync(_raw, _rawProcessor, _rawOptions);

[Benchmark(OperationsPerInvoke = Records)]
public Task<int> Relayer() => RunAsync(_native, _adapter.Processor, _adapter.Options);

private async Task<int> RunAsync(IKafkaConsumer<byte[], byte[]> consumer, PartitionRecordProcessor<byte[], byte[]> processor,
PartitionedProcessingOptions options)
{
using var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(30));
var count = 0;
await foreach (var batch in _raw.ConsumeBatchAsync(timeout.Token))
{
foreach (var record in batch)
{
await using var scope = _provider.CreateAsyncScope();
var payload = JsonSerializer.Deserialize(record.Value, KafkaReceiveBenchmarkJson.Default.KafkaBenchmarkPayload);
await scope.ServiceProvider.GetRequiredService<KafkaBenchmarkHandler>().HandleAsync(payload, null!, timeout.Token);
count++;
}
if (batch.TryGetNextOffset(out var checkpoint))
{
_offset[0] = checkpoint;
await _raw.CommitAsync(_offset, timeout.Token);
}
if (count >= Records) return count;
}
throw new InvalidOperationException("Native stream ended before all benchmark records.");
_count = 0;
using var run = _run = new CancellationTokenSource(TimeSpan.FromSeconds(30));
try { await consumer.RunPartitionedAsync(processor, options, run.Token); }
catch (OperationCanceledException) when (_count >= Records) { }
if (_count < Records) throw new InvalidOperationException("Native run ended before all benchmark records.");
return _count;
}

[Benchmark(OperationsPerInvoke = Records)]
public async Task<int> Relayer()
private void Completed()
{
using var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(30));
var count = 0;
await foreach (var batch in _native.ConsumeBatchAsync(timeout.Token))
{
_health?.Progress();
count += await _adapter.ProcessBatchAsync(batch, timeout.Token);
_health?.Idle();
if (count >= Records) return count;
}
throw new InvalidOperationException("Native stream ended before all benchmark records.");
if (++_count == Records) _run.Cancel();
}

private async ValueTask RawAsync(PartitionRecordProcessorContext<byte[], byte[]> context, ConsumeResult<byte[], byte[]> record,
CancellationToken cancellationToken)
{
await using var scope = _provider.CreateAsyncScope();
var payload = JsonSerializer.Deserialize(record.Value, KafkaReceiveBenchmarkJson.Default.KafkaBenchmarkPayload);
await scope.ServiceProvider.GetRequiredService<KafkaBenchmarkHandler>().HandleAsync(payload, null!, cancellationToken);
Completed();
}

private async ValueTask<Disposition> ReceiveAsync(KafkaDelivery delivery, CancellationToken cancellationToken)
{
var disposition = await _receive.ProcessAsync(delivery, cancellationToken);
Completed();
return disposition;
}

[GlobalCleanup]
Expand Down
Loading
Loading