Ejemplo n.º 1
0
        public void CouldNotMoveNextPreviousOnEmpty()
        {
            var s = new AppendSeries <int, int>();
            var c = s.GetCursor();

            Assert.IsFalse(c.MoveNext());
            Assert.IsFalse(c.MovePrevious());
            Assert.IsFalse(c.MoveFirst());
            Assert.IsFalse(c.MoveLast());
            s.Append(1, 1);
            Assert.IsTrue(c.MoveNext());
            Assert.IsFalse(c.MovePrevious());
            Assert.IsTrue(c.MoveFirst());
            Assert.IsTrue(c.MoveLast());

            s.Append(2, 2);
            Assert.IsTrue(c.MoveNext());
            Assert.AreEqual(2, c.CurrentKey);
            Assert.AreEqual(2, c.CurrentValue);
            Assert.IsTrue(c.MovePrevious());
            Assert.AreEqual(1, c.CurrentKey);
            Assert.AreEqual(1, c.CurrentValue);
            Assert.IsTrue(c.MoveFirst());
            Assert.IsTrue(c.MoveLast());

            c.Dispose();
            s.Dispose();
        }
Ejemplo n.º 2
0
        public void CouldMoveAtOnNonEmpty(int count)
        {
            Assert.Fail("Need a proper MA test");
            var s = new AppendSeries <int, int>();

            for (int i = 1; i <= count; i++)
            {
                s.Append(i, i);
            }

            var searchValue = count / 2;

            // TODO all corners all directions and all inner

            var c = s.GetCursor();

            Assert.IsTrue(c.MoveAt(searchValue, Lookup.GT));
            Assert.AreEqual(searchValue + 1, c.CurrentValue);
            Assert.AreEqual(searchValue + 1, c.CurrentKey);

            Assert.IsTrue(c.MoveAt(searchValue, Lookup.GE));
            Assert.IsTrue(c.MoveAt(searchValue, Lookup.EQ));
            Assert.IsTrue(c.MoveAt(searchValue, Lookup.LE));
            Assert.IsTrue(c.MoveAt(searchValue, Lookup.LT));
            c.Dispose();
            s.Dispose();
        }
Ejemplo n.º 3
0
        public void CouldMoveFirstLast(int count)
        {
            var s = new AppendSeries <int, int>();

            for (int i = 1; i <= count; i++)
            {
                s.Append(i, i);
            }
            SCursor <int, int> c;

            c = s.GetCursor();
            Assert.IsTrue(c.MoveFirst());
            Assert.IsTrue(c.MoveLast());
            c.Dispose();

            c = s.GetCursor();
            Assert.IsTrue(c.MoveLast());
            Assert.IsTrue(c.MoveFirst());
            c.Dispose();

            s.Dispose();
        }
Ejemplo n.º 4
0
        public void CouldMoveAtOnSingle()
        {
            Assert.Fail("Proper MA tests");
            var s = new AppendSeries <int, int>();

            s.Append(1, 1);
            var c = s.GetCursor();

            Assert.IsFalse(c.MoveAt(1, Lookup.GT));
            Assert.IsTrue(c.MoveAt(1, Lookup.GE));
            Assert.IsTrue(c.MoveAt(1, Lookup.EQ));
            Assert.IsTrue(c.MoveAt(1, Lookup.LE));
            Assert.IsFalse(c.MoveAt(1, Lookup.LT));
            c.Dispose();
            s.Dispose();
        }
Ejemplo n.º 5
0
        public void CouldMovePreviousFromInitialized(int count)
        {
            var s = new AppendSeries <int, int>();

            for (int i = 1; i <= count; i++)
            {
                s.Append(i, i);
            }

            var c = s.GetCursor();

            Assert.IsTrue(c.MovePrevious());
            Assert.AreEqual(count, c.CurrentKey);
            Assert.AreEqual(count, c.CurrentValue);

            c.Dispose();
            s.Dispose();
        }
