Ejemplo n.º 1
0
        public void ZipNFromLogoAndReadmeRepeatWorks()
        {
            var sum = (_upper.Repeat() + _lower);

            Assert.AreEqual(2, sum.Count());
            Assert.AreEqual(32, sum[3]);
            Assert.AreEqual(54, sum[5]);
        }
Ejemplo n.º 2
0
        public void CouldRepeatSeries()
        {
            var sm  = new SortedMap <DateTime, double>();
            var sm2 = new SortedMap <DateTime, double>();

            var count = 1000000;

            for (int i = 0; i < count; i++)
            {
                sm.Add(DateTime.UtcNow.Date.AddSeconds(i * 2), i);
            }

            for (int i = 0; i < count; i++)
            {
                sm2.Add(DateTime.UtcNow.Date.AddSeconds(i * 2 + 1), i);
            }

            var expected = 0.0;

            for (int i = 0; i < count; i++)
            {
                expected += i * 2;;
            }

            var sw = new Stopwatch();

            sw.Start();
            var sum = (sm.Repeat() + sm2).Values.Sum();

            sw.Stop();
            Assert.AreEqual(expected, sum);
            Console.WriteLine("Repeat + zip, elapsed: {0}, ops: {1}", sw.ElapsedMilliseconds, (int)((double)count / (sw.ElapsedMilliseconds / 1000.0)));
        }
Ejemplo n.º 3
0
        public void CouldRepeatMapSeries()
        {
            var sm  = new SortedMap <DateTime, double>();
            var sm2 = new SortedMap <DateTime, double>();

            var count = 1000000;

            for (int i = 0; i < count; i++)
            {
                sm.Add(DateTime.UtcNow.Date.AddSeconds(i * 2), i);
            }

            for (int i = 0; i < count; i++)
            {
                sm2.Add(DateTime.UtcNow.Date.AddSeconds(i * 2 + 1), i);
            }

            var expected = 0.0;

            for (int i = 0; i < count; i++)
            {
                expected += i * 2 + 1 + 1;
            }
            OptimizationSettings.CombineFilterMapDelegates = false;
            var sw = new Stopwatch();

            sw.Start();
            var sum = (sm.Repeat().Map(x => x + 1.0).Repeat().Map(x => x + 1.0) + sm2).Values.Sum(); //

            sw.Stop();
            //Assert.AreEqual(expected, sum);
            Console.WriteLine("Repeat + zip, elapsed: {0}, ops: {1}", sw.ElapsedMilliseconds, (int)((double)count / (sw.ElapsedMilliseconds / 1000.0)));
        }
Ejemplo n.º 4
0
        public void CouldRepeatSeries() {
            var sm = new SortedMap<DateTime, double>();
            var sm2 = new SortedMap<DateTime, double>();

            var count = 1000000;

            for (int i = 0; i < count; i++) {
                sm.Add(DateTime.UtcNow.Date.AddSeconds(i * 2), i);
            }

            for (int i = 0; i < count; i++) {
                sm2.Add(DateTime.UtcNow.Date.AddSeconds(i * 2 + 1), i);
            }

            var expected = 0.0;
            for (int i = 0; i < count; i++) {
                expected += i * 2; ;
            }

            var sw = new Stopwatch();
            sw.Start();
            var sum = (sm.Repeat() + sm2).Values.Sum();
            sw.Stop();
            Assert.AreEqual(expected, sum);
            Console.WriteLine("Repeat + zip, elapsed: {0}, ops: {1}", sw.ElapsedMilliseconds, (int)((double)count / (sw.ElapsedMilliseconds / 1000.0)));

        }
Ejemplo n.º 5
0
        public void CouldRepeatEmptySeries()
        {
            var sm = new SortedMap <DateTime, double>();

            var c = sm.Repeat().GetCursor();

            Assert.IsFalse(c.MoveNext());
        }
Ejemplo n.º 6
0
        public void ZipNFromLogoAndReadmeRepeatCouldMoveCursorCorrectly() {
            var upper = new SortedMap<int, int> { { 2, 2 }, { 4, 4 } };
            var lower = new SortedMap<int, int> { { 1, 10 }, { 3, 30 }, { 5, 50 } };
            var sum = (upper.Repeat() + lower);
            var cursor = sum.GetCursor();

            Assert.AreEqual(32, sum[3]);
            Assert.AreEqual(54, sum[5]);

            Assert.IsFalse(cursor.MoveAt(1, Lookup.EQ));
            Assert.IsTrue(cursor.MoveAt(1, Lookup.GE));
            Assert.AreEqual(3, cursor.CurrentKey);
            Assert.AreEqual(32, cursor.CurrentValue);

            // move forward

            Assert.IsTrue(cursor.MoveNext());
            Assert.AreEqual(5, cursor.CurrentKey);
            Assert.AreEqual(54, cursor.CurrentValue);

            // finished
            Assert.IsFalse(cursor.MoveNext());

            //// move back

            Assert.IsTrue(cursor.MovePrevious());
            Assert.AreEqual(3, cursor.CurrentKey);
            Assert.AreEqual(32, cursor.CurrentValue);

            // async moves
            Assert.IsTrue(cursor.MoveNext(CancellationToken.None).Result);
            Assert.AreEqual(5, cursor.CurrentKey);
            Assert.AreEqual(54, cursor.CurrentValue);

            var moved = false;
            var t = Task.Run(async () => {
                moved = await cursor.MoveNext(CancellationToken.None);
            });

            // add new value
            lower.Add(6, 60);
            t.Wait();
            Assert.IsTrue(moved);
            Assert.AreEqual(6, cursor.CurrentKey);
            Assert.AreEqual(4 + 60, cursor.CurrentValue);

            // when all sources are marked as immutable/complete, MNA must return false
            var t2 = Task.Run(async () => {
                moved = await cursor.MoveNext(CancellationToken.None);
            });
            upper.Complete();
            lower.Complete();
            t2.Wait();
            Assert.IsFalse(moved);

        }
