示例#1
0
    public static void Main(string[] args)
    {
        var disruptor  = new Disruptor <DynamicEvent>(() => new DynamicEvent(), 1024, TaskScheduler.Current);
        var ringBuffer = disruptor.Start();

        // Construct 2 batch event processors.
        var handler1   = new DynamicHandler();
        var processor1 = EventProcessorFactory.Create(ringBuffer, ringBuffer.NewBarrier(), handler1);

        var handler2   = new DynamicHandler();
        var processor2 = EventProcessorFactory.Create(ringBuffer, ringBuffer.NewBarrier(processor1.Sequence), handler2);

        // Dynamically add both sequences to the ring buffer
        ringBuffer.AddGatingSequences(processor1.Sequence, processor2.Sequence);

        // Start the new batch processors.
        var t1 = processor1.Start();
        var t2 = processor2.Start();

        Thread.Sleep(5000);

        // Remove a processor.

        // Stop the processor
        processor2.Halt();

        // Wait for shutdown the complete
        handler2.WaitShutdown(); // Or simply t2.Wait()

        // Remove the gating sequence from the ring buffer
        ringBuffer.RemoveGatingSequence(processor2.Sequence);
    }
示例#2
0
    public OneToThreeDiamondSequencedThroughputTest()
    {
        var sequenceBarrier = _ringBuffer.NewBarrier();

        var fizzHandler = new FizzBuzzEventHandler(FizzBuzzStep.Fizz);

        _eventProcessorFizz = EventProcessorFactory.Create(_ringBuffer, sequenceBarrier, fizzHandler);

        var buzzHandler = new FizzBuzzEventHandler(FizzBuzzStep.Buzz);

        _eventProcessorBuzz = EventProcessorFactory.Create(_ringBuffer, sequenceBarrier, buzzHandler);

        var sequenceBarrierFizzBuzz = _ringBuffer.NewBarrier(_eventProcessorFizz.Sequence, _eventProcessorBuzz.Sequence);

        _fizzBuzzHandler        = new FizzBuzzEventHandler(FizzBuzzStep.FizzBuzz);
        _eventProcessorFizzBuzz = EventProcessorFactory.Create(_ringBuffer, sequenceBarrierFizzBuzz, _fizzBuzzHandler);

        var temp = 0L;

        for (long i = 0; i < _iterations; i++)
        {
            var fizz = 0 == (i % 3L);
            var buzz = 0 == (i % 5L);

            if (fizz && buzz)
            {
                ++temp;
            }
        }
        _expectedResult = temp;

        _ringBuffer.AddGatingSequences(_eventProcessorFizzBuzz.Sequence);
    }
示例#3
0
    public OneToOneSequencedThroughputTest_BatchPublisher_BatchHandler()
    {
        _ringBuffer = RingBuffer <PerfEvent> .CreateSingleProducer(PerfEvent.EventFactory, _bufferSize, new YieldingWaitStrategy());

        var sequenceBarrier = _ringBuffer.NewBarrier();

        _handler        = new AdditionBatchEventHandler();
        _eventProcessor = EventProcessorFactory.Create(_ringBuffer, sequenceBarrier, _handler);
        _ringBuffer.AddGatingSequences(_eventProcessor.Sequence);
    }
    public OneToOneSequencedThroughputTest_Unmanaged()
    {
        _eventHandler = new AdditionEventHandler();
        _memory       = UnmanagedRingBufferMemory.Allocate(_bufferSize, PerfValueEvent.Size);
        _ringBuffer   = new UnmanagedRingBuffer <PerfValueEvent>(_memory, ProducerType.Single, new YieldingWaitStrategy());
        var sequenceBarrier = _ringBuffer.NewBarrier();

        _eventProcessor = EventProcessorFactory.Create(_ringBuffer, sequenceBarrier, _eventHandler);
        _ringBuffer.AddGatingSequences(_eventProcessor.Sequence);
    }