Ejemplo n.º 6
0
        public async Task CouldReadDataStreamWhileWritingFromManyThreads()
        {
            var map = new AppendSeries <int, int>();

            var writeCount = 0L;

            var count  = 1000_000_000;
            var rounds = 1;

            var writeTask = Task.Run(async() =>
            {
                using (Benchmark.Run("Write", count * rounds, true))
                {
                    for (int j = 0; j < rounds; j++)
                    {
                        var t1 = Task.Run(() =>
                        {
                            try
                            {
                                for (int i = j * count; i < (j + 1) * count; i++)
                                {
                                    map.DangerousTryAppend(i, i);
                                    // Thread.SpinWait(10);
                                    // Thread.Yield();
                                    Interlocked.Increment(ref writeCount);
                                }
                            }
                            catch (Exception e)
                            {
                                Console.WriteLine(e);
                                throw;
                            }
                        });
                        await t1;
                    }
                }
            });

            AsyncCursor <int, int, SCursor <int, int> > cursor = null;
            var cnt      = 0L;
            var readTask = Task.Run(async() =>
            {
                for (int r = 0; r < 1; r++)
                {
                    using (cursor = new AsyncCursor <int, int, SCursor <int, int> >(map.GetCursor()))
                    {
                        using (Benchmark.Run("Read", count * rounds, true))
                        {
                            try
                            {
                                while (await cursor.MoveNextAsync())
                                {
                                    Interlocked.Increment(ref cnt);
                                    // Thread.Sleep(1);
                                }
                            }
                            catch (Exception e)
                            {
                                Console.WriteLine(e);
                                throw;
                            }


                            // Left from coreclr 19161 tests, TODO remove when everything works OK
                            // here is a strong reference to cursor with side effects of printing to console
                            // Console.WriteLine("Last value: " + cursor.Current.Key);
                            // another strong reference after while loop, we dereference it's value and return from task
                            // lastKey1 = cursor.CurrentKey;
                        }
                    }
                }
            });


            var monitor     = true;
            var monitorTask = Task.Run(async() =>
            {
                try
                {
                    var previousR = cursor?.CurrentKey;
                    var previousW = Volatile.Read(ref writeCount);
                    while (monitor)
                    {
                        await Task.Delay(1000);
                        var r = cursor.CurrentKey;
                        var w = Volatile.Read(ref writeCount);
                        Console.WriteLine($"R: {r:N0} - {((r - previousR) / 1000000.0):N2} Mops \t | W: {w:N0}- {((w - previousW) / 1000000.0):N2} Mops");

                        if (r == previousR)
                        {
                            Console.WriteLine($"IsAwaiting {cursor.IsTaskAwating} IsCompl {cursor.IsCompleted} Counter {cursor._counter}");
                        }

                        previousR = r;
                        previousW = w;
                        // cursor.TryComplete(false);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    throw;
                }
            });

            await writeTask;

            Console.WriteLine("COMPLETE");
            map.MarkReadOnly();
            map.NotifyUpdate();
            Console.WriteLine("Read after map complete:" + Interlocked.Read(ref cnt));
            await readTask;

            Console.WriteLine("Read after finish:" + Interlocked.Read(ref cnt));
            // Console.WriteLine("Last key: " + lastKey);

            Benchmark.Dump();
            GC.KeepAlive(cursor);
            map.Dispose();
            monitor = false;
        }
Ejemplo n.º 7
0
        public void CouldMovePartialFromMoving(int count)
        {
            var s = new AppendSeries <int, int>();

            for (int i = 1; i <= count; i++)
            {
                s.Append(i, i);
            }

            SCursor <int, int> c;

            c = s.GetCursor();
            c.MoveNext();
            Assert.AreEqual(count - 1, c.Move(count * 2, true));
            Assert.AreEqual(count, c.CurrentKey);
            Assert.AreEqual(count, c.CurrentValue);
            c.Dispose();

            c = s.GetCursor();
            c.MoveNext();
            Assert.AreEqual(count - 1, c.Move(count * 200, true));
            Assert.AreEqual(count, c.CurrentKey);
            Assert.AreEqual(count, c.CurrentValue);
            c.Dispose();

            c = s.GetCursor();
            c.MoveNext();
            Assert.AreEqual(count - 1, c.Move(count + 1, true));
            Assert.AreEqual(count, c.CurrentKey);
            Assert.AreEqual(count, c.CurrentValue);
            c.Dispose();

            c = s.GetCursor();
            c.MoveNext();
            Assert.AreEqual(count - 1, c.Move(count, true));
            Assert.AreEqual(count, c.CurrentKey);
            Assert.AreEqual(count, c.CurrentValue);
            c.Dispose();

            // ---------

            c = s.GetCursor();
            c.MovePrevious();
            Assert.AreEqual(-count + 1, c.Move(-count * 2, true));
            Assert.AreEqual(1, c.CurrentKey);
            Assert.AreEqual(1, c.CurrentValue);
            c.Dispose();

            c = s.GetCursor();
            c.MovePrevious();
            Assert.AreEqual(-count + 1, c.Move(-count * 200, true));
            Assert.AreEqual(1, c.CurrentKey);
            Assert.AreEqual(1, c.CurrentValue);
            c.Dispose();

            c = s.GetCursor();
            c.MovePrevious();
            Assert.AreEqual(-count + 1, c.Move(-(count + 1), true));
            Assert.AreEqual(1, c.CurrentKey);
            Assert.AreEqual(1, c.CurrentValue);
            c.Dispose();

            c = s.GetCursor();
            c.MovePrevious();
            Assert.AreEqual(-count + 1, c.Move(-count, true));
            Assert.AreEqual(1, c.CurrentKey);
            Assert.AreEqual(1, c.CurrentValue);
            c.Dispose();

            s.Dispose();
        }