Ejemplo n.º 7
0
        public static void EquiJoinBench()
        {
            var sml = new SortedMap <long, long>(); // alternative: SortedChunkedMap
            var smr = new SortedMap <long, long>(); // alternative: SortedChunkedMap

            var countl = 1_000_000;
            var step   = 10;
            var countr = countl * step;

            for (int i = 0; i < countl; i++)
            {
                sml.Add(i * step, i * step);
            }

            for (int i = 0; i < countr; i++)
            {
                smr.Add(i, i);
            }

            var rounds = 10;

            for (int r = 0; r < rounds; r++)
            {
                var count = 0L;
                var sum   = 0L;
                using (Benchmark.Run("EquiJoin", countr))
                {
                    var result = sml.Repeat().Zip(smr, (lv, rv) => lv);

                    foreach (var keyValuePair in result)
                    {
                        sum += keyValuePair.Value;
                        count++;
                    }

                    Console.WriteLine("COUNT: " + count);
                }
            }

            Benchmark.Dump();
        }
Ejemplo n.º 8
0
        public void ZipNFromLogoAndReadmeRepeatCouldMoveCursorCorrectly()
        {
            var upper = new SortedMap <int, int> {
                { 2, 2 }, { 4, 4 }
            };
            var lower = new SortedMap <int, int> {
                { 1, 10 }, { 3, 30 }, { 5, 50 }
            };
            var sum    = (upper.Repeat() + lower);
            var cursor = sum.GetCursor();

            Assert.AreEqual(32, sum[3]);
            Assert.AreEqual(54, sum[5]);

            Assert.IsFalse(cursor.MoveAt(1, Lookup.EQ));
            Assert.IsTrue(cursor.MoveAt(1, Lookup.GE));
            Assert.AreEqual(3, cursor.CurrentKey);
            Assert.AreEqual(32, cursor.CurrentValue);

            // move forward

            Assert.IsTrue(cursor.MoveNext());
            Assert.AreEqual(5, cursor.CurrentKey);
            Assert.AreEqual(54, cursor.CurrentValue);

            // finished
            Assert.IsFalse(cursor.MoveNext());

            //// move back

            Assert.IsTrue(cursor.MovePrevious());
            Assert.AreEqual(3, cursor.CurrentKey);
            Assert.AreEqual(32, cursor.CurrentValue);

            // async moves
            Assert.IsTrue(cursor.MoveNext(CancellationToken.None).Result);
            Assert.AreEqual(5, cursor.CurrentKey);
            Assert.AreEqual(54, cursor.CurrentValue);

            var moved = false;
            var t     = Task.Run(async() => {
                moved = await cursor.MoveNext(CancellationToken.None);
            });

            // add new value
            lower.Add(6, 60);
            t.Wait();
            Assert.IsTrue(moved);
            Assert.AreEqual(6, cursor.CurrentKey);
            Assert.AreEqual(4 + 60, cursor.CurrentValue);

            // when all sources are marked as immutable/complete, MNA must return false
            var t2 = Task.Run(async() => {
                moved = await cursor.MoveNext(CancellationToken.None);
            });

            upper.Complete();
            lower.Complete();
            t2.Wait();
            Assert.IsFalse(moved);
        }
Ejemplo n.º 9
0
        public void CouldZipContinuousInRealTimeWithOneShort() {
            var sm1 = new SortedMap<DateTime, double>();
            var sm2 = new SortedMap<DateTime, double>();

            var count = 1000;


            for (int i = 0; i < count; i++) {
                sm1.Add(DateTime.UtcNow.Date.AddSeconds(i), i);
            }

            sm2.Add(DateTime.UtcNow.Date.AddSeconds(0), 2);
            sm2.Complete();

            Task.Run(() => {
                Thread.Sleep(1000);
                for (int i = count; i < count * 2; i++) {
                    sm1.Add(DateTime.UtcNow.Date.AddSeconds(i), i);
                    //Thread.Sleep(50);
                }

                sm1.Complete();
            });

            // this test measures isolated performance of ZipN, without ToSortedMap

            var sw = new Stopwatch();


            var series = new[] { sm1.Repeat(), sm2.Repeat() };

            sw.Start();
            var totalSum = 0.0;
            var sumCursor = series.Zip((k, varr) => varr.Sum()).GetCursor();
            var c = 0;
            while (c < 5 && sumCursor.MoveNext()) {
                Assert.AreEqual(c + 2, sumCursor.CurrentValue);
                totalSum += sumCursor.CurrentValue;
                c++;
            }


            Task.Run(async () => {
                while (await sumCursor.MoveNext(CancellationToken.None)) {
                    Assert.AreEqual(c + 2, sumCursor.CurrentValue);
                    Console.WriteLine("Value: " + sumCursor.CurrentValue);
                    totalSum += sumCursor.CurrentValue;
                    c++;
                }
                sw.Stop();
                Console.WriteLine("Elapsed msec: {0}", sw.ElapsedMilliseconds);
                Console.WriteLine("Total sum: {0}", totalSum);
            }).Wait();


        }
