Beispiel #1
0
        public void PriorityChannelBenchmarkReadFirst()
        {
            var count = 100_000_000;
            var pc    = new PriorityChannel <long>();

            var rt = Task.Run(() =>
            {
                using (Benchmark.Run("Read", count, true))
                {
                    var c = 0;

                    while (c < count)
                    {
                        pc.TryTake(out var i, out var isPriority);
                        //if (i >> 3 == 0 && !isPriority)
                        //{
                        //    Assert.Fail("i >> 8 == 0 && !isPriority");
                        //}

                        c++;
                    }
                }
            });

            Thread.Sleep(100);

            var wt = Task.Run(() =>
            {
                using (Benchmark.Run("Write", count, true))
                {
                    for (var i = 0; i < count; i++)
                    {
                        pc.TryAdd(i); //, i >> 3 == 0);
                    }
                }
            });

            rt.Wait();

            wt.Wait();

            Benchmark.Dump();
        }
Beispiel #2
0
        public void PriorityChannelBenchmarkWriteThenReadNoWait()
        {
            var count = 200_000_000;
            var pc    = new PriorityChannel <ushort>();

            var wt = Task.Run(() =>
            {
                using (Benchmark.Run("Write", count, true))
                {
                    for (var i = 0; i < count; i++)
                    {
                        pc.TryAdd((ushort)(i % ushort.MaxValue)); //, i >> 3 == 0);
                    }
                }
            });

            wt.Wait();

            var rt = Task.Run(() =>
            {
                using (Benchmark.Run("Read", count, true))
                {
                    var c = 0;

                    while (c < count)
                    {
                        pc.TryTake(out var i, out var isPriority);
                        //if (i >> 3 == 0 && !isPriority)
                        //{
                        //    Assert.Fail("i >> 8 == 0 && !isPriority");
                        //}

                        c++;
                    }
                }
            });

            rt.Wait();

            Benchmark.Dump();
        }
Beispiel #3
0
        public void PriorityChannelBenchmarkSlowWriterTryThenBlock()
        {
            var count = 1_000_000;
            var cts   = new CancellationTokenSource();
            var pc    = new PriorityChannel <int>(cts.Token);

            var wt = Task.Run(() =>
            {
                using (Benchmark.Run("Write", count, true))
                {
                    for (var i = 0; i < count; i++)
                    {
                        var priority = i % 2 == 0;
                        pc.TryAdd(i, priority);
                        if (i % 10 == 0)
                        {
                            if (!Thread.Yield())
                            {
                                Thread.Sleep(0);
                            }
                        }
                        else
                        {
                            Thread.SpinWait(10);
                        }
                    }
                }
            });

            var rt = Task.Run(() =>
            {
                using (Benchmark.Run("Read", count, true))
                {
                    var c       = 0;
                    var spinner = new SpinWait();
                    while (c < count)
                    {
                        if (!pc.TryTake(out var i, out var isPriority))
                        {
                            if (!pc.TryTake(out i, out isPriority))
                            {
                                // still no new notifications, retry toggle after some wait
                                spinner.SpinOnce();
                                if (spinner.NextSpinWillYield)
                                {
                                    spinner.Reset();
                                    if (!Thread.Yield())
                                    {
                                        Thread.Sleep(0);
                                    }
                                }
                                continue;
                            }
                        }

                        //if (i >> 3 == 0 && !isPriority)
                        //{
                        //    Assert.Fail("i >> 8 == 0 && !isPriority");
                        //}

                        c++;
                    }
                }
            });

            rt.Wait();

            wt.Wait();

            Benchmark.Dump();
        }