Exemple #1
0
    public static void Main()
    {
        // Specify the size of the ring buffer, must be power of 2.
        const int bufferSize = 1024;

        // Construct the Disruptor
        var disruptor = new Dsl.Disruptor <SampleEvent>(() => new SampleEvent(), bufferSize);

        // Connect the handler
        disruptor.HandleEventsWith(new SampleEventHandler());

        // Start the Disruptor, starts all threads running
        disruptor.Start();

        // Get the ring buffer from the Disruptor to be used for publishing.
        var ringBuffer = disruptor.RingBuffer;

        var producer = new SampleEventProducer(ringBuffer);
        var memory   = new Memory <byte>(new byte[12]);

        for (var i = 0; ; i++)
        {
            MemoryMarshal.Write(memory.Span, ref i);

            producer.ProduceUsingRawApi(memory);

            Thread.Sleep(1000);
        }
    }
        public UniCast1P1CDisruptorPerfTest()
            : base(100 * Million)
        {
            _disruptor = new Dsl.Disruptor <ValueEvent>(() => new ValueEvent(),
                                                        new SingleThreadedClaimStrategy(BufferSize),
                                                        new YieldingWaitStrategy());

            _mru          = new ManualResetEvent(false);
            _eventHandler = new ValueAdditionEventHandler(Iterations, _mru);
            _disruptor.HandleEventsWith(_eventHandler);
            _ringBuffer = _disruptor.RingBuffer;
        }
        public UniCast1P1CDisruptorPerfTest()
            : base(100 * Million)
        {
            _disruptor = new Dsl.Disruptor<ValueEvent>(() => new ValueEvent(),
                                                          new SingleThreadedClaimStrategy(BufferSize),
                                                          new YieldingWaitStrategy());

            _mru = new ManualResetEvent(false);
            _eventHandler = new ValueAdditionEventHandler(Iterations, _mru);
            _disruptor.HandleEventsWith(_eventHandler);
            _ringBuffer = _disruptor.RingBuffer;
        }
