static void DemoNonDSLDisruptor() { var bufferSize = (int)Math.Pow(2, 4); var ringBuffer = RingBuffer <DataVO> .CreateSingleProducer(() => new DataVO(), bufferSize); var barrier = ringBuffer.NewBarrier(); var eventProcessor = new BatchEventProcessor <DataVO>(ringBuffer, barrier, new DataEventHandler("Handler1")); Task.Factory.StartNew(() => eventProcessor.Run()); var task = Task.Run(() => eventProcessor.Run()); var sequenceNo = ringBuffer.Next(); Console.WriteLine($"Current SequenceNo:{sequenceNo.ToString()}"); var data = ringBuffer[sequenceNo]; data.Value = "Hello"; ringBuffer.Publish(sequenceNo); sequenceNo = ringBuffer.Next(); Console.WriteLine($"Current SequenceNo:{sequenceNo.ToString()}"); data = ringBuffer[sequenceNo]; data.Value = "World"; ringBuffer.Publish(sequenceNo); Thread.Sleep(3000); eventProcessor.Halt(); }
protected override long RunDisruptorPass() { CountdownEvent latch = new CountdownEvent(1); fizzBuzzHandler.rset(latch, batchProcessorFizzBuzz.Sequence.Value + ITERATIONS); Task.Run(() => batchProcessorFizz.Run()); Task.Run(() => batchProcessorBuzz.Run()); Task.Run(() => batchProcessorFizzBuzz.Run()); Stopwatch start = Stopwatch.StartNew(); for (long i = 0; i < ITERATIONS; i++) { long sequence = ringBuffer.Next(); ringBuffer[sequence].Value = i; ringBuffer.Publish(sequence); } //mr.WaitOne (); latch.Wait(); long opsPerSecond = (ITERATIONS * 1000L) / (start.ElapsedMilliseconds); batchProcessorFizz.Halt(); batchProcessorBuzz.Halt(); batchProcessorFizzBuzz.Halt(); PerfTestUtil.failIfNot(expectedResult, fizzBuzzHandler.FizzBuzzCounter); return(opsPerSecond); }
protected override long RunDisruptorPass() { var latch = new ManualResetEvent(false); stepThreeFunctionHandler.Reset(latch, stepThreeBatchProcessor.Sequence.Value + ITERATIONS); Task.Factory.StartNew(() => stepOneBatchProcessor.Run(), CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default); Task.Factory.StartNew(() => stepTwoBatchProcessor.Run(), CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default); Task.Factory.StartNew(() => stepThreeBatchProcessor.Run(), CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default); var runTime = Stopwatch.StartNew(); var operandTwo = OPERAND_TWO_INITIAL_VALUE; for (long i = 0; i < ITERATIONS; i++) { var sequence = ringBuffer.Next(); var @event = ringBuffer[sequence]; @event.OperandOne = i; @event.OperandTwo = operandTwo--; ringBuffer.Publish(sequence); } latch.WaitOne(); var opsPerSecond = (ITERATIONS * 1000L) / runTime.ElapsedMilliseconds; stepOneBatchProcessor.Halt(); stepTwoBatchProcessor.Halt(); stepThreeBatchProcessor.Halt(); InitResult(); Assert.AreEqual(expectedResult, stepThreeFunctionHandler.GetStepThreeCounter); return(opsPerSecond); }
protected override long RunDisruptorPass() { CountdownEvent latch = new CountdownEvent(1); long expectedCount = batchEventProcessor.Sequence.Value + ITERATIONS; handler.reset(latch, expectedCount); //executor.submit(batchEventProcessor); //Task.Factory.StartNew(() => batchEventProcessor.Run() // , CancellationToken.None // , TaskCreationOptions.LongRunning // , new LimitedConcurrencyLevelTaskScheduler(4)); ThreadPool.UnsafeQueueUserWorkItem(o => batchEventProcessor.Run(), null); Stopwatch start = Stopwatch.StartNew(); RingBuffer <ValueEvent> rb = ringBuffer; for (long i = 0; i < ITERATIONS; i++) { long next = rb.Next(); rb[next].Value = i; rb.Publish(next); } latch.Wait(); long opsPerSecond = (ITERATIONS * 1000L) / (start.ElapsedMilliseconds); waitForEventProcessorSequence(expectedCount); batchEventProcessor.Halt(); PerfTestUtil.failIfNot(expectedResult, handler.Value); return(opsPerSecond); }
protected override long RunDisruptorPass() { CountdownEvent latch = new CountdownEvent(1); stepThreeFunctionHandler.reset(latch, stepThreeBatchProcessor.Sequence.Value + ITERATIONS); Task.Run(() => stepOneBatchProcessor.Run()); Task.Run(() => stepTwoBatchProcessor.Run()); Task.Run(() => stepThreeBatchProcessor.Run()); Stopwatch start = Stopwatch.StartNew(); long operandTwo = OPERAND_TWO_INITIAL_VALUE; for (long i = 0; i < ITERATIONS; i++) { long sequence = ringBuffer.Next(); FunctionEvent @event = ringBuffer[sequence]; @event.OperandOne = i; @event.OperandTwo = operandTwo--; ringBuffer.Publish(sequence); } latch.Wait(); long opsPerSecond = (ITERATIONS * 1000L) / (start.ElapsedMilliseconds); stepOneBatchProcessor.Halt(); stepTwoBatchProcessor.Halt(); stepThreeBatchProcessor.Halt(); PerfTestUtil.failIfNot(expectedResult, stepThreeFunctionHandler.StepThreeCounter); return(opsPerSecond); }
public void ShouldCallExceptionHandlerOnUncaughtException() { var ex = new Exception(); var exceptionHandlerMock = new Mock <IExceptionHandler>(); _batchEventProcessor.SetExceptionHandler(exceptionHandlerMock.Object); _batchHandlerMock.Setup(bh => bh.OnEvent(_ringBuffer[0], 0, true)) .Throws(ex); // OnNext raises an expcetion exceptionHandlerMock.Setup(bh => bh.HandleEventException(ex, 0, _ringBuffer[0])) .Callback(() => _countDownEvent.Signal()); // Exception should be handled here and signal the CDE //var thread = new Thread(_batchEventProcessor.Run); //thread.Start(); var _task = Task.Factory.StartNew(() => _batchEventProcessor.Run()); _ringBuffer.Publish(_ringBuffer.Next()); _countDownEvent.Wait(50); _batchEventProcessor.Halt(); SpinWait.SpinUntil(() => _task.IsCompleted); //thread.Join(); _batchHandlerMock.VerifyAll(); exceptionHandlerMock.VerifyAll(); }
protected override long RunDisruptorPass() { CountdownEvent latch = new CountdownEvent(1); handler.reset(latch, batchEventProcessor.Sequence.Value + ((ITERATIONS / NUM_PUBLISHERS) * NUM_PUBLISHERS)); Task[] task = new Task[NUM_PUBLISHERS]; //for (int i = 0; i < NUM_PUBLISHERS; Interlocked.Increment(ref i)) //{ // task[i] = Task.Run(() => valuePublishers[i].Run()); //} task[0] = Task.Factory.StartNew(() => valuePublishers[0].Run()); task[1] = Task.Factory.StartNew(() => valuePublishers[1].Run()); task[2] = Task.Factory.StartNew(() => valuePublishers[2].Run()); //ThreadPool.QueueUserWorkItem(o=>{},null); Task.Factory.StartNew(() => batchEventProcessor.Run()); var stopWatch = Stopwatch.StartNew(); cyclicBarrier.SignalAndWait(); try { Task.WaitAll(task); } catch (AggregateException ex) { Console.WriteLine(ex.ToString()); } latch.Wait(); stopWatch.Stop(); var millisecongd = stopWatch.ElapsedMilliseconds; long opsPerSecond = (ITERATIONS * 1000L) / (millisecongd == 0?1:millisecongd); batchEventProcessor.Halt(); return(opsPerSecond); }
protected override long RunDisruptorPass() { CountdownEvent latch = new CountdownEvent(1); long expectedCount = batchEventProcessor.Sequence.Value + ITERATIONS; handler.Reset(latch, ITERATIONS); Task.Factory.StartNew(() => batchEventProcessor.Run()); Stopwatch start = Stopwatch.StartNew(); RingBuffer <long[]> rb = ringBuffer; for (long i = 0; i < ITERATIONS; i++) { long next = rb.Next(); long[] @event = rb.Get(next); for (int j = 0; j < @event.Length; j++) { @event[j] = i; } rb.Publish(next); } latch.Wait(); long opsPerSecond = (ITERATIONS * ARRAY_SIZE * 1000L) / (start.ElapsedMilliseconds); waitForEventProcessorSequence(expectedCount); batchEventProcessor.Halt(); PerfTestUtil.failIf(0, handler.Value); return(opsPerSecond); }
public void Run(Stopwatch sw) { var mre = new ManualResetEvent(false); _fizzBuzzHandler.Reset(mre, _batchProcessorFizzBuzz.Sequence.Value + _iterations); var processorTask1 = Task.Run(() => _batchProcessorFizz.Run()); var processorTask2 = Task.Run(() => _batchProcessorBuzz.Run()); var processorTask3 = Task.Run(() => _batchProcessorFizzBuzz.Run()); _batchProcessorFizz.WaitUntilStarted(TimeSpan.FromSeconds(5)); _batchProcessorBuzz.WaitUntilStarted(TimeSpan.FromSeconds(5)); _batchProcessorFizzBuzz.WaitUntilStarted(TimeSpan.FromSeconds(5)); sw.Start(); for (long i = 0; i < _iterations; i++) { var sequence = _ringBuffer.Next(); _ringBuffer[sequence].Value = i; _ringBuffer.Publish(sequence); } mre.WaitOne(); sw.Stop(); _batchProcessorFizz.Halt(); _batchProcessorBuzz.Halt(); _batchProcessorFizzBuzz.Halt(); Task.WaitAll(processorTask1, processorTask2, processorTask3); }
protected override long RunDisruptorPass() { CountdownEvent latch = new CountdownEvent(1); handler.reset(latch, batchEventProcessor.Sequence.Value + ((ITERATIONS / NUM_PUBLISHERS) * NUM_PUBLISHERS)); Task[] task = new Task[NUM_PUBLISHERS]; //for (int i = 0; i < NUM_PUBLISHERS; i++) //{ // task[i] = Task.Run(() => valuePublishers[i].Run()); //} task[0] = Task.Factory.StartNew(() => valuePublishers[0].Run()); task[1] = Task.Factory.StartNew(() => valuePublishers[1].Run()); task[2] = Task.Factory.StartNew(() => valuePublishers[2].Run()); //Task.Run(() => batchEventProcessor.Run()); Task.Factory.StartNew(() => batchEventProcessor.Run()); var stopWatch = Stopwatch.StartNew(); cyclicBarrier.SignalAndWait(); Task.WaitAll(task); latch.Wait(); long opsPerSecond = (ITERATIONS * 1000L) / (stopWatch.ElapsedMilliseconds); batchEventProcessor.Halt(); return(opsPerSecond); }
public long Run(Stopwatch stopwatch) { _cyclicBarrier.Reset(); var latch = new ManualResetEvent(false); _handler.Reset(latch, _batchEventProcessor.Sequence.Value + ((_iterations / _numPublishers) * _numPublishers)); var futures = new Task[_numPublishers]; for (var i = 0; i < _numPublishers; i++) { var index = i; futures[i] = Task.Factory.StartNew(() => _valuePublishers[index](_cyclicBarrier, _ringBuffer, _iterations), CancellationToken.None, TaskCreationOptions.None, _scheduler); } var processorTask = Task.Factory.StartNew(() => _batchEventProcessor.Run(), CancellationToken.None, TaskCreationOptions.None, _scheduler); stopwatch.Start(); _cyclicBarrier.Signal(); _cyclicBarrier.Wait(); for (var i = 0; i < _numPublishers; i++) { futures[i].Wait(); } latch.WaitOne(); stopwatch.Stop(); _batchEventProcessor.Halt(); processorTask.Wait(2000); return(_iterations); }
protected override long RunDisruptorPass() { var latch = new ManualResetEvent(false); fizzBuzzHandler.Reset(latch, batchProcessorFizzBuzz.Sequence.Value + ITERATIONS); Task.Factory.StartNew(() => batchProcessorFizz.Run(), CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default); Task.Factory.StartNew(() => batchProcessorBuzz.Run(), CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default); Task.Factory.StartNew(() => batchProcessorFizzBuzz.Run(), CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default); var run = DateTime.Now;//Stopwatch.StartNew(); for (long i = 0; i < ITERATIONS; i++) { var sequence = ringBuffer.Next(); ringBuffer[sequence].Value = i; ringBuffer.Publish(sequence); } latch.WaitOne(); var opsPerSecond = (ITERATIONS * 1000L) / (long)DateTime.Now.Subtract(run).Milliseconds; batchProcessorFizz.Halt(); batchProcessorBuzz.Halt(); batchProcessorFizzBuzz.Halt(); Assert.AreEqual(ExpectedResult, fizzBuzzHandler.FizzBuzzCounter); return(opsPerSecond); }
private const int _ringSize = 16; // Must be multiple of 2 static void Main(string[] args) { var sw = new Stopwatch(); sw.Start(); var ringBuffer = RingBuffer <MyValueEntry> .CreateSingleProducer(() => new MyValueEntry(), _ringSize, new YieldingWaitStrategy()); //Case A: 1 Producer - 1 Consumer (Unicast event) var sequenceBarrier = ringBuffer.NewBarrier(); var batchProcessorPrintVal = new BatchEventProcessor <MyValueEntry>(ringBuffer, sequenceBarrier, new MyValueAdditionHandler()); var processorTask1 = Task.Run(() => batchProcessorPrintVal.Run()); batchProcessorPrintVal.WaitUntilStarted(TimeSpan.FromSeconds(5)); Simulator.SimulatePublishEvent(ringBuffer, 1000); batchProcessorPrintVal.Halt(); Task.WaitAll(processorTask1); sw.Stop(); Console.WriteLine($"elapsed {sw.ElapsedMilliseconds} ms"); Console.Read(); }
private void RunDisruptorPass() { var latch = new System.Threading.ManualResetEvent(false); stepThreeFunctionHandler.Reset(latch, stepThreeBatchProcessor.Sequence.Value + ITERATIONS); Task.Factory.StartNew(() => stepOneBatchProcessor.Run()); Task.Factory.StartNew(() => stepTwoBatchProcessor.Run()); Task.Factory.StartNew(() => stepThreeBatchProcessor.Run()); for (long i = 0; i < ITERATIONS; i++) { var t0 = Stopwatch.GetTimestamp(); var sequence = ringBuffer.Next(); ringBuffer[sequence].Value = t0; ringBuffer.Publish(sequence); var pauseStart = Stopwatch.GetTimestamp(); while (PAUSE_NANOS > ((Stopwatch.GetTimestamp() - pauseStart) * TicksToNanos)) { // busy spin } } latch.WaitOne(); stepOneBatchProcessor.Halt(); stepTwoBatchProcessor.Halt(); stepThreeBatchProcessor.Halt(); }
public long Run(Stopwatch stopwatch) { var latch = new ManualResetEvent(false); _fizzBuzzHandler.Reset(latch, _batchProcessorFizzBuzz.Sequence.Value + _iterations); var processorTask1 = Task.Run(() => _batchProcessorFizz.Run()); var processorTask2 = Task.Run(() => _batchProcessorBuzz.Run()); var processorTask3 = Task.Run(() => _batchProcessorFizzBuzz.Run()); stopwatch.Start(); for (long i = 0; i < _iterations; i++) { var sequence = _ringBuffer.Next(); _ringBuffer[sequence].Value = i; _ringBuffer.Publish(sequence); } latch.WaitOne(); stopwatch.Stop(); _batchProcessorFizz.Halt(); _batchProcessorBuzz.Halt(); _batchProcessorFizzBuzz.Halt(); Task.WaitAll(processorTask1, processorTask2, processorTask3); PerfTestUtil.FailIfNot(_expectedResult, _fizzBuzzHandler.FizzBuzzCounter); return(_iterations); }
private void RunDisruptorPass() { var latch = new ManualResetEvent(false); var barrier = new Barrier(3); pinger.Reset(barrier, latch, histogram); ponger.Reset(barrier); System.Threading.Tasks.Task.Factory.StartNew(() => pongProcessor.Run()); System.Threading.Tasks.Task.Factory.StartNew(() => pingProcessor.Run()); barrier.SignalAndWait(); latch.WaitOne(); pingProcessor.Halt(); pongProcessor.Halt(); }
public void Should_Call_Exception_Handler_On_Uncaught_Exception() { var exceptionLatch = new CountdownEvent(1); var exceptionHandler = new LatchExceptionHandler(exceptionLatch); var processor = new BatchEventProcessor <StubEvent>(_ringBuffer, _barrier, new ExceptionEventHandler(), exceptionHandler); _ringBuffer.AddGatingSequences(processor.GetSequence()); var thread = new Thread(() => processor.Run()); thread.Start(); _ringBuffer.Publish(_ringBuffer.Next()); Assert.True(exceptionLatch.Wait(TimeSpan.FromSeconds(2000))); processor.Halt(); thread.Join(); }
public void ShouldCallMethodsInLifecycleOrderForBatch() { var eventSignal = new CountdownEvent(3); var eventHandler = new ActionEventHandler <StubEvent>(x => eventSignal.Signal()); var batchEventProcessor = new BatchEventProcessor <StubEvent>(_ringBuffer, _sequenceBarrier, eventHandler); _ringBuffer.AddGatingSequences(batchEventProcessor.Sequence); _ringBuffer.Publish(_ringBuffer.Next()); _ringBuffer.Publish(_ringBuffer.Next()); _ringBuffer.Publish(_ringBuffer.Next()); var task = Task.Run(() => batchEventProcessor.Run()); Assert.IsTrue(eventSignal.Wait(TimeSpan.FromSeconds(2))); batchEventProcessor.Halt(); Assert.IsTrue(task.Wait(500)); }
public void ShouldCallExceptionHandlerOnUncaughtException() { var exceptionSignal = new CountdownEvent(1); var exceptionHandler = new ActionExceptionHandler <StubEvent>(x => exceptionSignal.Signal()); var eventHandler = new ActionEventHandler <StubEvent>(x => throw new NullReferenceException()); var batchEventProcessor = new BatchEventProcessor <StubEvent>(_ringBuffer, _sequenceBarrier, eventHandler); _ringBuffer.AddGatingSequences(batchEventProcessor.Sequence); batchEventProcessor.SetExceptionHandler(exceptionHandler); var task = Task.Run(() => batchEventProcessor.Run()); _ringBuffer.Publish(_ringBuffer.Next()); Assert.IsTrue(exceptionSignal.Wait(TimeSpan.FromSeconds(2))); batchEventProcessor.Halt(); Assert.IsTrue(task.Wait(500)); }
public void Should_Call_Methods_In_Lifecycle_Order_For_Batch() { var eventLatch = new CountdownEvent(3); var eventHandler = new LatchEventHandler(eventLatch); var processor = new BatchEventProcessor <StubEvent>(_ringBuffer, _barrier, eventHandler, _exceptionHandler); _ringBuffer.AddGatingSequences(processor.GetSequence()); _ringBuffer.Publish(_ringBuffer.Next()); _ringBuffer.Publish(_ringBuffer.Next()); _ringBuffer.Publish(_ringBuffer.Next()); var thread = new Thread(() => processor.Run()); thread.Start(); Assert.True(eventLatch.Wait(TimeSpan.FromSeconds(2))); processor.Halt(); thread.Join(); }
static void DemoNonDSLUnicast() { var ringBuffer = RingBuffer <DataVO> .CreateSingleProducer(() => new DataVO(), (int)Math.Pow(2, 4)); var barrier = ringBuffer.NewBarrier(); var eventProcessor = new BatchEventProcessor <DataVO>(ringBuffer, barrier, new DataEventHandler("Handler1")); Task.Factory.StartNew(() => eventProcessor.Run()); var idx = 0; while (true) { var sequenceNo = ringBuffer.Next(); var data = ringBuffer[sequenceNo]; data.Value = idx++.ToString(); ringBuffer.Publish(sequenceNo); Thread.Sleep(250); } eventProcessor.Halt(); }
public void ReportAccurateBatchSizesAtBatchStartTime() { var batchSizes = new List <long>(); var signal = new CountdownEvent(6); var batchEventProcessor = new BatchEventProcessor <StubEvent>(_ringBuffer, _sequenceBarrier, new LoopbackEventHandler(_ringBuffer, batchSizes, signal)); _ringBuffer.Publish(_ringBuffer.Next()); _ringBuffer.Publish(_ringBuffer.Next()); _ringBuffer.Publish(_ringBuffer.Next()); var task = Task.Run(() => batchEventProcessor.Run()); signal.Wait(); batchEventProcessor.Halt(); Assert.IsTrue(task.Wait(500)); Assert.That(batchSizes, Is.EqualTo(new List <long> { 3, 2, 1 })); }
protected override long RunDisruptorPass() { CountdownEvent latch = new CountdownEvent(1); //ManualResetEvent latch = new ManualResetEvent(false); long expectedCount = batchEventProcessor.Sequence.Value + ITERATIONS * BATCH_SIZE; handler.reset(latch, expectedCount); Task.Factory.StartNew(() => batchEventProcessor.Run() , CancellationToken.None , TaskCreationOptions.LongRunning , new LimitedConcurrencyLevelTaskScheduler(4)); //ThreadPool.QueueUserWorkItem(o=>batchEventProcessor.Run()); Stopwatch start = Stopwatch.StartNew(); RingBuffer <ValueEvent> rb = ringBuffer; for (long i = 0; i < ITERATIONS; i++) { long hi = rb.Next(BATCH_SIZE); long lo = hi - (BATCH_SIZE - 1); for (long l = lo; l <= hi; l++) { rb.Get(l).Value = i; } rb.Publish(lo, hi); } latch.Wait(); long opsPerSecond = (BATCH_SIZE * ITERATIONS * 1000L) / (start.ElapsedMilliseconds); waitForEventProcessorSequence(expectedCount); batchEventProcessor.Halt(); Console.WriteLine("expectedResult={0:###,###,###},realValue={1:###,###,###}", expectedResult, handler.Value); PerfTestUtil.failIfNot(expectedResult, handler.Value); return(opsPerSecond); }