Ejemplo n.º 10
0
        public void CouldZipMillionIntsWithMovePreviousContinuous() {
            var sw = new Stopwatch();

            var sm1 = new SortedMap<int, int>();
            var sm2 = new SortedMap<int, int>();
            sm1.Add(0, 0);
            sm2.Add(0, 0);

            for (int i = 2; i < 100000; i = i + 2) {
                sm1.Add(i, i);
                sm2.Add(i + 1, i);
            }

            var series = new[] { sm1.Repeat(), sm2.Repeat(), };

            sw.Start();

            var sum = series.Zip((k, varr) => varr.Sum());
            var sumCursor = sum.GetCursor();
            var pos = 1000000 - 1;
            while (sumCursor.MovePrevious() && sumCursor.MovePrevious() && sumCursor.CurrentKey >= 2) {
                //Assert.AreEqual(pos * 2 - 2, sum[pos]);
                ////sumCursor.MovePrevious();
                //pos--;
                //pos--;
            }


            sw.Stop();
            //Console.WriteLine("Elapsed msec: {0}", sw.ElapsedMilliseconds);
            //for (int i = 2; i < 1000000; i = i + 2) {
            //    Assert.AreEqual(i * 2 - 2, sum[i]);
            //}

        }
Ejemplo n.º 11
0
        public void CouldZipManyContinuousInRealTime() {
            //Assert.Inconclusive();
            //Trace.TraceWarning("volkswagening: this test hangs when started together with ZipN tests");
            //return;
            var sm1 = new SortedMap<DateTime, double>();
            var sm2 = new SortedMap<DateTime, double>();

            var count = 100000;

            for (int i = 0; i < count; i++) {
                sm1.Add(DateTime.UtcNow.Date.AddSeconds(i), i);
                sm2.Add(DateTime.UtcNow.Date.AddSeconds(i), i * 3);
            }

            var t1 = Task.Run(() => {
                try {
                    Thread.Sleep(1000);
                    for (int i = count; i < count * 2; i++) {
                        sm1.Add(DateTime.UtcNow.Date.AddSeconds(i), i);
                    }
                } finally {
                    sm1.Complete();
                    Console.WriteLine("sm1.Complete()");
                }
            });

            var t2 = Task.Run(() => {
                try {
                    Thread.Sleep(950);
                    for (int i = count; i < count * 2; i++) {
                        sm2.Add(DateTime.UtcNow.Date.AddSeconds(i), i * 3);
                    }
                } finally {
                    sm2.Complete();
                    Console.WriteLine("sm2.Complete()");
                }
            });

            // this test measures isolated performance of ZipN, without ToSortedMap

            var sw = new Stopwatch();


            var series = new[] { sm1.Repeat(), sm2.Repeat() };

            sw.Start();
            var totalSum = 0.0;
            var sumCursor = series.Zip((k, varr) => varr.Sum()).GetCursor();
            var c = 0;
            while (c < 5 && sumCursor.MoveNext()) {
                //Assert.AreEqual(c * 4.0, sumCursor.CurrentValue);
                totalSum += sumCursor.CurrentValue;
                c++;
            }

            var t3 = Task.Run(async () =>
            {
                var previous = sumCursor.CurrentKey;
                while (await sumCursor.MoveNext(CancellationToken.None)) {
                    //Assert.AreEqual(c * 4.0, sumCursor.CurrentValue);
                    //Console.WriteLine("Value: " + sumCursor.CurrentValue);
                    totalSum += sumCursor.CurrentValue;
                    c++;
                    Assert.IsTrue(sumCursor.CurrentKey > previous, "Next key is less than previous");
                    previous = sumCursor.CurrentKey;
                }
            });
            Task.WaitAll(t1, t2, t3);
            sw.Stop();
            Console.WriteLine("Elapsed msec: {0}", sw.ElapsedMilliseconds);
            Console.WriteLine("Total sum: {0}", totalSum);
            Assert.AreEqual(count * 2, sm1.Count);
            Assert.AreEqual(count * 2, sm2.Count);

        }
Ejemplo n.º 12
0
        public void BugFromStrategies() {

            var sw = new Stopwatch();

            var sm1 = new SortedMap<int, int>();
            var sm2 = new SortedMap<int, int>();

            sm1.Add(0, 0);
            sm2.Add(-100500, 0);
            for (int i = 2; i < 100; i++) {
                sm1.Add(i, i);
                if (i % 10 == 0) {
                    sm2.Add(i, i);
                }
            }
            // assertion failure
            var repeated = sm2.Repeat().Fill(0);//.ToSortedMap();
            var result = repeated.Zip(sm1, (k, p, d) => p).Lag(1u); // .Fill(0)

            var cursor = result.GetCursor();
            Assert.IsTrue(cursor.MoveNext());

            var clone = cursor.Clone();
            //Assert.IsTrue(clone.MoveNext());
            //Assert.IsTrue(clone.MoveNext());


            var sm = result.ToSortedMap();
            Console.WriteLine(result.Count());


        }
