Example #1
0
        public void CouldConsumeMapAsync()
        {
            var count  = 10;
            var sm     = new SortedMap <int, int>();
            var zipMap = sm.Map(x => x * x).Zip(sm.Map(x => x * x), (l, r) => l + r);

            var t1 = Task.Run(async() =>
            {
                for (int i = 0; i < count; i++)
                {
                    await Task.Delay(250);
                    var _ = await sm.TryAdd(i, i);
                }
                await sm.Complete();
            });
            var cnt = 0;
            var t2  = Task.Run(async() =>
            {
                var c = zipMap.GetAsyncCursor();

                while (await c.MoveNextAsync())
                {
                    cnt++;
                    Console.WriteLine($"{c.CurrentKey} - {c.CurrentValue}");
                }
            });

            Task.WhenAll(t1, t2).Wait();
            Assert.AreEqual(count, cnt);
        }
Example #2
0
        public async Task AddSpeed()
        {
            const int count = 5_000_000;

            for (int r = 0; r < 10; r++)
            {
                var sl  = new SortedList <int, int>();
                var sm  = new SortedMap <int, int>();
                var scm = new SortedChunkedMap <int, int>();

                var c = sm.GetCursor();

                sm._isSynchronized  = false;
                scm._isSynchronized = false;

                using (Benchmark.Run("SL", count))
                {
                    for (int i = 0; i < count; i++)
                    {
                        if (i != 2)
                        {
                            sl.Add(i, i);
                        }
                    }
                }

                using (Benchmark.Run("SM", count))
                {
                    for (int i = 0; i < count; i++)
                    {
                        if (i != 2)
                        {
                            await sm.TryAdd(i, i);
                        }
                    }
                }

                using (Benchmark.Run("SCM", count))
                {
                    for (int i = 0; i < count; i++)
                    {
                        if (i != 2)
                        {
                            await scm.TryAdd(i, i);
                        }
                    }
                }
                scm.Dispose();
            }

            Benchmark.Dump();
        }
Example #3
0
        public async Task CouldEnumerateSMUsingCursor()
        {
            var map   = new SortedMap <int, int>();
            var count = 1_000_000;

            for (int i = 0; i < count; i++)
            {
                await map.TryAdd(i, i);
            }

#pragma warning disable HAA0401 // Possible allocation of reference type enumerator
            var ae = map.GetAsyncEnumerator();
#pragma warning restore HAA0401 // Possible allocation of reference type enumerator

            var t = Task.Run(async() =>
            {
                using (Benchmark.Run("SCM.AsyncEnumerator", count))
                {
                    var cnt = 0;
                    while (await ae.MoveNextAsync())
                    {
                        cnt++;
                    }

                    await ae.DisposeAsync();
                    Assert.AreEqual(count * 2, cnt);
                }

                Benchmark.Dump();
            });

            for (int i = count; i < count * 2; i++)
            {
                await map.TryAdd(i, i);
            }
            await map.Complete();

            t.Wait();
        }
Example #4
0
        public async Task NotificationWorksWhenCursorIsNotWating()
        {
            var sm = new SortedMap <int, int>();

            sm.Add(1, 1);
            sm.Add(2, 2);
            // sm.Flush();

            var cursor = sm.GetAsyncEnumerator();

            Assert.True(await cursor.MoveNextAsync());
            Assert.True(await cursor.MoveNextAsync());

            Assert.IsTrue(cursor.MovePrevious());

            await sm.TryAdd(3, 3);

            await Task.Delay(250);

            Assert.AreEqual(1, cursor.CurrentKey);
            Assert.AreEqual(1, cursor.CurrentValue);
        }
Example #5
0
        public async Task CouldEnumerateSCMUsingCursor()
        {
            Settings.SCMDefaultChunkLength = Settings.SCMDefaultChunkLength * 4;
            var scm   = new SortedMap <int, int>();
            var count = 1_000_000; // Settings.SCMDefaultChunkLength - 1;

            for (int i = 0; i < count; i++)
            {
                await scm.TryAdd(i, i);
            }

            Console.WriteLine("Added first half");

#pragma warning disable HAA0401 // Possible allocation of reference type enumerator
            var ae = scm.GetAsyncEnumerator();
#pragma warning restore HAA0401 // Possible allocation of reference type enumerator

            var t = Task.Run(async() =>
            {
                using (Benchmark.Run("SCM.AsyncEnumerator", count))
                {
                    try
                    {
                        var cnt = 0;
                        while (await ae.MoveNextAsync())
                        {
                            if (cnt != ae.Current.Key)
                            {
                                ThrowHelper.ThrowInvalidOperationException();
                            }

                            cnt++;
                        }

                        await ae.DisposeAsync();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("EXCEPTION: " + ex.ToString());
                        throw;
                    }

                    // Assert.AreEqual(scm.Count, cnt);
                }

                Benchmark.Dump();
            });

            // Thread.Sleep(1000);

            for (int i = count; i < count * 2; i++)
            {
                await scm.TryAdd(i, i);

                //Thread.SpinWait(50);
            }

            // Thread.Sleep(2000);

            await scm.Complete();

            t.Wait();
        }