protected override long RunDisruptorPass()
        {
            int batchSize = 10;
            var latch = new ManualResetEvent(false);
            long expectedCount = myRunnable.GetSequence.Value + (ITERATIONS * batchSize);
            myRunnable.Reset(latch, expectedCount);
            var _scheduler = new RoundRobinThreadAffinedTaskScheduler(4);
            Task.Factory.StartNew(myRunnable.Run, CancellationToken.None, TaskCreationOptions.None, _scheduler);
            var start = Stopwatch.StartNew();
            var spinwait = default(SpinWait);
            var sequencer = this.sequencer;

            for (long i = 0; i < ITERATIONS; i++)
            {
                long next = sequencer.Next(batchSize);
                sequencer.Publish((next - (batchSize - 1)), next);
            }

            latch.WaitOne();

            long opsPerSecond = (ITERATIONS * 1000L * batchSize) / start.ElapsedMilliseconds;
            WaitForEventProcessorSequence(expectedCount, spinwait,myRunnable);

            return opsPerSecond;
        }
 public UniCast1P1CDisruptorWithAffinityPerfTest()
     : base(100 * Million)
 {
     _scheduler = new RoundRobinThreadAffinedTaskScheduler(2);
     _disruptor = new Disruptor<ValueEvent>(() => new ValueEvent(), BufferSize, _scheduler, ProducerType.SINGLE, new YieldingWaitStrategy());
     _mru = new ManualResetEvent(false);
     _eventHandler = new ValueAdditionEventHandler(Iterations, _mru);
     _disruptor.HandleEventsWith(_eventHandler);
     _ringBuffer = _disruptor.GetRingBuffer;
 }
        public UniCast1P1CBatchDisruptorWithAffinityPerfTest()
            : base(100 * Million)
        {
            _scheduler = new RoundRobinThreadAffinedTaskScheduler(2);

            _disruptor = new Disruptor<ValueEvent>(()=>new ValueEvent(),
                                                   new SingleThreadedClaimStrategy(BufferSize),
                                                   new YieldingWaitStrategy(),
                                                   _scheduler);
            _mru = new ManualResetEvent(false);
            _eventHandler = new ValueAdditionEventHandler(Iterations, _mru);
            _disruptor.HandleEventsWith(_eventHandler);
            _ringBuffer = _disruptor.RingBuffer;
        }
        public MultiCast1P3CDisruptorWithAffinityPerfTest()
            : base(100 * Million)
        {
            _scheduler = new RoundRobinThreadAffinedTaskScheduler(4);
            _disruptor = new Disruptor<ValueEvent>(() => new ValueEvent(), Size, _scheduler, ProducerType.MULTI, new YieldingWaitStrategy());

            _latch = new CountdownEvent(3);

            _handler1 = new ValueMutationEventHandler(Operation.Addition, Iterations, _latch);
            _handler2 = new ValueMutationEventHandler(Operation.Substraction, Iterations, _latch);
            _handler3 = new ValueMutationEventHandler(Operation.And, Iterations, _latch);

            _disruptor.HandleEventsWith(_handler1, _handler2, _handler3);
            _ringBuffer = _disruptor.GetRingBuffer;
        }
        public Pipeline3StepDisruptorWithAffinityPerfTest()
            : base(100 * Million)//1千万  1亿
        {
            _scheduler = new RoundRobinThreadAffinedTaskScheduler(4);
            _disruptor = new Disruptor<FunctionEvent>(() => new FunctionEvent(),Size,_scheduler, ProducerType.SINGLE,new YieldingWaitStrategy());                                                    

            _mru = new ManualResetEvent(false);
            _stepThreeFunctionEventHandler = new FunctionEventHandler(FunctionStep.Three, Iterations, _mru);

            _disruptor.HandleEventsWith(new FunctionEventHandler(FunctionStep.One, Iterations, _mru))
                .Then(new FunctionEventHandler(FunctionStep.Two, Iterations, _mru))
                .Then(_stepThreeFunctionEventHandler);

            _ringBuffer = _disruptor.GetRingBuffer;
        }
        public Pipeline3StepLatencyDisruptorWithAffinityPerfTest()
            : base(2 * Million)
        {
            _scheduler = new RoundRobinThreadAffinedTaskScheduler(4);

            _disruptor = new Disruptor<ValueEvent>(() => new ValueEvent(), Size, _scheduler, ProducerType.SINGLE, new YieldingWaitStrategy());

            _mru = new ManualResetEvent(false);
            _stepOneFunctionEventHandler = new LatencyStepEventHandler(FunctionStep.One, Histogram, StopwatchTimestampCostInNano, TicksToNanos, Iterations, _mru);
            _stepTwoFunctionEventHandler = new LatencyStepEventHandler(FunctionStep.Two, Histogram, StopwatchTimestampCostInNano, TicksToNanos, Iterations, _mru);
            _stepThreeFunctionEventHandler = new LatencyStepEventHandler(FunctionStep.Three, Histogram, StopwatchTimestampCostInNano, TicksToNanos, Iterations, _mru);

            _disruptor.HandleEventsWith(_stepOneFunctionEventHandler)
                .Then(_stepTwoFunctionEventHandler)
                .Then(_stepThreeFunctionEventHandler);
            _ringBuffer = _disruptor.GetRingBuffer;
        }
        public DiamondPath1P3CDisruptorWithAffinityPerfTest()
            : base(100 * Million)
        {
            _scheduler = new RoundRobinThreadAffinedTaskScheduler(4);
            _disruptor = new Disruptor<FizzBuzzEvent>(() => new FizzBuzzEvent(),
                                                     Size,
                                                      _scheduler);

            _mru = new ManualResetEvent(false);
            _fizzEventHandler = new FizzBuzzEventHandler(FizzBuzzStep.Fizz, Iterations, _mru);
            _buzzEventHandler = new FizzBuzzEventHandler(FizzBuzzStep.Buzz, Iterations, _mru);
            _fizzBuzzEventHandler = new FizzBuzzEventHandler(FizzBuzzStep.FizzBuzz, Iterations, _mru);

            _disruptor.HandleEventsWith(_fizzEventHandler, _buzzEventHandler)
                      .Then(_fizzBuzzEventHandler);
            _ringBuffer = _disruptor.GetRingBuffer;
        }
        public Pipeline3StepDisruptorWithAffinityPerfTest()
            : base(100 * Million)
        {
            _scheduler = new RoundRobinThreadAffinedTaskScheduler(4);
            _disruptor = new Disruptor<FunctionEvent>(() => new FunctionEvent(),
                                                      new SingleThreadedClaimStrategy(Size),
                                                      new YieldingWaitStrategy(),
                                                      _scheduler);

            _mru = new ManualResetEvent(false);
            _stepThreeFunctionEventHandler = new FunctionEventHandler(FunctionStep.Three, Iterations, _mru);

            _disruptor.HandleEventsWith(new FunctionEventHandler(FunctionStep.One, Iterations, _mru))
                .Then(new FunctionEventHandler(FunctionStep.Two, Iterations, _mru))
                .Then(_stepThreeFunctionEventHandler);

            _ringBuffer = _disruptor.RingBuffer;
        }
        public Sequencer3P1CDisruptorWithAffinityPerfTest()
            : base(20 * Million)
        {
            _scheduler = new RoundRobinThreadAffinedTaskScheduler(4);
            _disruptor = new Disruptor<ValueEvent>(() => new ValueEvent(), Size, TaskScheduler.Default, ProducerType.MULTI, new YieldingWaitStrategy());

            _mru = new ManualResetEvent(false);
            _eventHandler = new ValueAdditionEventHandler(Iterations * NumProducers, _mru);
            _disruptor.HandleEventsWith(_eventHandler);

            _valueProducers = new ValueProducer[NumProducers];
            _ringBuffer = _disruptor.GetRingBuffer;

            for (int i = 0; i < NumProducers; i++)
            {
                _valueProducers[i] = new ValueProducer(_testStartBarrier, _ringBuffer, Iterations);
            }
        }
        protected override long RunDisruptorPass()
        {
            var ob = Observable.Create<long>(o =>
            {
                var cancel = new CancellationDisposable(); // internally creates a new CancellationTokenSource
               
                Scheduler.Default.Schedule(() =>
                {
                    for (long i = 0; i < ITERATIONS; i++)
                    {
                        //Thread.Sleep(200);  // here we do the long lasting background operation
                        if (!cancel.Token.IsCancellationRequested)    // check cancel token periodically
                            o.OnNext(i++);
                        else
                        {
                            Console.WriteLine("Aborting because cancel event was signaled!");
                            o.OnCompleted();
                            return;
                        }
                    }
                }
                );

                return cancel;
            }
             );

            var spinwait = default(SpinWait);
            //var latch = new CountdownEvent(1);
            var latch = new ManualResetEvent(false);
            var expectedCount = myRunnable.GetSequence.Value + ITERATIONS;
            myRunnable.Reset(latch, expectedCount);
            var _scheduler = new RoundRobinThreadAffinedTaskScheduler(4);
            //TaskScheduler.Default调度器 CPU 约在 50 % 两个繁忙,两个空闲
            //
           
            Task.Factory.StartNew(myRunnable.Run, CancellationToken.None, TaskCreationOptions.None,TestTaskScheduler);

            var start = Stopwatch.StartNew();

            var sequencer = this.sequencer;
            //var range = Observable.Range(0, 1000 * 1000 * 100, Scheduler.Default)
            //       .Subscribe(i => {
            //           long next = sequencer.Next();
            //           sequencer.Publish(next);
            //       });
           // var subscription = ob.Subscribe(i =>
            for (long i = 0; i < ITERATIONS; i++)
            {
                long next = sequencer.Next();
                sequencer.Publish(next);
            }
           //);

            latch.WaitOne();
            long opsPerSecond = (ITERATIONS * 1000L) / start.ElapsedMilliseconds;

            WaitForEventProcessorSequence(expectedCount, spinwait,myRunnable);

            return opsPerSecond;
        }
 public OneToOneRawThroughputTest()
 {
     _taskScheduler = new RoundRobinThreadAffinedTaskScheduler(2);
     _myRunnable = new MyRunnable(_sequencer);
     _sequencer.AddGatingSequences(_myRunnable.Sequence);
 }