Ejemplo n.º 13
0
        public void CouldZipMillionIntsWithMoveNextContinuous() {
            var sw = new Stopwatch();

            var sm1 = new SortedMap<int, int>();
            var sm2 = new SortedMap<int, int>();
            sm1.Add(0, 0);
            sm2.Add(0, 0);

            for (int i = 2; i < 100000; i = i + 2) {
                sm1.Add(i, i);
                sm2.Add(i + 1, i);
            }

            var series = new[] { sm1.Repeat(), sm2.Repeat(), };

            sw.Start();

            var sum = series.Zip((k, varr) => varr.Sum()).ToSortedMap();

            sw.Stop();
            Console.WriteLine("Elapsed msec: {0}", sw.ElapsedMilliseconds);
            for (int i = 2; i < 100000; i = i + 2) {
                Assert.AreEqual(i * 2 - 2, sum[i]);
            }

        }
Ejemplo n.º 14
0
        public void CouldMoveContinuousOnEmptyIntersect() {

            var sm1 = new SortedMap<int, int>(new Dictionary<int, int>() {
                //{ 1, 1}
            });
            var sm2 = new SortedMap<int, int>(new Dictionary<int, int>()
                {

                    { 1, 2},
                    { 2, 4},
                    { 3, 6},
                    { 5, 10},
                    { 7, 14}
                });


            var zipped = sm1.Repeat() + sm2.Repeat();
            var c1 = zipped.GetCursor();
            //Assert.IsFalse(c1.MoveNext());
            //Assert.IsFalse(c1.MoveFirst());
            var task = c1.MoveNext(CancellationToken.None);

            sm1.Add(6, 1);
            sm1.Add(8, 1);
            Thread.Sleep(50);
            task.Wait();
            Assert.AreEqual(TaskStatus.RanToCompletion, task.Status);
            Assert.AreEqual(6, c1.CurrentKey);
            Assert.AreEqual(15, c1.CurrentValue);

        }
Ejemplo n.º 15
0
        public void CouldMoveAtPositionOfThreeDifferentSeriesAllContinuous() {

            var sm1 = new SortedMap<int, int>(new Dictionary<int, int>()
                {
                    { 1, 1},
                    //{ 2, 2}, // Fill(100)
                    { 3, 3},
                    //{ 5, 5}, // Fill(100)
                    { 7, 7}
                });
            var sm2 = new SortedMap<int, int>(new Dictionary<int, int>()
                {
                    { 1, 2},
                    { 2, 4},
                    { 3, 6},
                    { 5, 10},
                    { 7, 14}

                });
            var sm3 = new SortedMap<int, int>(new Dictionary<int, int>()
                {
                    { 1, 3},
                    { 2, 6},
                    { 3, 9},
                    { 5, 15},
                    { 7, 21}
                });

            var series = new[] { sm1.Fill(100), sm2.Repeat(), sm3.Repeat() };
            var sum = series.Zip((k, varr) => k * varr.Sum());

            var zipNCursor = sum.GetCursor();
            var movedAtEQ = zipNCursor.MoveAt(3, Lookup.EQ);
            Assert.IsTrue(movedAtEQ);
            Assert.AreEqual((3 + 6 + 9) * 3, zipNCursor.CurrentValue);

            var movedAtLE = zipNCursor.MoveAt(2, Lookup.LE);
            Assert.IsTrue(movedAtLE);
            Assert.AreEqual((100 + 4 + 6) * 2, zipNCursor.CurrentValue);

            var movedAtGE = zipNCursor.MoveAt(5, Lookup.GE);
            Assert.IsTrue(movedAtGE);
            Assert.AreEqual((100 + 10 + 15) * 5, zipNCursor.CurrentValue);

            var movedAtLT = zipNCursor.MoveAt(3, Lookup.LT);
            Assert.IsTrue(movedAtLT);
            Assert.AreEqual((100 + 4 + 6) * 2, zipNCursor.CurrentValue);
            movedAtLT = zipNCursor.MoveAt(1, Lookup.LT);
            Assert.IsTrue(!movedAtLT);

            var movedAtGT = zipNCursor.MoveAt(3, Lookup.GT);
            Assert.IsTrue(movedAtGT);
            Assert.AreEqual((100 + 10 + 15) * 5, zipNCursor.CurrentValue);
            movedAtGT = zipNCursor.MoveAt(7, Lookup.GT);
            Assert.IsTrue(!movedAtGT);
            int val;
            var hasGTValue = zipNCursor.TryGetValue(8, out val);
            Assert.IsTrue(hasGTValue);
            Assert.AreEqual((100 + 14 + 21) * 8, val);
        }
Ejemplo n.º 16
0
        public void CouldRepeatMapSeries() {
            var sm = new SortedMap<DateTime, double>();
            var sm2 = new SortedMap<DateTime, double>();

            var count = 1000000;

            for (int i = 0; i < count; i++) {
                sm.Add(DateTime.UtcNow.Date.AddSeconds(i * 2), i);
            }

            for (int i = 0; i < count; i++) {
                sm2.Add(DateTime.UtcNow.Date.AddSeconds(i * 2 + 1), i);
            }

            var expected = 0.0;
            for (int i = 0; i < count; i++) {
                expected += i * 2 + 1 + 1;
            }
            OptimizationSettings.CombineFilterMapDelegates = false;
            var sw = new Stopwatch();
            sw.Start();
            var sum = (sm.Repeat().Map(x => x + 1.0).Repeat().Map(x => x + 1.0) + sm2).Values.Sum(); //
            sw.Stop();
            //Assert.AreEqual(expected, sum);
            Console.WriteLine("Repeat + zip, elapsed: {0}, ops: {1}", sw.ElapsedMilliseconds, (int)((double)count / (sw.ElapsedMilliseconds / 1000.0)));

        }
