Пример #1
0
        protected override long RunDisruptorPass()
        {
            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, _scheduler);

            var start = Stopwatch.StartNew();

            var sequencer = this.sequencer;

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

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

            WaitForEventProcessorSequence(expectedCount, spinwait, myRunnable);

            return(opsPerSecond);
        }
        protected override long RunDisruptorPass()
        {
            int            batchSize     = 10;
            CountdownEvent latch         = new CountdownEvent(1);
            long           expectedCount = myRunnable.GetSequence.Value + (ITERATIONS * batchSize);

            myRunnable.Reset(latch, expectedCount);
            Task.Factory.StartNew(() => myRunnable.Run());
            Stopwatch watch = Stopwatch.StartNew();

            ISequenced sequencer = this.sequencer;

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

            latch.Wait();

            long opsPerSecond = (ITERATIONS * 1000L * batchSize) / (watch.ElapsedMilliseconds);

            waitForEventProcessorSequence(expectedCount);

            return(opsPerSecond);
        }