示例#5
0
    public OneToOneSequencedThroughputTest_LongArray()
    {
        _ringBuffer = RingBuffer <long[]> .CreateSingleProducer(() => new long[_arraySize], _bufferSize, new YieldingWaitStrategy());

        var sequenceBarrier = _ringBuffer.NewBarrier();

        _handler        = new LongArrayEventHandler();
        _eventProcessor = EventProcessorFactory.Create(_ringBuffer, sequenceBarrier, _handler);
        _ringBuffer.AddGatingSequences(_eventProcessor.Sequence);
    }
示例#6
0
    public void ShouldCreateValueTypeProxyForEachParameter()
    {
        // Arrange
        var dataProvider    = new RingBuffer <TestEvent>(() => new TestEvent(), 64);
        var sequenceBarrier = dataProvider.NewBarrier();

        // Act
        var eventProcessor = EventProcessorFactory.Create(dataProvider, sequenceBarrier, new DummyEventHandler <TestEvent>());

        // Assert
        foreach (var genericArgument in eventProcessor.GetType().GetGenericArguments().Where(x => x != typeof(TestEvent)))
        {
            Assert.True(genericArgument.IsValueType, $"Generic argument {genericArgument.Name} is not a value type");
        }
    }
示例#7
0
    public PingPongSequencedLatencyTest()
    {
        var pingBuffer = RingBuffer <PerfEvent> .CreateSingleProducer(PerfEvent.EventFactory, _bufferSize, new BlockingWaitStrategy());

        var pongBuffer = RingBuffer <PerfEvent> .CreateSingleProducer(PerfEvent.EventFactory, _bufferSize, new BlockingWaitStrategy());

        var pingBarrier = pingBuffer.NewBarrier();
        var pongBarrier = pongBuffer.NewBarrier();

        _pinger = new Pinger(pingBuffer, _iterations, _pauseNanos);
        _ponger = new Ponger(pongBuffer);

        _pingProcessor = EventProcessorFactory.Create(pongBuffer, pongBarrier, _pinger);
        _pongProcessor = EventProcessorFactory.Create(pingBuffer, pingBarrier, _ponger);

        pingBuffer.AddGatingSequences(_pongProcessor.Sequence);
        pongBuffer.AddGatingSequences(_pingProcessor.Sequence);
    }
    public PingPongSequencedLatencyTest_AsyncBatchHandler()
    {
        var pingBuffer = RingBuffer <PingPongEvent> .CreateSingleProducer(() => new PingPongEvent(), _bufferSize, new AsyncWaitStrategy());

        var pongBuffer = RingBuffer <PingPongEvent> .CreateSingleProducer(() => new PingPongEvent(), _bufferSize, new AsyncWaitStrategy());

        var pingBarrier = pingBuffer.NewAsyncBarrier();
        var pongBarrier = pongBuffer.NewAsyncBarrier();

        _pinger = new Pinger(pongBuffer, _pauseNanos);
        _ponger = new Ponger(pingBuffer);

        _pingProcessor = EventProcessorFactory.Create(pingBuffer, pingBarrier, _pinger);
        _pongProcessor = EventProcessorFactory.Create(pongBuffer, pongBarrier, _ponger);

        pingBuffer.AddGatingSequences(_pingProcessor.Sequence);
        pongBuffer.AddGatingSequences(_pongProcessor.Sequence);
    }
    public OneToThreeSequencedThroughputTest()
    {
        for (long i = 0; i < _iterations; i++)
        {
            _results[0] = Operation.Addition.Op(_results[0], i);
            _results[1] = Operation.Subtraction.Op(_results[1], i);
            _results[2] = Operation.And.Op(_results[2], i);
        }

        _ringBuffer = RingBuffer <PerfEvent> .CreateSingleProducer(PerfEvent.EventFactory, _bufferSize, new YieldingWaitStrategy());

        var sequenceBarrier = _ringBuffer.NewBarrier();

        _handlers[0] = new PerfMutationEventHandler(Operation.Addition);
        _handlers[1] = new PerfMutationEventHandler(Operation.Subtraction);
        _handlers[2] = new PerfMutationEventHandler(Operation.And);


        for (var i = 0; i < _numEventProcessors; i++)
        {
            _eventProcessors[i] = EventProcessorFactory.Create(_ringBuffer, sequenceBarrier, _handlers[i]);
        }
        _ringBuffer.AddGatingSequences(_eventProcessors.Select(x => x.Sequence).ToArray());
    }
    public OneToThreePipelineSequencedThroughputTest()
    {
        var stepOneFunctionHandler = new FunctionEventHandler(FunctionStep.One);
        var stepTwoFunctionHandler = new FunctionEventHandler(FunctionStep.Two);

        _stepThreeFunctionHandler = new FunctionEventHandler(FunctionStep.Three);

        var stepOneSequenceBarrier = _ringBuffer.NewBarrier();

        _stepOneEventProcessor = EventProcessorFactory.Create(_ringBuffer, stepOneSequenceBarrier, stepOneFunctionHandler);

        var stepTwoSequenceBarrier = _ringBuffer.NewBarrier(_stepOneEventProcessor.Sequence);

        _stepTwoEventProcessor = EventProcessorFactory.Create(_ringBuffer, stepTwoSequenceBarrier, stepTwoFunctionHandler);

        var stepThreeSequenceBarrier = _ringBuffer.NewBarrier(_stepTwoEventProcessor.Sequence);

        _stepThreeEventProcessor = EventProcessorFactory.Create(_ringBuffer, stepThreeSequenceBarrier, _stepThreeFunctionHandler);

        var temp       = 0L;
        var operandTwo = _operandTwoInitialValue;

        for (long i = 0; i < _iterations; i++)
        {
            var stepOneResult = i + operandTwo--;
            var stepTwoResult = stepOneResult + 3;

            if ((stepTwoResult & 4L) == 4L)
            {
                ++temp;
            }
        }
        _expectedResult = temp;

        _ringBuffer.AddGatingSequences(_stepThreeEventProcessor.Sequence);
    }
    public void ShouldReportValueEventHandlerProgressByUpdatingSequenceViaCallback()
    {
        var ringBuffer = ValueRingBuffer <StubValueEvent> .CreateMultiProducer(() => new StubValueEvent(-1), 16);

        var sequenceBarrier = ringBuffer.NewBarrier();
        var handler         = new TestSequenceReportingEventHandler();
        var eventProcessor  = EventProcessorFactory.Create(ringBuffer, sequenceBarrier, handler);

        ringBuffer.AddGatingSequences(eventProcessor.Sequence);

        var task = eventProcessor.Start();

        Assert.AreEqual(-1L, eventProcessor.Sequence.Value);
        ringBuffer.Publish(ringBuffer.Next());

        handler.CallbackSignal.WaitOne();
        Assert.AreEqual(0L, eventProcessor.Sequence.Value);

        handler.OnEndOfBatchSignal.Set();
        Assert.AreEqual(0L, eventProcessor.Sequence.Value);

        eventProcessor.Halt();
        Assert.IsTrue(task.Wait(TimeSpan.FromSeconds(10)));
    }
示例#12
0
 private static IValueEventProcessor <T> CreateEventProcessor <T>(IValueDataProvider <T> dataProvider, SequenceBarrier sequenceBarrier, IValueEventHandler <T> eventHandler)
     where T : struct
 {
     return(EventProcessorFactory.Create(dataProvider, sequenceBarrier, eventHandler));
 }
示例#13
0
 private static IAsyncEventProcessor <T> CreateEventProcessor <T>(IDataProvider <T> dataProvider, AsyncSequenceBarrier sequenceBarrier, IAsyncBatchEventHandler <T> eventHandler)
     where T : class
 {
     return(EventProcessorFactory.Create(dataProvider, sequenceBarrier, eventHandler));
 }
 public void Setup()
 {
     _processor = EventProcessorFactory.Create(_ringBuffer, _ringBuffer.NewBarrier(), new XEventHandler(() => _processor.Halt()));
 }