Ejemplo n.º 17
0
        public void CouldRepeatEmptySeries() {
            var sm = new SortedMap<DateTime, double>();

            var c = sm.Repeat().GetCursor();

            Assert.IsFalse(c.MoveNext());

        }
Ejemplo n.º 18
0
        public void ContinuousZipWithEmptySeriesIsEmpty() {
            var sm1 = new SortedMap<int, int>();
            var sm2 = new SortedMap<int, int>();

            var rp1 = sm1.Repeat();
            var rp2 = sm2.Repeat();

            var zip = rp1.Zip(rp2, (l, r) => l + r);
            Assert.AreEqual(0, zip.Count());

            sm1.Add(1, 1);
            var zip2 = rp1.Zip(rp2, (l, r) => l + r);
            Assert.AreEqual(0, zip2.Count());

            var cursor = zip.GetCursor();
            Assert.IsFalse(cursor.MoveNext());

            var cursor2 = zip2.GetCursor();
            Assert.IsFalse(cursor2.MoveNext());

            var fill = sm2.Fill(0);
            var zip3 = rp1.Zip(fill, (l, r) => l + r);
            Assert.AreEqual(1, zip3.Count());
        }
Ejemplo n.º 19
0
        public void ContinuousZipIsCorrectByConstrcution() {
            var sw = new Stopwatch();

            var sm1 = new SortedMap<int, int>();
            var sm2 = new SortedMap<int, int>();
            sm1.Add(0, 0);
            sm2.Add(0, 0);

            for (int i = 2; i < 50; i = i + 2) {
                sm1.Add(i, i);
                sm2.Add(i + 1, i);
            }
            sm1.IsMutable = false;
            sm2.IsMutable = false;

            var series = new[] { sm1.Repeat(), sm2.Repeat(), };

            sw.Start();
            var ser = series.Zip((k, varr) => varr.Sum());

            var sum = ser.ToSortedMap();

            sw.Stop();
            Console.WriteLine("Elapsed msec: {0}", sw.ElapsedMilliseconds);
            for (int i = 2; i < 50; i = i + 2) {
                Assert.AreEqual(i * 2 - 2, sum[i]);
            }


            var cur = ser.GetCursor();

            var cur2 = cur.Clone();
            var sum2 = new SortedMap<int, int>();
            while (cur2.MoveNext(CancellationToken.None).Result) {
                sum2.Add(cur2.CurrentKey, cur2.CurrentValue);
            }

            Assert.AreEqual(sum.Count, sum2.Count, "Results of sync and async moves must be equal");

            Assert.IsTrue(cur.MoveNext(CancellationToken.None).Result);
            Assert.AreEqual(0, cur.CurrentValue);
            var c = 2;
            while (cur.MoveNext(CancellationToken.None).Result) {
                Assert.AreEqual(c * 2 - 2, cur.CurrentValue);
                var x = cur.MoveNext(CancellationToken.None).Result;
                c += 2;
            }

        }
Ejemplo n.º 20
0
        public void CouldZipManyContinuousInRealTime() {

            var sm1 = new SortedMap<DateTime, double>();
            var sm2 = new SortedMap<DateTime, double>();

            var count = 100000;

            for (int i = 0; i < count; i++) {
                sm1.Add(DateTime.UtcNow.Date.AddSeconds(i), i);
                sm2.Add(DateTime.UtcNow.Date.AddSeconds(i), i * 3);
            }
            sm1.IsMutable = true; // will mutate after the first batch

            Task.Run(() => {
                Thread.Sleep(1000);
                for (int i = count; i < count * 2; i++) {
                    sm1.Add(DateTime.UtcNow.Date.AddSeconds(i), i);
                    //Thread.Sleep(50);
                }

                sm1.IsMutable = false; // stop mutating
                //Console.WriteLine("Set immutable");
            });

            Task.Run(() => {
                Thread.Sleep(950);
                for (int i = count; i < count * 2; i++) {
                    sm2.Add(DateTime.UtcNow.Date.AddSeconds(i), i * 3);
                    //Thread.Sleep(50);
                }

                sm2.IsMutable = false; // stop mutating
                //Console.WriteLine("Set immutable");
            });

            // this test measures isolated performance of ZipN, without ToSortedMap

            var sw = new Stopwatch();


            var series = new[] { sm1.Repeat(), sm2.Repeat() };

            sw.Start();
            var totalSum = 0.0;
            var sumCursor = series.Zip((k, varr) => varr.Sum()).GetCursor();
            var c = 0;
            while (c < 5 && sumCursor.MoveNext()) {
                //Assert.AreEqual(c * 4.0, sumCursor.CurrentValue);
                totalSum += sumCursor.CurrentValue;
                c++;
            }

            while (sumCursor.MoveNext(CancellationToken.None).Result) {
                //Assert.AreEqual(c * 4.0, sumCursor.CurrentValue);
                //Console.WriteLine("Value: " + sumCursor.CurrentValue);
                totalSum += sumCursor.CurrentValue;
                c++;
            }
            sw.Stop();
            Console.WriteLine("Elapsed msec: {0}", sw.ElapsedMilliseconds);
            Console.WriteLine("Total sum: {0}", totalSum);


        }
