Exemplo n.º 1
0
        public async Task MNATest()
        {
            var sm    = new MutableSeries <int, int>();
            var count = 1_0;
            var sum   = 0;

            using (Benchmark.Run("MNA"))
            {
                var _ = Task.Run(() =>
                {
                    for (int i = 0; i < count; i++)
                    {
                        sm.TryAppend(i, i);
                    }
                    sm.MarkReadOnly();
                });

                var c = sm.GetAsyncCursor();
                while (await c.MoveNextAsync())
                {
                    sum += c.CurrentValue;
                }
            }
            Assert.IsTrue(sum > 0);

            Benchmark.Dump();
        }
Exemplo n.º 2
0
        public bool AddLast(TKey key, DataBlock value)
        {
            // var wr = new WeakReference<DataBlock>(value);
            DataBlock?lastBlock = null;
            var       last      = _blockSeries.Last;

            if (last.IsPresent)
            {
                lastBlock = last.Present.Value;
                // Console.WriteLine($"LAST: {last.Present.Key} adding: {key}");
                //if (!last.Present.Value.TryGetTarget(out lastBlock))
                //{
                //    // Console.WriteLine("CANNOT GET LAST");
                //    // TODO cleanup
                //    // Assert that all do not have target
                //}
            }
            else
            {
                // Console.WriteLine("Cannot get last: " + key);
                // last = _weakSeries.Last;
            }

            var added = _blockSeries.TryAppend(key, value);

            if (!added)
            {
                // ThrowHelper.ThrowInvalidOperationException("This should always succeed");
                return(false);
            }

            if (lastBlock != null)
            {
                lastBlock.NextBlock = value;
                value.PreviousBlock = lastBlock;
            }
            else
            {
                // Console.WriteLine("SETTING ROOT");
                _root = value;
            }
            // GC.KeepAlive(value);
            _last = value;
            return(true);
        }
