From 806219a1ac36ab05693e70baf0cc23414c10307f Mon Sep 17 00:00:00 2001 From: Olivier Coanet Date: Fri, 4 Oct 2019 18:46:00 +0200 Subject: [PATCH] Update BatchEventProcessorBenchmarks --- .../BatchEventProcessorBenchmarks.cs | 68 ++++++++++++------- 1 file changed, 43 insertions(+), 25 deletions(-) diff --git a/src/Disruptor.Benchmarks/BatchEventProcessorBenchmarks.cs b/src/Disruptor.Benchmarks/BatchEventProcessorBenchmarks.cs index 08d75c61..45ff078d 100644 --- a/src/Disruptor.Benchmarks/BatchEventProcessorBenchmarks.cs +++ b/src/Disruptor.Benchmarks/BatchEventProcessorBenchmarks.cs @@ -1,39 +1,67 @@ -using BenchmarkDotNet.Attributes; +using System; +using System.Runtime.CompilerServices; +using BenchmarkDotNet.Attributes; namespace Disruptor.Benchmarks { public class BatchEventProcessorBenchmarks { + private readonly Sequence _sequence = new Sequence(); private readonly RingBuffer _ringBuffer; - private readonly IBatchEventProcessor _processor; private readonly TestEventHandler _eventHandler; + private readonly ISequenceBarrier _sequenceBarrier; public BatchEventProcessorBenchmarks() { _ringBuffer = new RingBuffer(() => new TestEvent(), new SingleProducerSequencer(4096, new SpinWaitWaitStrategy())); _eventHandler = new TestEventHandler(); - _processor = BatchEventProcessorFactory.Create(_ringBuffer, _ringBuffer.NewBarrier(), _eventHandler); + _sequenceBarrier = _ringBuffer.NewBarrier(); - _eventHandler.Processor = _processor; - _eventHandler.RingBuffer = _ringBuffer; - _ringBuffer.AddGatingSequences(_processor.Sequence); + _ringBuffer.PublishEvent().Dispose(); } - [IterationSetup] - public void BeforeRun() + public volatile int Running; + + [Benchmark] + public long ProcessEvent() { - var sequence = _ringBuffer.Next(4096); - for (var i = 0; i < 4096; i++) + var nextSequence = 0L; + try + { + var availableSequence = _sequenceBarrier.WaitFor(nextSequence); + + while (nextSequence <= availableSequence) + { + var evt = _ringBuffer[nextSequence]; + _eventHandler.OnEvent(evt, nextSequence, nextSequence == availableSequence); + nextSequence++; + } + + _sequence.SetValue(availableSequence); + } + catch (TimeoutException) + { + NotifyTimeout(_sequence.Value); + } + catch (AlertException) + { + if (Running != 2) + { + return nextSequence; + } + } + catch (Exception) { - _ringBuffer[sequence + i].Data = i; + _sequence.SetValue(nextSequence); + nextSequence++; } - _ringBuffer.Publish(sequence); + + return nextSequence; } - [Benchmark(OperationsPerInvoke = 4096)] - public void Run() + [MethodImpl(MethodImplOptions.NoInlining)] + private void NotifyTimeout(long sequenceValue) { - _processor.Run(); } public class TestEvent @@ -43,18 +71,8 @@ public class TestEvent public class TestEventHandler : IEventHandler { - public long Sum { get; set; } - public RingBuffer RingBuffer { get; set; } - public IBatchEventProcessor Processor { get; set; } - public void OnEvent(TestEvent data, long sequence, bool endOfBatch) { - Sum += data.Data; - - if (data.Data == 4095) - Processor.Halt(); - else - RingBuffer.Publish(sequence + 1); } } }