Ejemplo n.º 21
0
        public void CouldZipContinuousInRealTime() {

            var sm1 = new SortedMap<DateTime, double>();
            var sm2 = new SortedMap<DateTime, double>();

            var count = 100;

            for (int i = 0; i < count; i++) {
                sm1.Add(DateTime.UtcNow.Date.AddSeconds(i), i);
                sm2.Add(DateTime.UtcNow.Date.AddSeconds(i), i * 3);
            }
            sm1.IsMutable = true; // will mutate after the first batch

            Task.Run(() => {
                Thread.Sleep(1000);
                for (int i = count; i < count * 2; i++) {
                    sm1.Add(DateTime.UtcNow.Date.AddSeconds(i), i);
                    Thread.Sleep(50);
                }

                sm1.IsMutable = false; // stop mutating
                //Console.WriteLine("Set immutable");
            });

            Task.Run(() => {
                Thread.Sleep(950);
                for (int i = count; i < count * 2; i++) {
                    sm2.Add(DateTime.UtcNow.Date.AddSeconds(i), i * 3);
                    Thread.Sleep(50);
                }

                sm2.IsMutable = false; // stop mutating
                //Console.WriteLine("Set immutable");
            });

            // this test measures isolated performance of ZipN, without ToSortedMap
            Thread.Sleep(1050);
            var sw = new Stopwatch();


            var series = new[] { sm1.Repeat(), sm2.Repeat() };

            sw.Start();
            var totalSum = 0.0;
            var sumCursor = series.Zip((k, varr) => varr.Sum()).GetCursor();
            var c = 0;
            while (c < 5 && sumCursor.MoveNext()) {
                Assert.AreEqual(c * 4.0, sumCursor.CurrentValue);
                totalSum += sumCursor.CurrentValue;
                c++;
            }


            Task.Run(async () => {
                while (await sumCursor.MoveNext(CancellationToken.None)) {
                    if (Math.Abs(c * 4.0 - sumCursor.CurrentValue) <= 3.0) { // NB VolksWagening
                        // TODO deal with it somehow, e.g. with recalc of the last value, and explicitly document
                        Trace.TraceWarning("Zipping continuous series in real-time is inherently non-deterministic");
                    } else {
                        Assert.AreEqual(c * 4.0, sumCursor.CurrentValue);
                    }
                    Console.WriteLine("Value: " + sumCursor.CurrentValue);
                    totalSum += sumCursor.CurrentValue;
                    c++;
                }
                sw.Stop();
                Console.WriteLine("Elapsed msec: {0}", sw.ElapsedMilliseconds);
                Console.WriteLine("Total sum: {0}", totalSum);
            }).Wait();
            Thread.Sleep(100);
            sumCursor.Dispose();

        }
Ejemplo n.º 22
0
        public void ContinuousZipIsCorrectByRandomCheck(SortedMap <int, int> sm1, SortedMap <int, int> sm2, int seed)
        {
            var series = new ISeries <int, int>[] { sm1.Repeat(), sm2 };

            int[] expectedKeys;
            int[] expectedValues;
            int   size;
            SortedMap <int, int> expectedMap;

            using (Benchmark.Run("Manual join", sm1.Count + sm2.Count))
            {
                var allKeys = sm1.keys.Union(sm2.keys).OrderBy(x => x).ToArray();

                expectedKeys = new int[allKeys.Length];

                expectedValues = new int[allKeys.Length];
                size           = 0;
                for (int i = 0; i < allKeys.Length; i++)
                {
                    var val = 0;
                    KeyValuePair <int, int> temp;
                    var hasFirst = sm1.TryFindAt(allKeys[i], Lookup.LE, out temp);
                    if (hasFirst)
                    {
                        val += temp.Value;
                        var hasSecond = sm2.TryFindAt(allKeys[i], Lookup.EQ, out temp);
                        if (hasSecond)
                        {
                            val += temp.Value;
                            expectedKeys[size]   = allKeys[i];
                            expectedValues[size] = val;
                            size++;
                        }
                    }
                }
                expectedMap = SortedMap <int, int> .OfSortedKeysAndValues(expectedKeys, expectedValues, size);
            }

            SortedMap <int, int> sum;
            Series <int, int>    ser;

            using (Benchmark.Run("Zip join", sm1.Count + sm2.Count))
            {
                sum = sm1.Repeat().Zip(sm2, (v1, v2) => v1 + v2).ToSortedMap();
            }
            Assert.AreEqual(expectedMap.Count, sum.Count, "Expected size");
            foreach (var kvp in expectedMap)
            {
                Assert.AreEqual(kvp.Value, sum[kvp.Key]);
            }

            //using (Benchmark.Run("ZipN join", sm1.Count + sm2.Count))
            //{
            //    ser = series.ZipOld((k, varr) => varr.Sum());
            //    sum = ser.ToSortedMap();
            //}
            //Assert.AreEqual(expectedMap.Count, sum.Count, "Expected size");
            //foreach (var kvp in expectedMap)
            //{
            //    Assert.AreEqual(kvp.Value, sum[kvp.Key]);
            //}

            var sum1 = new SortedMap <int, int>();

            using (Benchmark.Run("Zip Async join", sm1.Count + sm2.Count))
            {
                var zip  = sm1.Repeat().Zip(sm2, (v1, v2) => v1 + v2);
                var cur  = zip.GetAsyncCursor();
                var last = zip.Last.Present.Key;
                Task.Run(async() =>
                {
                    var prev = default(int);
                    while (await cur.MoveNextAsync())
                    {
                        if (cur.CurrentKey == prev)
                        {
                            Console.WriteLine($"Break on equal keys condition, seed {seed}");
                        }
                        prev = cur.CurrentKey;
                        sum1.Add(cur.CurrentKey, cur.CurrentValue);
                        //if (prev == last)
                        //{
                        //    Console.WriteLine("Next should be false");
                        //}
                    }
                })
                .Wait();
            }
            Assert.AreEqual(expectedMap.Count, sum1.Count, "Results of sync and async moves must be equal");
            foreach (var kvp in expectedMap)
            {
                Assert.AreEqual(kvp.Value, sum1[kvp.Key]);
            }

            // TODO this uses Subscribe which is not implemented yet
            //var sum2 = new SortedMap<int, int>();
            //using (Benchmark.Run("ZipN Async join", sm1.Count + sm2.Count))
            //{
            //    var cur = ser.GetCursor();

            //    var cur2 = cur.Clone();

            //    Task.Run(async () =>
            //        {
            //            while (await cur2.MoveNextAsync(CancellationToken.None))
            //            {
            //                sum2.Add(cur2.CurrentKey, cur2.CurrentValue);
            //            }
            //        })
            //        .Wait();
            //}

            //Assert.AreEqual(sum.Count, sum2.Count, "Results of sync and async moves must be equal");
            //foreach (var kvp in expectedMap)
            //{
            //    Assert.AreEqual(kvp.Value, sum2[kvp.Key]);
            //}
        }