Exemple #4
0
        public MultiCast1P3CDisruptorPerfTest()
            : base(100 * Million)
        {
            _disruptor = new Dsl.Disruptor <ValueEvent>(() => new ValueEvent(),
                                                        new SingleThreadedClaimStrategy(Size),
                                                        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.RingBuffer;
        }
        public MultiCast1P3CDisruptorPerfTest()
            : base(100 * Million)
        {
            _disruptor = new Dsl.Disruptor<ValueEvent>(() => new ValueEvent(),
                                                      new SingleThreadedClaimStrategy(Size),
                                                      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.RingBuffer;
        }
        public Pipeline3StepLatencyDisruptorPerfTest()
            : base(2 * Million)
        {
            _disruptor = new Dsl.Disruptor <ValueEvent>(() => new ValueEvent(),
                                                        new SingleThreadedClaimStrategy(Size),
                                                        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.RingBuffer;
        }
        public Pipeline3StepLatencyDisruptorPerfTest()
            : base(2 * Million)
        {
            _disruptor = new Dsl.Disruptor<ValueEvent>(() => new ValueEvent(),
                                                   new SingleThreadedClaimStrategy(Size),
                                                   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.RingBuffer;
        }
        public long RunPass(int PassNumber)
        {
            InitRingQueue();

            var sw = Stopwatch.StartNew();

            Thread.MemoryBarrier();

            /*
             * for (int i = 0; i < MAX_MSG_COUNT; i++)
             * {
             *  var sequence = _ringBuffer.Next();
             *  _ringBuffer[sequence].Value = i;
             *  _ringBuffer.Publish(sequence);
             *  //Console.WriteLine("RunPass(): sequence = {0}, {1}, {2}.", MAX_MSG_COUNT, i, sequence);
             * }
             * //*/

            for (int i = 0; i < PUSH_CNT; i++)
            {
                _producerThreads[i].Join();
            }

            //Thread.MemoryBarrier();
            //Thread.Sleep(100);

            Thread.MemoryBarrier();

            /*
             * for (int i = 0; i < POP_CNT; i++)
             * {
             *  for (int j = 0; j < POP_CNT; j++)
             *  {
             *      try
             *      {
             *          _latchs[i].Signal();
             *      }
             *      catch (Exception ex) { }
             *  }
             * }
             * Thread.MemoryBarrier();*
             * //*/

            for (int i = 0; i < POP_CNT; i++)
            {
                //_latchs.Wait();
            }

            //Thread.MemoryBarrier();

            /*
             * _workerPool.DrainAndHalt();
             *
             * Thread.MemoryBarrier();
             *
             * for (int i = 0; i < POP_CNT; i++)
             * {
             *  var lifecycleAware = _workHandlers[i] as ILifecycleAware;
             *  if (lifecycleAware != null)
             *      lifecycleAware.OnShutdown();
             * }
             * //*/

            /*
             * while (!_remainCount.AtomicCompareExchange(-1, 0))
             * {
             *  // Do nothing!
             *  Thread.Yield();
             * }
             * //*/

            Thread.MemoryBarrier();

            _disruptor.Shutdown();

            //Thread.Sleep(200);

            long opsPerSecond;

            if (sw.ElapsedMilliseconds != 0)
            {
                opsPerSecond = (MAX_MSG_COUNT * 1000L) / sw.ElapsedMilliseconds;
            }
            else
            {
                opsPerSecond = 0L;
            }

            Console.WriteLine("Run = {0}, Time = {1} ms, Disruptor = {2:###,###,###,###} ops/sec.\n",
                              PassNumber, sw.ElapsedMilliseconds, (long)opsPerSecond);

            /*
             * try
             * {
             *  for (int i = 0; i < POP_CNT; i++)
             *  {
             *      if (i == 0)
             *          Assert.AreEqual(ExpectedResults[0], _eventHandlers[0].Value, "Addition");
             *      else if (i == 1)
             *          Assert.AreEqual(ExpectedResults[1], _eventHandlers[1].Value, "Sub");
             *      else if (i == 2)
             *          Assert.AreEqual(ExpectedResults[2], _eventHandlers[2].Value, "And");
             *      else
             *          Assert.AreEqual(ExpectedResults[i], _eventHandlers[i].Value, "And");
             *  }
             * }
             * catch (Exception ex)
             * {
             *  //Console.WriteLine(ex.Message.ToString());
             *  Console.WriteLine(ex.ToString());
             *  //Console.ReadKey();
             * }
             * finally
             * {
             *  //
             * }
             * //*/

            long totals = 0;

            for (int i = 0; i < POP_CNT; i++)
            {
                Console.WriteLine("counters({0}) = {1}", i, _counters[i].ReadUnfenced());
                totals += _counters[i].ReadUnfenced();
            }
            Console.WriteLine("\ntotals = {0} messages.\n", totals);

            _workerPool.Halt();
            for (int i = 0; i < POP_CNT; i++)
            {
                _workHandlers[i] = null;
            }
            _workerPool = null;

            if (_disruptor != null)
            {
                _disruptor.Shutdown();
                _disruptor = null;
            }

            _ringBuffer = null;

            System.GC.Collect();

            return(opsPerSecond);
        }
        public void InitRingQueue()
        {
            if (_disruptor == null)
            {
                if (PUSH_CNT <= 1)
                {
                    _disruptor = new Dsl.Disruptor <MessageEvent>(
                        () => new MessageEvent(),
                        new SingleThreadedClaimStrategy(BUFFER_SIZE),
                        //new MultiThreadedClaimStrategy(BUFFER_SIZE),
                        //new BusySpinWaitStrategy(),
                        new YieldingWaitStrategy(),
                        //new SleepingWaitStrategy(),
                        TaskScheduler.Default);
                }
                else
                {
                    _disruptor = new Dsl.Disruptor <MessageEvent>(
                        () => new MessageEvent(),
                        //new SingleThreadedClaimStrategy(BUFFER_SIZE),
                        new MultiThreadedClaimStrategy(BUFFER_SIZE),
                        //new BusySpinWaitStrategy(),
                        new YieldingWaitStrategy(),
                        //new BetterYieldingWaitStrategy(),
                        //new SleepingWaitStrategy(),
                        TaskScheduler.Default);
                }

                _latchs = new CountdownEvent(POP_CNT);

                _eventHandlers = new ValueMutationEventHandler[POP_CNT];

                for (int i = 0; i < POP_CNT; i++)
                {
                    if (i == 0)
                    {
                        _eventHandlers[0] = new ValueMutationEventHandler(0, Operation.Addition, MAX_MSG_COUNT, _latchs);
                    }
                    else if (i == 1)
                    {
                        _eventHandlers[1] = new ValueMutationEventHandler(1, Operation.Substraction, MAX_MSG_COUNT, _latchs);
                    }
                    else if (i == 2)
                    {
                        _eventHandlers[2] = new ValueMutationEventHandler(2, Operation.And, MAX_MSG_COUNT, _latchs);
                    }
                    else
                    {
                        _eventHandlers[i] = new ValueMutationEventHandler(i, Operation.And, MAX_MSG_COUNT, _latchs);
                    }
                }

                _workHandlers = new CountingWorkHandler[POP_CNT];
                for (int i = 0; i < POP_CNT; i++)
                {
                    _workHandlers[i] = new CountingWorkHandler(i, POP_MSG_CNT, _counters, _latchs);
                }

                _workProcessors = new CountingWorkProcessor[POP_CNT];
                for (int i = 0; i < POP_CNT; i++)
                {
                    _workProcessors[i] = new CountingWorkProcessor(i, POP_MSG_CNT, _counters, _latchs);
                }

                //_disruptor.HandleEventsWith(_eventHandlers);
                //_disruptor.HandleEventsWith(_workProcessors);

                _ringBuffer = _disruptor.RingBuffer;

                ///*
                _workerPool = new WorkerPool <MessageEvent>(_ringBuffer,
                                                            _ringBuffer.NewBarrier(),
                                                            new FatalExceptionHandler(),
                                                            _workHandlers);
                //*/

                ///*
                _ringBuffer.SetGatingSequences(_workerPool.WorkerSequences);

                for (int i = 0; i < POP_CNT; i++)
                {
                    _workHandlers[i].SetRingBuffer(_ringBuffer);
                    _workProcessors[i].SetRingBuffer(_ringBuffer);
                }
                //*/

                ResetEvents();
                ResetCounters();

                _producerThreads = new Thread[PUSH_CNT];
                for (int i = 0; i < PUSH_CNT; i++)
                {
                    Producer producer = new Producer(i, PUSH_MSG_CNT, 0, _ringBuffer);
                    producer.Reset();
                    _producerThreads[i] = new Thread(new ThreadStart(producer.Run));
                }

                Thread.MemoryBarrier();

                for (int i = 0; i < PUSH_CNT; i++)
                {
                    _producerThreads[i].Start();
                }

                _remainCount.AtomicExchange(MAX_MSG_COUNT);

                Thread.MemoryBarrier();
                _workerPool.Start(TaskScheduler.Default);

                //_disruptor.Start();
            }
        }