Exemplo n.º 3
0
        public void SortedMapNotifierTest()
        {
            var rounds = 100_000;

            for (int r = 0; r < rounds; r++)
            {
                var count = 1_000_000;
                var cnt1  = 0;
                var cnt2  = 0;
                var cnt3  = 0;
                var cnt4  = 0;

                var sm1 = new MutableSeries <int, int>();
                // sm1._isSynchronized = false;
                var addTask = Task.Run(async() =>
                {
                    // await Task.Delay(5000);
                    try
                    {
                        // sm1.TryAddLast(0, 0);
                        for (int i = 0; i < count; i++)
                        {
                            if (i != 2)
                            {
                                sm1.TryAppend(i, i);
                                Thread.SpinWait(5);

                                //if (i % 250000 == 0)
                                //{
                                //    GC.Collect(0, GCCollectionMode.Forced, false);
                                //}
                            }
                        }

                        sm1.MarkReadOnly();
                        //Console.WriteLine("cnt1: " + cnt1);
                        //Console.WriteLine("cnt2: " + cnt2);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex);
                    }
                });

                // addTask.Wait();
                using (Benchmark.Run("SM.Updated", count * 5))
                {
                    var t1 = Task.Run(async() =>
                    {
                        Thread.CurrentThread.Name = "MNA1";
                        try
                        {
                            using (var cursor1 = sm1.GetAsyncCursor())
                            {
                                // Console.WriteLine("MNA1 started");
                                while (await cursor1.MoveNextAsync())
                                {
                                    AsyncCursor.LogFinished();
                                    if (cnt1 == 2)
                                    {
                                        cnt1++;
                                        // Console.WriteLine("MNA1 moving");
                                    }

                                    if (cursor1.CurrentKey != cnt1)
                                    {
                                        ThrowHelper.ThrowInvalidOperationException("Wrong cursor enumeration");
                                    }

                                    cnt1++;
                                    //if (c % 250000 == 0)
                                    //{
                                    //    GC.Collect(0, GCCollectionMode.Forced, false);
                                    //    Console.WriteLine(c);
                                    //}
                                }

                                if (cnt1 != count)
                                {
                                    ThrowHelper.ThrowInvalidOperationException($"1 Cannot move to count: c={cnt1}, count={count}");
                                }

                                if (AsyncCursor.SyncCount == 0)
                                {
                                    Console.WriteLine("SyncCount == 0");
                                }

                                Thread.MemoryBarrier();
                            }
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine("MNA1 ex: " + e);
                        }
                    });

                    var t2 = Task.Run(async() =>
                    {
                        Thread.CurrentThread.Name = "MNA2";
                        try
                        {
                            using (var cursor2 = sm1.GetAsyncCursor())
                            {
                                // Console.WriteLine("MNA2 started");
                                while (await cursor2.MoveNextAsync())
                                {
                                    AsyncCursor.LogFinished();
                                    if (cnt2 == 2)
                                    {
                                        cnt2++;
                                        // Console.WriteLine("MNA2 moving");
                                    }

                                    if (cursor2.CurrentKey != cnt2)
                                    {
                                        ThrowHelper.ThrowInvalidOperationException("Wrong cursor enumeration");
                                    }

                                    cnt2++;
                                }

                                if (cnt2 != count)
                                {
                                    ThrowHelper.ThrowInvalidOperationException($"2 Cannot move to count: c={cnt2}, count={count}");
                                }

                                if (AsyncCursor.SyncCount == 0)
                                {
                                    Console.WriteLine("SyncCount == 0");
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine("MNA2 ex: " + e);
                        }
                    });

                    var t3 = Task.Run(async() =>
                    {
                        Thread.CurrentThread.Name = "MNA3";
                        try
                        {
                            using (var cursor3 = sm1.GetAsyncCursor())
                            {
                                // Console.WriteLine("MNA2 started");
                                while (await cursor3.MoveNextAsync())
                                {
                                    AsyncCursor.LogFinished();
                                    if (cnt3 == 2)
                                    {
                                        cnt3++;
                                        // Console.WriteLine("MNA2 moving");
                                    }

                                    if (cursor3.CurrentKey != cnt3)
                                    {
                                        ThrowHelper.ThrowInvalidOperationException("Wrong cursor enumeration");
                                    }

                                    cnt3++;
                                }

                                if (cnt3 != count)
                                {
                                    ThrowHelper.ThrowInvalidOperationException($"3 Cannot move to count: c={cnt3}, count={count}");
                                }

                                if (AsyncCursor.SyncCount == 0)
                                {
                                    Console.WriteLine("SyncCount == 0");
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine("MNA3 ex: " + e);
                        }
                    });

                    var t4 = Task.Run(async() =>
                    {
                        Thread.CurrentThread.Name = "MNA4";
                        try
                        {
                            using (var cursor4 = sm1.GetAsyncCursor())
                            {
                                // Console.WriteLine("MNA2 started");
                                while (await cursor4.MoveNextAsync())
                                {
                                    AsyncCursor.LogFinished();
                                    if (cnt4 == 2)
                                    {
                                        cnt4++;
                                        // Console.WriteLine("MNA2 moving");
                                    }

                                    if (cursor4.CurrentKey != cnt4)
                                    {
                                        ThrowHelper.ThrowInvalidOperationException("Wrong cursor enumeration");
                                    }

                                    cnt4++;
                                }

                                if (cnt4 != count)
                                {
                                    ThrowHelper.ThrowInvalidOperationException($"4 Cannot move to count: c={cnt4}, count={count}");
                                }

                                if (AsyncCursor.SyncCount == 0)
                                {
                                    Console.WriteLine("SyncCount == 0");
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine("MNA3 ex: " + e);
                        }
                    });

                    var finished = false;
                    while (!finished)
                    {
                        finished = Task.WhenAll(addTask, t1, t2, t3, t4).Wait(2000);
                        //Console.WriteLine("cnt1: " + cnt1);
                        //Console.WriteLine("cnt2: " + cnt2);
                    }
                    Console.WriteLine($"{r}: Sync: {AsyncCursor.SyncCount}, Async: {AsyncCursor.AsyncCount}, Await: {AsyncCursor.AwaitCount}, Skipped: {AsyncCursor.SkippedCount}, Missed: {AsyncCursor.MissedCount}, Finished: {AsyncCursor.FinishedCount}");
                    AsyncCursor.ResetCounters();
                }
            }
            Benchmark.Dump();
        }