Ejemplo n.º 23
0
        public void ContinuousZipIsCorrectByRandomCheck() {
            var sw = new Stopwatch();

            var sm1 = new SortedMap<int, int>();
            var sm2 = new SortedMap<int, int>();


            var rng = new System.Random(31415926); //31415926

            var prev1 = 0;
            var prev2 = 0;

            for (int i = 0; i < 100000; i = i + 1) {
                prev1 = prev1 + rng.Next(1, 11);
                sm1.Add(prev1, prev1);
                prev2 = prev2 + rng.Next(1, 11);
                sm2.Add(prev2, prev2);
            }
            sm1.IsMutable = false;
            sm2.IsMutable = false;

            //Console.WriteLine("First map:");
            //foreach (var kvp in sm1)
            //{
            //    Console.WriteLine(kvp.Key);
            //}

            //Console.WriteLine("Second map:");
            //foreach (var kvp in sm2) {
            //    Console.WriteLine(kvp.Key);
            //}

            var series = new[] { sm1.Repeat(), sm2.Repeat(), };

            sw.Start();
            var allKeys = sm1.keys.Union(sm2.keys).OrderBy(x => x).ToArray();
            int[] expectedKeys = new int[allKeys.Length];
            int[] expectedValues = new int[allKeys.Length];
            var size = 0;
            for (int i = 0; i < allKeys.Length; i++) {
                var val = 0;
                KeyValuePair<int, int> temp;
                var hasFirst = sm1.TryFind(allKeys[i], Lookup.LE, out temp);
                if (hasFirst) {
                    val += temp.Value;
                    var hasSecond = sm2.TryFind(allKeys[i], Lookup.LE, out temp);
                    if (hasSecond) {
                        val += temp.Value;
                        expectedKeys[size] = allKeys[i];
                        expectedValues[size] = val;
                        size++;
                    }
                }
            }

            var expectedMap = SortedMap<int, int>.OfSortedKeysAndValues(expectedKeys, expectedValues, size);

            sw.Stop();

            //Console.WriteLine("Expected map:");
            //foreach (var kvp in expectedMap) {
            //    Console.WriteLine(kvp.Key + " ; " + kvp.Value);
            //}

            Console.WriteLine("Manual join, elapsed msec: {0}", sw.ElapsedMilliseconds);


            SortedMap<int, int> sum = new SortedMap<int, int>();

            for (int round = 0; round < 1; round++) {
                sw.Restart();
                var ser = series.Zip((k, varr) => varr.Sum());

                var cur = ser.GetCursor();
                while (cur.MoveNext()) {
                    sum.AddLast(cur.CurrentKey, cur.CurrentValue);
                }

                sw.Stop();
                Console.WriteLine("Zip join, elapsed msec: {0}", sw.ElapsedMilliseconds);
                //Console.WriteLine("StateCreation: {0}", RepeatCursor<int, int>.StateCreation);
                //Console.WriteLine("StateHit: {0}", RepeatCursor<int, int>.StateHit);
                //Console.WriteLine("StateMiss: {0}", RepeatCursor<int, int>.StateMiss);
            }



            //Console.WriteLine("Sync zip map:");
            //foreach (var kvp in sum) {
            //    Console.WriteLine(kvp.Key + " ; " + kvp.Value);
            //}
            Assert.AreEqual(expectedMap.Count, sum.Count, "Results of sync and expected must be equal");

            foreach (var kvp in expectedMap) {
                Assert.AreEqual(kvp.Value, sum[kvp.Key]);
            }

            for (int round = 0; round < 1; round++) {
                sw.Restart();
                var ser = series.Zip((k, varr) => varr.Sum());

                var cur = ser.GetCursor();

                var cur2 = cur.Clone();
                var sum2 = new SortedMap<int, int>();
                Task.Run(async () => {
                    while (await cur2.MoveNext(CancellationToken.None)) {
                        sum2.Add(cur2.CurrentKey, cur2.CurrentValue);
                    }
                }).Wait();

                sw.Stop();
                Console.WriteLine("Async Zip join, elapsed msec: {0}", sw.ElapsedMilliseconds);
                Assert.AreEqual(sum.Count, sum2.Count, "Results of sync and async moves must be equal");
                foreach (var kvp in expectedMap) {
                    Assert.AreEqual(kvp.Value, sum2[kvp.Key]);
                }


            }

            Console.WriteLine("");
        }
