Exemplo n.º 1
0
 public void SimpleInitShutdownTest(int count)
 {
     for (int i = 0; i < count; i++)
     {
         MultiEventLoop.Initialize();
         MultiEventLoop.Shutdown();
     }
 }
Exemplo n.º 2
0
        public void SimpleMultiEventLoopBenchmark()
        {
            const int TestSize = 30_000_000;

            MultiEventLoop.Initialize();
            try
            {
                // Warm-up
                var t = Task.Factory.StartNew(
                    () => { },
                    CancellationToken.None,
                    TaskCreationOptions.None,
                    MultiEventLoop.Scheduler);
                t.Wait();

                Stopwatch stopwatch = Stopwatch.StartNew();

                t = Task.Factory.StartNew(
                    () => EmptyLoop(TestSize),
                    CancellationToken.None,
                    TaskCreationOptions.None,
                    MultiEventLoop.Scheduler).Unwrap();
                t.Wait();

                stopwatch.Stop();
                double singleCoreSpeed = (TestSize * 1000.0) / stopwatch.ElapsedMilliseconds;
                TestContext.WriteLine($"single-thread:{singleCoreSpeed / 1000_000.0:F4} millions/sec");

                var tasks = new List <Task>();

                stopwatch = Stopwatch.StartNew();

                for (int i = 0; i < Environment.ProcessorCount; i++)
                {
                    t = Task.Factory.StartNew(
                        () => EmptyLoop(TestSize),
                        CancellationToken.None,
                        TaskCreationOptions.None,
                        MultiEventLoop.Scheduler).Unwrap();
                    tasks.Add(t);
                }

                foreach (var tsk in tasks)
                {
                    tsk.Wait();
                }

                stopwatch.Stop();
                double multiCoreSpeed = ((long)TestSize * tasks.Count * 1000.0) / stopwatch.ElapsedMilliseconds;
                TestContext.WriteLine(
                    $"{tasks.Count}-thread: {multiCoreSpeed / 1000_000.0:F4} millions/sec scalability={(multiCoreSpeed / (singleCoreSpeed * tasks.Count)) * 100.0:F3}%");
            }
            finally
            {
                MultiEventLoop.Shutdown();
            }
        }
Exemplo n.º 3
0
        private async Task EmptyLoop(int count)
        {
            var yieldAwaitable = MultiEventLoop.Yield();

            for (int i = 0; i < count; i++)
            {
                // await Task.Yield();
                await yieldAwaitable;
            }

            // ------- Use this trick to get totally unlimited calculation power, without blocking other tasks.
            ////var eventLoop = MultiEventLoop.CurrentEventLoop;
            ////for (int i = 0; i < count; i++)
            ////{
            ////    // ReSharper disable once PossibleNullReferenceException
            ////    eventLoop.DoEventsUnsafe((i & 0xFFFF) == 0);
            ////}
        }