Ejemplo n.º 24
0
        public void UnionKeysTest() {
            var sm1 = new SortedMap<int, int>();
            var sm2 = new SortedMap<int, int>();
            var zip = sm1.Repeat() + sm2.Repeat();
            var c = zip.GetCursor();


            sm1.Add(1, 1);
            Assert.IsFalse(c.MoveNext());
            sm2.Add(0, 0);
            Assert.IsTrue(c.MoveNext());
            Assert.AreEqual(1, c.CurrentKey);

            sm1.Add(0, 0);
            Assert.IsFalse(c.MoveNext());
            Assert.IsTrue(c.MovePrevious());
            Assert.AreEqual(0, c.CurrentKey);
            Assert.IsFalse(c.MovePrevious());
            Assert.IsTrue(c.MoveNext());
            Assert.AreEqual(1, c.CurrentKey);

            sm1.Add(3, 3);
            Assert.IsTrue(c.MoveNext());
            Assert.AreEqual(3, c.CurrentKey);
            Assert.AreEqual(3, c.CurrentValue);

            var t = Task.Run(async () => {
                return await c.MoveNext(CancellationToken.None);
            });
            Thread.Sleep(15);
            sm2.Add(4, 4);
            Assert.IsTrue(t.Wait(50));
            Assert.IsTrue(t.Result);
            Assert.AreEqual(4, c.CurrentKey);
            Assert.AreEqual(7, c.CurrentValue);
        }
Ejemplo n.º 25
0
        public void CouldNotMoveAsyncContinuousOnEmptyZip() {

            var sm1 = new SortedMap<int, int>();
            var sm2 = new SortedMap<int, int>();
            sm1.IsMutable = false;
            sm2.IsMutable = false;

            var zipped = sm1.Repeat() + sm2.Repeat();
            var c1 = zipped.GetCursor();
            Assert.IsFalse(sm1.GetCursor().MoveNext(CancellationToken.None).Result);
            Assert.IsFalse(sm2.GetCursor().MoveNext(CancellationToken.None).Result);
            Assert.IsFalse(c1.MoveNext());
            Assert.IsFalse(c1.MoveFirst());
            var task = c1.MoveNext(CancellationToken.None);
            task.Wait();
            Assert.AreEqual(TaskStatus.RanToCompletion, task.Status);
            Assert.IsFalse(task.Result);
        }
Ejemplo n.º 26
0
        public void CouldZipManyContinuousInRealTime3() {

            var sm1 = new SortedMap<DateTime, double>();
            var sm2 = new SortedMap<DateTime, double>();

            var count = 100000;

            for (int i = 0; i < count; i++) {
                sm1.Add(DateTime.UtcNow.Date.AddSeconds(i), i);
                sm2.Add(DateTime.UtcNow.Date.AddSeconds(i), i * 3);
            }
            sm1.Complete(); // will mutate after the first batch
            sm2.Complete();


            // this test measures isolated performance of ZipN, without ToSortedMap

            var sw = new Stopwatch();


            var series = new[] { sm1.Repeat(), sm2.Repeat(), sm1.Repeat(), sm2.Repeat(), sm1.Repeat(), sm2.Repeat(), sm1.Repeat(), sm2.Repeat(), sm1.Repeat(), sm2.Repeat() };

            sw.Start();
            var totalSum = 0.0;
            var sumCursor = series.Zip((k, varr) => varr.Sum()).GetCursor();
            var c = 0;
            //while (sumCursor.MoveNext()) {
            //    //Assert.AreEqual(c * 4.0, sumCursor.CurrentValue);
            //    totalSum += sumCursor.CurrentValue;
            //    c++;
            //}

            Task.Run(async () => {
                while (await sumCursor.MoveNext(CancellationToken.None)) {
                    //Assert.AreEqual(c * 4.0, sumCursor.CurrentValue);
                    //Console.WriteLine("Value: " + sumCursor.CurrentValue);
                    totalSum += sumCursor.CurrentValue;
                    c++;
                }
                sw.Stop();
                Console.WriteLine("Elapsed msec: {0}", sw.ElapsedMilliseconds);
                Console.WriteLine("Total sum: {0}", totalSum);
            }).Wait();



        }