Beispiel #1
0
        public void CouldUseBaseSeriesAddOperator()
        {
            var count = 10000;
            var sm1   = new SortedMap <int, double>();
            var sm2   = new SortedMap <int, double>();

            var    result   = new SortedMap <int, double>();
            double expected = 0;

            sm1.Add(0, 0);
            sm2.Add(0, 0);

            for (int i = 2; i < count; i++)
            {
                sm1.Add(i, i);
                if (i % 1 == 0)
                {
                    sm2.Add(i, 2 * i);
                    expected += i + 2 * i;

                    result.Add(i, i + 2 * i);
                }
            }

            for (int round = 0; round < 20; round++)
            {
                double actual = 0;
                using (Benchmark.Run("ZipDiscrete", (int)sm1.Count + (int)sm2.Count))
                {
                    var calculated = sm1 + sm2;
                    using (var c = calculated.GetEnumerator())
                    {
                        while (c.MoveNext())
                        {
                            actual += c.CurrentValue;
                        }
                    }
                    //foreach (var cursorSeries in calculated)
                    //{
                    //    actual += cursorSeries.Value;
                    //}
                }

                Assert.AreEqual(expected, actual);

                //double actual1 = 0;
                //using (Benchmark.Run("LINQ", (int)sm1.Count + (int)sm2.Count))
                //{
                //    var linq = sm1.Zip(sm2, (l, r) => l.Value + r.Value);
                //    foreach (var l in linq)
                //    {
                //        actual1 += l;
                //    }
                //}

                //Assert.AreEqual(expected, actual);
            }

            Benchmark.Dump();
        }
Beispiel #2
0
        public void CouldMoveNextAsyncWhenChangingOrder_NoSemaphore()
        {
            var cts = new CancellationTokenSource();
            var ct  = cts.Token;

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

            sm.IsSynchronized = true;
            var tcs     = new TaskCompletionSource <bool>();
            var sumTask = Task.Run(async() =>
            {
                var c = sm.GetCursor();
                tcs.SetResult(true);
                Assert.IsTrue(await c.MoveNext(ct));
                // here we change order
                Assert.IsTrue(await c.MoveNext(ct));
                Assert.IsFalse(await c.MoveNext(ct));
            });

            tcs.Task.Wait(ct);

            sm.Add(1, 1);
            Thread.Sleep(100);
            //sm.Add(0, 0); // will through OOO
            sm.Add(2, 2);
            //sm.Add(3, 3);
            sm.Complete();

            sumTask.Wait(ct);
        }
Beispiel #3
0
        public void RangeCursorMovesAfterAwating()
        {
            var sm = new SortedMap <int, int>();

            sm.Add(1, 1);

            var range = sm.Range(int.MinValue, 2, true, true);

            //Assert.AreEqual(1, range.First.Value);

            var cursor = range.GetAsyncEnumerator();

            var source = cursor.Source;

            var cts = new CancellationTokenSource();

            var t = Task.Run(async() =>
            {
                var moved = await cursor.MoveNextAsync();
                Assert.True(moved);
                moved = await cursor.MoveNextAsync();
                Assert.True(moved);
                moved = await cursor.MoveNextAsync();
                Assert.False(moved);
                Assert.AreEqual(2, cursor.CurrentKey);
                Assert.AreEqual(2, cursor.CurrentValue);
            });

            Thread.Sleep(100);
            sm.Add(2, 2);
            sm.Add(3, 3);
            t.Wait();
        }
Beispiel #4
0
        public void CouldCalculateSMAWithWidthEQ()
        {
            Assert.Throws <NotImplementedException>(() =>
            {
                var count = 20;
                var sm    = new SortedMap <int, double>();
                sm.Add(0, 0);
                for (int i = 2; i <= count; i++)
                {
                    sm.Add(i, i);
                }
                sm.Remove(11);
                sm.Remove(12);
                var onlineOp = new SumAvgOnlineOp <int, double, SortedMapCursor <int, double> >();
                var smaOp    = new SpanOpWidth <int, double, double, SortedMapCursor <int, double>, SumAvgOnlineOp <int, double, SortedMapCursor <int, double> > >
                                   (2, Lookup.EQ, onlineOp);
                var smaSeries =
                    new SpanOpImpl <int,
                                    double,
                                    double,
                                    SpanOpWidth <int, double, double, SortedMapCursor <int, double>, SumAvgOnlineOp <int, double, SortedMapCursor <int, double> > >,
                                    SortedMapCursor <int, double>
                                    >(sm.GetEnumerator(), smaOp).Source;

                foreach (var keyValuePair in smaSeries)
                {
                    Trace.WriteLine($"{keyValuePair.Key} - {keyValuePair.Value}");
                }
            });
        }
        public void CouldCalculateSMAInRealTime() {
            var sm = new SortedMap<int, double>();

            Task.Run(async () => {

                for (int i = 0; i < 20; i++) {
                    sm.Add(i, i);
                }

                await Task.Delay(100);

                for (int i = 20; i < 100; i++) {
                    await Task.Delay(1); // 15 msec
                    sm.Add(i, i);
                }
                sm.IsMutable = false;
            });


            var sma = sm.SMA(10, true);

            var c = sma.GetCursor();

            while (c.MoveNext(CancellationToken.None).Result) {
                Console.WriteLine("Key: {0}, value: {1}", c.CurrentKey, c.CurrentValue);
            }

        }
Beispiel #6
0
        public void DeadCursorDoesNotCauseEndlessLoopInNotifyUpdate()
        {
            var sm = new SortedMap <int, int>();

            sm.Add(1, 1);

            var cursor = sm.GetAsyncEnumerator();

            Assert.True(cursor.MoveNext());
            Assert.False(cursor.MoveNext());

            cursor.MoveNextAsync();

            cursor = default;

            GC.Collect(2, GCCollectionMode.Forced, true);
            GC.Collect(2, GCCollectionMode.Forced, true);

            //var t = Task.Run(() => cursor.MoveNextAsync(cts.Token));

            sm.Add(2, 2);
            sm.Add(3, 3);

            Assert.True(sm.Count == 3);
        }
Beispiel #7
0
        public async Task CouldUseZipNSelector()
        {
            var sm1 = new SortedMap <int, double>();
            var sm2 = new SortedMap <int, double>();
            var sm3 = new SortedMap <int, double>();

            double expected = 0;

            sm1.Add(0, 0);
            sm2.Add(0, 0);
            sm3.Add(0, 0);

            var count = 10_000_000;

            for (int i = 2; i < count; i++)
            {
                expected += i + 2 * i + 3 * i;
                sm1.Add(i, i);
                sm2.Add(i, 2 * i);
                sm3.Add(i, 3 * i);
            }

            await sm1.Complete();

            await sm2.Complete();

            await sm3.Complete();


            //var result = new[] {sm1, sm2, sm3}.Zip((int k, Span<double> sp) => { return sp[0] + sp[1] + sp[2]; });
            var result = new[] { sm1, sm2, sm3 }.Zip((key, values) =>
            {
                return(values[0] + values[1] + values[2]);
            });

            // var resultX = result + 1;

            var sum = 0.0;
            var c   = 0L;

            for (int r = 0; r < 10; r++)
            {
                using (Benchmark.Run("ZipN", count))
                {
#if NETCOREAPP3_0
                    // await
#endif
                    foreach (var item in result)
                    {
                        sum += item.Value;
                        c++;
                    }
                }
            }
            Benchmark.Dump();


            Console.WriteLine(sum);
            Console.WriteLine("Total " + c.ToString("N"));
        }
Beispiel #8
0
        public void DeadCursorDoesntCauseEndlessLoopInNotifyUpdate()
        {
            var sm = new SortedMap <int, int>();

            sm.Add(1, 1);

            var cursor = sm.GetCursor();

            Assert.True(cursor.MoveNext());
            Assert.False(cursor.MoveNext());

            var cts = new CancellationTokenSource();

            cursor.MoveNext(cts.Token);

            cursor = null;

            GC.Collect(2, GCCollectionMode.Forced, true);
            GC.Collect(2, GCCollectionMode.Forced, true);

            //var t = Task.Run(() => cursor.MoveNext(cts.Token));

            sm.Add(2, 2);
            sm.Add(3, 3);

            Assert.True(sm.Count == 3);
        }
        public void CouldCalculateSMAInRealTime()
        {
            var sm = new SortedMap <int, double>();

            Task.Run(async() => {
                for (int i = 0; i < 20; i++)
                {
                    sm.Add(i, i);
                }

                await Task.Delay(100);

                for (int i = 20; i < 100; i++)
                {
                    await Task.Delay(1); // 15 msec
                    sm.Add(i, i);
                }
                sm.Complete();
            });


            var sma = sm.SMA(10, true);

            var c = sma.GetCursor();

            while (c.MoveNext(CancellationToken.None).Result)
            {
                Console.WriteLine("Key: {0}, value: {1}", c.CurrentKey, c.CurrentValue);
            }
        }
Beispiel #10
0
        public void RangeCursorStopsBeforeEndKey()
        {
            var sm = new SortedMap <int, int>();

            sm.Add(1, 1);
            sm.Add(2, 2);
            sm.Add(3, 3);
            var range = sm.Range(int.MinValue, 2, true, true);

            //Assert.AreEqual(1, range.First.Value);

            var cursor = range.GetCursor();

            var source = cursor.Source;

            var cts = new CancellationTokenSource();

            var t = Task.Run(async() =>
            {
                var moved = await cursor.MoveNext(cts.Token);
                Assert.True(moved);
                moved = await cursor.MoveNext(cts.Token);
                Assert.True(moved);
                moved = await cursor.MoveNext(cts.Token);
                Assert.False(moved);
                Assert.AreEqual(2, cursor.CurrentKey);
                Assert.AreEqual(2, cursor.CurrentValue);
            });

            t.Wait();
        }
Beispiel #11
0
        public void CouldCancelRangeCursor()
        {
            var sm = new SortedMap <int, int>();

            sm.Add(-1, -1);
            sm.Add(1, 1);
            sm.Add(2, 2);

            var range = sm.Range(0, 2, true, false);

            Assert.AreEqual(1, range.First.Value);

            var cursor = range.GetCursor();

            var source = cursor.Source;

            Assert.AreEqual(1, source.Count());

            Assert.True(cursor.MoveNext());
            Assert.False(cursor.MoveNext());

            var cts = new CancellationTokenSource();

            var t = Task.Run(() =>
            {
                Thread.Sleep(100);
                var task = cursor.MoveNext(cts.Token);
                Assert.True(task.IsCanceled);
            });

            cts.Cancel();
            t.Wait();
        }
Beispiel #12
0
        public void AddExistingThrowsAndKeepsVersion()
        {
            var sm = new SortedMap <long, long>();

            sm.Add(1, 1);
            Assert.AreEqual(1, sm.Version);
            Assert.Throws <ArgumentException>(() => sm.Add(1, 1));
            Assert.AreEqual(1, sm.Version);
        }
Beispiel #13
0
        public async void RangeOnVariantSeriesWorks()
        {
            var sm = new SortedMap <DateTime, int>();

            for (int i = 0; i < 100; i++)
            {
                sm.Add(DateTime.Today.AddSeconds(i), i);
            }

            var vs = new VariantSeries <DateTime, int>(sm);

            Assert.IsTrue(!vs.Comparer.Equals(KeyComparer <Variant> .Default));

            var rs = vs.After(Variant.Create(DateTime.Today.AddSeconds(50)));

            Assert.IsTrue(!rs.Comparer.Equals(KeyComparer <Variant> .Default));

            var expected = 0;

            for (int i = 50; i < 100; i++)
            {
                expected += i;
            }

            var sum = 0;

            foreach (var variantKvp in rs)
            {
                sum += variantKvp.Value.Get <int>();
            }
            Assert.AreEqual(expected, sum);


            var t = Task.Run(async() =>
            {
                try
                {
                    for (int i = 100; i < 150; i++)
                    {
                        sm.Add(DateTime.Today.AddSeconds(i), i);
                        await Task.Delay(1);
                    }
                    sm.Complete();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            });
            var c = rs.GetCursor();

            while (await c.MoveNext(CancellationToken.None))
            {
                sum += c.CurrentValue.Get <int>();
            }
        }
Beispiel #14
0
        public unsafe void CouldSerializeRegularSortedMapWithZstd()
        {
            var rng = new Random();

            var dest   = (Memory <byte>) new byte[1000000];
            var buffer = dest;
            var handle = buffer.Pin();
            var ptr    = (IntPtr)handle.Pointer;

            var sm = new SortedMap <DateTime, decimal>();

            for (var i = 0; i < 1000; i++)
            {
                sm.Add(DateTime.Today.AddSeconds(i), (decimal)Math.Round(i + rng.NextDouble(), 2));
            }

            var sizeOf  = BinarySerializer.SizeOf(sm, out var tmp);
            var written = BinarySerializer.Write(sm, dest.Span, tmp);

            Assert.AreEqual(sizeOf, written);
            Console.WriteLine($"Useful: {sm.Count * 24}");
            Console.WriteLine($"Total: {written}");
            // NB interesting that with converting double to decimal savings go from 65% to 85%,
            // even calculated from (8+8) base size not decimal's 16 size
            Console.WriteLine($"Savings: {1.0 - ((written * 1.0) / (sm.Count * 24.0))}");
            SortedMap <DateTime, decimal> sm2 = null;
            var len2 = BinarySerializer.Read(buffer.Span, out sm2);

            Assert.AreEqual(written, len2);

            Assert.IsTrue(sm2.Keys.SequenceEqual(sm.Keys));
            Assert.IsTrue(sm2.Values.SequenceEqual(sm.Values));
        }
 public IReadOnlyOrderedMap<DateTime, double> MultiplyMap(IReadOnlyOrderedMap<DateTime, double> batch) {
     var sm = new SortedMap<DateTime, double>();
     foreach (var kvp in batch) {
         sm.Add(kvp.Key, kvp.Value * 10.0);
     }
     return sm;
 }
 /// <summary>
 /// Very straighforward batch operation for testing
 /// </summary>
 public IReadOnlyOrderedMap<DateTime, double> IncrementMap(IReadOnlyOrderedMap<DateTime, double> batch) {
     var sm = new SortedMap<DateTime, double>();
     foreach (var kvp in batch) {
         sm.Add(kvp.Key, kvp.Value + 1.0);
     }
     return sm;
 }
        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)));

        }
        public void CouldFillSeries() {
            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; ;
            }

            var sw = new Stopwatch();
            sw.Start();
            var sum = (sm.Fill(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)));

        }
Beispiel #19
0
        public void CouldCalculateOnlineMovingRegression()
        {
            var rng = new Random();
            var y   = new SortedMap <DateTime, double>();
            var xx  = new SortedMap <DateTime, double[]>();
            var dt  = DateTime.Today;

            for (int i = 0; i < 10000; i++)
            {
                var xrow = new double[3];
                xrow[0] = 1;
                xrow[1] = rng.NextDouble() * 5 * i;
                xrow[2] = rng.NextDouble() * 10 * i;
                var yrow = 0.33 + 0.25 * xrow[1] + 1.5 * xrow[2] + (rng.NextDouble() - 0.5);
                y.Add(dt, yrow);
                xx.Add(dt, xrow);
                dt = dt.AddSeconds(1);
            }

            var movingRegression = y.MovingRegression(xx, 1000, 100);

            foreach (var kvp in movingRegression)
            {
                Console.WriteLine($"{kvp.Value[0, 0]} - {kvp.Value[1, 0]} - {kvp.Value[2, 0]}");
            }
        }
Beispiel #20
0
        public void CouldReadVariantSeries()
        {
            var sm = new SortedMap <int, string>();

            for (int i = 0; i < 100; i++)
            {
                sm.Add(i, (i * 100).ToString());
            }

            var vs = new VariantSeries <int, string>(sm);


            foreach (var item in vs)
            {
                System.Console.WriteLine(item.Key.Get <int>() + ": " + item.Value.Get <string>());
            }

            Assert.AreEqual(Variant.Create(0), vs.First.Key);
            Assert.AreEqual(Variant.Create("0"), vs.First.Value);
            Assert.AreEqual(Variant.Create(99), vs.Last.Key);
            Assert.AreEqual(Variant.Create("9900"), vs.Last.Value);


            var cursorSeries = new CursorSeries <int, string>(sm);

            Assert.AreEqual(0, cursorSeries.First.Key);
        }
Beispiel #21
0
        public void CouldAddVariantSeries()
        {
            var sm = new SortedMap <int, double>();

            for (int i = 0; i < 100; i++)
            {
                sm.Add(i, (i * 100));
            }

            var vs = new VariantSeries <int, double>(sm);

            var doubled = vs + vs;

            foreach (var item in doubled)
            {
                System.Console.WriteLine(item.Key.Get <int>() + ": " + item.Value.Get <double>());
            }

            Assert.AreEqual(Variant.Create(0), doubled.First.Key);
            Assert.AreEqual(Variant.Create(0.0), doubled.First.Value);
            Assert.AreEqual(Variant.Create(99), doubled.Last.Key);
            Assert.AreEqual(Variant.Create(9900.0 * 2), doubled.Last.Value);


            var cursorSeries = doubled.ReadOnly();

            Assert.AreEqual(Variant.Create(0), cursorSeries.First.Key);
        }
Beispiel #22
0
        public void CouldCalculateAverageOnMovingWindowWithStep()
        {
            var sm = new SortedMap <DateTime, double>();

            var count = 100000;

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

            // slow implementation
            var sw = new Stopwatch();

            sw.Start();
            var ma = sm.Window(20, 2);//.ToSortedMap();
            var c  = 1;

            foreach (var m in ma)
            {
                var innersm = m.Value;//.ToSortedMap();
                if (innersm.Values.Average() != c + 8.5)
                {
                    Console.WriteLine(m.Value.Values.Average());
                    throw new ApplicationException("Invalid value");
                }
                c++;
                c++;
            }
            sw.Stop();
            Console.WriteLine($"Final c: {c}");
            Console.WriteLine("Window MA, elapsed: {0}, ops: {1}", sw.ElapsedMilliseconds, (int)((double)count / (sw.ElapsedMilliseconds / 1000.0)));
            Console.WriteLine("Calculation ops: {0}", (int)((double)count * 20.0 / (sw.ElapsedMilliseconds / 1000.0)));
        }
Beispiel #23
0
        public unsafe void CouldSerializeSortedMap2()
        {
            var rng = new Random();

            var dest   = (Memory <byte>) new byte[1000000];
            var buffer = dest;
            var handle = buffer.Pin();
            var ptr    = (IntPtr)handle.Pointer;

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

            for (var i = 0; i < 10000; i++)
            {
                sm.Add(i, i);
            }

            var len  = BinarySerializer.SizeOf(sm, out var temp);
            var len2 = BinarySerializer.Write(sm, buffer.Span, temp);

            Assert.AreEqual(len, len2);
            Console.WriteLine($"Useful: {sm.Count * 8}");
            Console.WriteLine($"Total: {len}");
            // NB interesting that with converting double to decimal savings go from 65% to 85%,
            // even calculated from (8+8) base size not decimal's 16 size
            Console.WriteLine($"Savings: {1.0 - ((len * 1.0) / (sm.Count * 8.0))}");
            SortedMap <int, int> sm2 = null;
            var len3 = BinarySerializer.Read(buffer.Span, out sm2);

            Assert.AreEqual(len, len3);

            Assert.IsTrue(sm2.Keys.SequenceEqual(sm.Keys));
            Assert.IsTrue(sm2.Values.SequenceEqual(sm.Values));
        }
        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)));
        }
        public void CouldMovePreviousWithoutBatching()
        {
            var sm = new SortedMap <DateTime, double>();

            var count = 1000;

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

            var bmvc = new BatchMapValuesCursor <DateTime, double, double>(sm.GetCursor, (v) => v + 1.0);
            var c    = 0;

            while (c < 500 && bmvc.MoveNext())
            {
                Assert.AreEqual(c + 1.0, bmvc.CurrentValue);
                c++;
            }
            c--;
            while (bmvc.MovePrevious())
            {
                c--;
                Assert.AreEqual(c + 1.0, bmvc.CurrentValue);
            }
            Assert.AreEqual(0, c);
        }
Beispiel #26
0
        public void CouldSerializeSortedMap2()
        {
            var rng = new Random();
            var ptr = Marshal.AllocHGlobal(1000000);
            var db  = new DirectBuffer(1000000, ptr);
            var sm  = new SortedMap <int, int>();

            for (var i = 0; i < 10000; i++)
            {
                sm.Add(i, i);
            }
            MemoryStream temp;
            var          len  = BinarySerializer.SizeOf(sm, out temp);
            var          len2 = BinarySerializer.Write(sm, ref db, 0, temp);

            Assert.AreEqual(len, len2);
            Console.WriteLine($"Useful: {sm.Count * 8}");
            Console.WriteLine($"Total: {len}");
            // NB interesting that with converting double to decimal savings go from 65% to 85%,
            // even calculated from (8+8) base size not decimal's 16 size
            Console.WriteLine($"Savings: {1.0 - ((len * 1.0) / (sm.Count * 8.0))}");
            SortedMap <int, int> sm2 = null;
            var len3 = BinarySerializer.Read(db, 0, ref sm2);

            Assert.AreEqual(len, len3);

            Assert.IsTrue(sm2.Keys.SequenceEqual(sm.Keys));
            Assert.IsTrue(sm2.Values.SequenceEqual(sm.Values));
        }
Beispiel #27
0
        public void CouldSerializeRegularSortedMapWithZstd()
        {
            BloscSettings.CompressionMethod = CompressionMethod.Zstd;
            var rng = new Random();
            var ptr = Marshal.AllocHGlobal(10000);
            var db  = new DirectBuffer(10000, ptr);
            var sm  = new SortedMap <DateTime, decimal>();

            for (var i = 0; i < 1; i++)
            {
                sm.Add(DateTime.Today.AddSeconds(i), (decimal)Math.Round(i + rng.NextDouble(), 2));
            }

            MemoryStream tmp;
            var          size = BinarySerializer.SizeOf(sm, out tmp);
            var          len  = BinarySerializer.Write(sm, ref db, 0, tmp);

            Console.WriteLine($"Useful: {sm.Count * 24}");
            Console.WriteLine($"Total: {len}");
            // NB interesting that with converting double to decimal savings go from 65% to 85%,
            // even calculated from (8+8) base size not decimal's 16 size
            Console.WriteLine($"Savings: {1.0 - ((len * 1.0) / (sm.Count * 24.0))}");
            SortedMap <DateTime, decimal> sm2 = null;
            var len2 = BinarySerializer.Read(db, 0, ref sm2);

            Assert.AreEqual(len, len2);

            Assert.IsTrue(sm2.Keys.SequenceEqual(sm.Keys));
            Assert.IsTrue(sm2.Values.SequenceEqual(sm.Values));
        }
Beispiel #28
0
        public void CouldZipLagSeries() {
            var sm = new SortedMap<DateTime, double>();

            var count = 10000000;

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

            // slow implementation
            var sw = new Stopwatch();
            sw.Start();
            var zipLag = sm.ZipLag(1, (cur, prev) => cur + prev); //.ToSortedMap();
            var c = 1;
            foreach (var zl in zipLag) {
                if (c + (c - 1) != zl.Value) {
                    throw new ApplicationException();
                }
                c++;
            }
            sw.Stop();
            Console.WriteLine($"Final c: {c}");
            Console.WriteLine("ZipLag, elapsed: {0}, ops: {1}", sw.ElapsedMilliseconds, (int)((double)count / (sw.ElapsedMilliseconds / 1000.0)));

        }
Beispiel #29
0
        public void CouldLagSeries() {
            var sm = new SortedMap<double, double>();

            var count = 1000000;

            for (int i = 0; i < count; i++) {
                sm.Add(i, i);
            }

            // slow implementation
            var sw = new Stopwatch();
            sw.Start();
            var lag = sm.Lag(1);//.ToSortedMap();
            var c = 1;
            foreach (var zl in lag) {
                if (c - 1 != zl.Value) {
                    throw new ApplicationException();
                }
                c++;
            }
            sw.Stop();
            Console.WriteLine($"Final c: {c}");
            Console.WriteLine("ZipLag, elapsed: {0}, ops: {1}", sw.ElapsedMilliseconds, (int)((double)count / (sw.ElapsedMilliseconds / 1000.0)));

            var repeated = lag.Repeat();

            for (int i = 1; i < 1000; i++)
            {
                double v;
                Assert.IsTrue(repeated.TryGetValue(i+1.5, out v));
                Assert.AreEqual(i, v);
            }

        }
Beispiel #30
0
        public void CouldNotEnumerateChangingSM()
        {
            var count = 1000000;
            var sw    = new Stopwatch();

            sw.Start();
            var sm = new SortedMap <DateTime, double>();
            var c  = sm.GetCursor();

            Assert.Throws <OutOfOrderKeyException <DateTime> >(() =>
            {
                for (int i = 0; i < count; i++)
                {
                    sm.Add(DateTime.UtcNow.Date.AddSeconds(i), i);
                    var version = sm.version;
                    if (i > 10)
                    {
                        sm[DateTime.UtcNow.Date.AddSeconds(i - 10)] = i - 10 + 1;
                        Assert.IsTrue(sm.version > version);
                    }
                    c.MoveNext();
                    Assert.AreEqual(i, c.CurrentValue);
                }
            });

            sw.Stop();
            Console.WriteLine("Elapsed msec: {0}", sw.ElapsedMilliseconds - 50);
            Console.WriteLine("Ops: {0}", Math.Round(0.000001 * count * 1000.0 / (sw.ElapsedMilliseconds * 1.0), 2));
        }
Beispiel #31
0
        public void ZipLagIssue11Test()
        {
            OptimizationSettings.CombineFilterMapDelegates = true;

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

            var count = 5000;

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

            var sma = data.SMA(20, true).Lag(1u);

            Console.WriteLine($"laggedSma count: {sma.Count()}");

            var deviation = sma;

            // this line or any other intermediate enumeration affect the last line
            Console.WriteLine($"deviation count: {deviation.Count()}");

            var direction = deviation;//.Map(x => (Math.Sign(x)));

            Console.WriteLine($"direction count: {direction.Count()}");

            var diff = direction.ZipLag(1u, (c, p) => c - p); //.ToSortedMap();

            Console.WriteLine($"Count: {diff.Count()}");

            Assert.IsTrue(diff.Count() > 0);
        }
        public void CouldMoveAtWithBatching()
        {
            var sm = new SortedMap <DateTime, double>();

            var count = 1000;

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

            var bmvc = new BatchMapValuesCursor <DateTime, double, double>(sm.GetCursor, (v) => v + 1.0, this.IncrementMap);
            var c    = 0;

            while (c < 500 && bmvc.MoveNext())
            {
                Assert.AreEqual(c + 1.0, bmvc.CurrentValue);
                c++;
            }
            c--;
            while (bmvc.MoveAt(DateTime.UtcNow.Date.AddSeconds(c), Lookup.EQ))
            {
                Assert.AreEqual(c + 1.0, bmvc.CurrentValue);
                c--;
            }
            Assert.AreEqual(-1, c);
        }
Beispiel #33
0
        public void CouldWriteToStorage()
        {
            var repo = new SeriesStorage(SeriesStorage.GetDefaultConnectionString("../StorageTests.db"));
            var test = new SortedMap <DateTime, double>();

            for (int i = 0; i < 10; i++)
            {
                test.Add(DateTime.UtcNow.Date.AddSeconds(i), i);
            }
            test.Complete();
            foreach (var kvp in test.Map(x => (decimal)x))
            {
                Console.WriteLine($"{kvp.Key} - {kvp.Key.Kind} - {kvp.Value}");
            }

            var storageSeries = repo.GetPersistentOrderedMap <DateTime, decimal>("test_series_CouldWriteToStorage");
            var test2         = storageSeries.ToSortedMap();

            foreach (var kvp in test2)
            {
                Console.WriteLine($"{kvp.Key} - {kvp.Key.Kind} - {kvp.Value}");
            }
            storageSeries.Append(test.Map(x => (decimal)x), AppendOption.RequireEqualOverlap);
            storageSeries.Flush();
        }
        public void CouldFillSeries()
        {
            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;;
            }

            var sw = new Stopwatch();

            sw.Start();
            var sum = (sm.Fill(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)));
        }
Beispiel #35
0
        public void CouldScanSeries()
        {
            OptimizationSettings.CombineFilterMapDelegates = true;

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

            var count = 5000;

            for (int i = 0; i < count; i++)
            {
                data.Add(DateTime.UtcNow.Date.AddSeconds(i), i);
            }
            var sign        = 1;
            var runnningSum = data.Zip(data, (d1, d2) => d1 + d2).Scan(0.0, (st, k, v) =>
            {
                if (st > 100)
                {
                    sign = -1;
                }
                if (st < -100)
                {
                    sign = 1;
                }
                return(st += sign * v);
            });
            var runnningSumSm = runnningSum.ToSortedMap();

            Assert.AreEqual(runnningSum.Count(), count);
        }
        public void CouldZipOneEmptyFillSeries()
        {
            var sm  = new SortedMap <DateTime, double>();
            var sm2 = new SortedMap <DateTime, double>();

            sm2.Add(DateTime.Today, 42.0);
            var one   = sm.Fill(1.0);
            var two   = sm2.Fill(2.0);
            var three = one + two;

            var threeeCursor = three.GetCursor();

            Assert.IsTrue(threeeCursor.MoveNext());
            Assert.AreEqual(DateTime.Today, threeeCursor.CurrentKey);
            Assert.AreEqual(43.0, threeeCursor.CurrentValue);

            double v;

            Assert.IsTrue(threeeCursor.TryGetValue(DateTime.Now, out v));
            Assert.AreEqual(3.0, v);
            Assert.IsTrue(three.TryGetValue(DateTime.Now, out v));
            Assert.AreEqual(3.0, v);

            Assert.IsTrue(three.TryGetValue(DateTime.Today, out v));
            Assert.AreEqual(43.0, v);
        }
Beispiel #37
0
        public void CouldZipLagSeries()
        {
            var sm = new SortedMap <DateTime, double>();

            var count = 10000000;

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

            // slow implementation
            var sw = new Stopwatch();

            sw.Start();
            var zipLag = sm.ZipLag(1, (cur, prev) => cur + prev); //.ToSortedMap();
            var c      = 1;

            foreach (var zl in zipLag)
            {
                if (c + (c - 1) != zl.Value)
                {
                    throw new ApplicationException();
                }
                c++;
            }
            sw.Stop();
            Console.WriteLine($"Final c: {c}");
            Console.WriteLine("ZipLag, elapsed: {0}, ops: {1}", sw.ElapsedMilliseconds, (int)((double)count / (sw.ElapsedMilliseconds / 1000.0)));
        }
Beispiel #38
0
 public void CouldMoveAsyncOnEmptySM() {
     var sm = new SortedMap<DateTime, double>();
     var c = sm.GetCursor();
     var moveTask = c.MoveNext(CancellationToken.None);
     sm.Add(DateTime.UtcNow.Date.AddSeconds(0), 0);
     var result = moveTask.Result;
     Assert.IsTrue(result);
 }
        public void UpdateEventIsTriggered() {
            var sm = new SortedMap<DateTime, double>();
            (sm as IObservableEvents<DateTime, double>).OnNext += (kvp) => {
                Console.WriteLine("Added {0} : {1}", kvp.Key, kvp.Value);
            };

            sm.Add(DateTime.UtcNow.Date.AddSeconds(0), 0);

        }
Beispiel #40
0
        public void UpdateEventIsTriggered() {
            var sm = new SortedMap<DateTime, double>();
            (sm as IUpdateable<DateTime, double>).OnData += (s, x) => {
                Console.WriteLine("Added {0} : {1}", x.Key,
                    x.Value);
            };

            sm.Add(DateTime.UtcNow.Date.AddSeconds(0), 0);

        }
Beispiel #41
0
		public void CouldEnumerateGrowingSM() {
            var count = 1000000;
            var sw = new Stopwatch();
            sw.Start();
            var sm = new SortedMap<DateTime, double>();
            var c = sm.GetCursor();

            for (int i = 0; i < count; i++) {
                sm.Add(DateTime.UtcNow.Date.AddSeconds(i), i);
                c.MoveNext();
                Assert.AreEqual(i, c.CurrentValue);
            }
            sw.Stop();
            Console.WriteLine("Elapsed msec: {0}", sw.ElapsedMilliseconds - 50);
            Console.WriteLine("Ops: {0}", Math.Round(0.000001 * count * 1000.0 / (sw.ElapsedMilliseconds * 1.0), 2));

        }
        public void CouldCalculateIncompleteMovingAverage() {
            var sm = new SortedMap<int, double>();
            for (int i = 0; i < 20; i++) {
                sm.Add(i, i);
            }

            var sma = sm.SMA(2, true).ToSortedMap();

            var c = 0;
            foreach (var kvp in sma) {
                if (c == 0) {
                    Assert.AreEqual(c, kvp.Value);
                } else {
                    Assert.AreEqual(0.5 * (c + (double)(c - 1)), kvp.Value);
                }

                c++;
            }

        }
        public void CouldCalculateComplexGraph() {
            // TODO! need real complex data to test properly
            var sm = new SortedMap<DateTime, double>();

            var dataTask = Task.Run(async () => {

                for (int i = 0; i < 1000; i++) {
                    sm.Add(DateTime.Today.AddSeconds(i), i+10000);
                    await Task.Delay(25);
                }
                sm.IsMutable = false;
            });

            Thread.Sleep(50);

            var closeSeries = sm;

            var baseLeverage = 1.0;

            var sma = closeSeries.SMA(20, true);
            var deviation = sma / closeSeries - 1.0;
            var leverage = (baseLeverage * (-(5.0 * (deviation.Map(x => Math.Abs(x)))) + 1.0));

            var smaSignal = deviation.Map(x => (double)(Math.Sign(x)));

            var smaPositionMultiple = ((smaSignal * leverage).Map(x => 0.25 * (Math.Round(x / 0.25))));
            var smaPositionMultipleMap = smaPositionMultiple.ToSortedMap();

            var traderTask = Task.Run(async () => {
                var positionCursor = smaPositionMultiple.GetCursor();
                while (await positionCursor.MoveNext(CancellationToken.None)) //
                {
                    await Task.Delay(15);
                    Console.WriteLine("Time: {0}, position: {1}", positionCursor.CurrentKey, positionCursor.CurrentValue);

                }
            });

            dataTask.Wait();
            traderTask.Wait();
        }
        public void CouldMoveNextWithoutBatching() {
            var sm = new SortedMap<DateTime, double>();

            var count = 1000;

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

            var bmvc = new BatchMapValuesCursor<DateTime, double, double>(sm.GetCursor, (v) => v + 1.0);
            var c = 0;
            while (c < 500 && bmvc.MoveNext()) {
                Assert.AreEqual(c + 1.0, bmvc.CurrentValue);
                c++;
            }

            while (bmvc.MoveNext(CancellationToken.None).Result) { // Setting IsMutable to false allows us to skip this check: c < 1000 &&
                Assert.AreEqual(c + 1.0, bmvc.CurrentValue);
                c++;
            }
            Assert.AreEqual(count, c);
        }
        public void CouldMovePreviousWithoutBatching() {
            var sm = new SortedMap<DateTime, double>();

            var count = 1000;

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

            var bmvc = new BatchMapValuesCursor<DateTime, double, double>(sm.GetCursor, (v) => v + 1.0);
            var c = 0;
            while (c < 500 && bmvc.MoveNext()) {
                Assert.AreEqual(c + 1.0, bmvc.CurrentValue);
                c++;
            }
            c--;
            while (bmvc.MovePrevious()) {
                c--;
                Assert.AreEqual(c + 1.0, bmvc.CurrentValue);

            }
            Assert.AreEqual(0, c);
        }
Beispiel #46
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());


        }
Beispiel #47
0
        public void ZipLagIssue11Test() {
            OptimizationSettings.CombineFilterMapDelegates = true;

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

            var count = 5000;

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

            var sma = data.SMA(20, true).Lag(1u);
            Console.WriteLine($"laggedSma count: {sma.Count()}");

            var deviation = sma;

            // this line or any other intermediate enumeration affect the last line
            Console.WriteLine($"deviation count: {deviation.Count()}");

            var direction = deviation;//.Map(x => (Math.Sign(x)));

            Console.WriteLine($"direction count: {direction.Count()}");

            var diff = direction.ZipLag(1u, (c, p) => c - p); //.ToSortedMap();

            Console.WriteLine($"Count: {diff.Count()}");

            Assert.IsTrue(diff.Count() > 0);
        }
Beispiel #48
0
        public void CouldCalculateMovingStDevIncomlete() {
            var sm = new SortedMap<DateTime, double>();

            var count = 1000000;

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

            // slow implementation
            var sw = new Stopwatch();
            sw.Start();
            var ma = sm.StDev(20, true); //.ToSortedMap();
            var c = 1;
            foreach (var m in ma) {
                if (c < 30) {
                    Console.WriteLine(m.Value);
                } else
                // TODO on c = 9490618 we have a different value: 5.91740018927231
                //   Excel value       5.91607978309962

                if (Math.Abs(m.Value - 5.9160797830996161) > 0.0000001) {
                    Console.WriteLine(m.Value);
                    Console.WriteLine($"Error c: {c}");
                    throw new ApplicationException("Invalid value");

                }
                c++;
            }
            sw.Stop();
            Console.WriteLine($"Final c: {c}");
            Console.WriteLine("SMA, elapsed: {0}, ops: {1}", sw.ElapsedMilliseconds, (int)((double)count / (sw.ElapsedMilliseconds / 1000.0)));
            // 
        }
Beispiel #49
0
        public void CouldCalculateMovingAverageIncomplete() {
            var sm = new SortedMap<DateTime, double>();

            var count = 100000;

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

            // slow implementation
            var sw = new Stopwatch();
            sw.Start();
            var ma = sm.SMA(20, true); //.ToSortedMap();
            var c = 1;
            foreach (var m in ma) {
                if (c <= 20) {
                    //Console.WriteLine(m.Value);
                } else if (m.Value != c - 19 + 8.5) {
                    Console.WriteLine(m.Value);
                    throw new ApplicationException("Invalid value");
                }
                c++;
            }
            sw.Stop();
            Console.WriteLine($"Final c: {c}");
            Console.WriteLine("SMA, elapsed: {0}, ops: {1}", sw.ElapsedMilliseconds, (int)((double)count / (sw.ElapsedMilliseconds / 1000.0)));
            // 
        }
Beispiel #50
0
        public void CouldCalculateMovingAverage() {
            var sm = new SortedMap<DateTime, double>();

            var count = 1000000;

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

            // slow implementation
            var sw = new Stopwatch();
            sw.Start();
            var ma = sm.SMA(20); //.ToSortedMap();

            var cursor = ma.GetCursor();
            cursor.MoveNext();
            //var cc = 0;
            //while (cursor.MoveNext())
            //{
            //    cc++;
            //}
            //Console.WriteLine(cc);
            //if (cursor.MoveNext())
            //{
            //    throw new ApplicationException("Moved next after MoveNext() returned false");
            //}
            //cursor.MoveFirst();
            var c = 1;
            //foreach (var m in ma) {
            cursor.Reset();
            while (cursor.MoveNext()) {
                if (cursor.CurrentValue != c + 8.5)
                {
                    Console.WriteLine(cursor.CurrentValue);// m.Value);
                    Console.WriteLine($"Error c: {c}");
                    throw new ApplicationException("Invalid value");
                }
                c++;
                if (c == 999982)
                {
                    Console.WriteLine("Catch me");
                }
            }
            sw.Stop();
            Console.WriteLine($"Final c: {c}");
            Console.WriteLine("SMA, elapsed: {0}, ops: {1}", sw.ElapsedMilliseconds, (int)((double)count / (sw.ElapsedMilliseconds / 1000.0)));
            ma = null;
            
            GC.Collect(3, GCCollectionMode.Forced, true);
            Thread.Sleep(2000);
            // NB! In release mode this must print that ToSortedMap task exited, in Debug mode GC does not collect SM and weak reference stays alive
             
        }
Beispiel #51
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]);
            }

        }
Beispiel #52
0
        public void SmaDeviationTest()
        {
            var data = new SortedMap<DateTime, double>();
            for (int i = 1; i < 101; i++)
            {
                data.Add(DateTime.UtcNow.Date.AddMinutes(i), i);
            }

            var sma = data.SMA(20, true);
            var deviation = (data/sma - 1);
            var deviationSm = deviation;
            var smaDirection = deviation.Map(Math.Sign);
            Assert.AreEqual(100, smaDirection.Count());
            Assert.AreEqual(100, deviation.Count());
            
        }
Beispiel #53
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]);
            //}

        }
Beispiel #54
0
        public void CouldZipMillionIntsMovePreviousBenchmark() {
            var sw = new Stopwatch();

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

            sm1.Add(0, 0);

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

            var series = new[] { sm1, sm1, sm1, sm1, sm1, };// sm1, sm1, sm1, sm1, sm1,        sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, };

            sw.Start();

            var cur = series.Zip((k, varr) => varr.Sum()).GetCursor();
            var totalSum = 0L;
            while (cur.MovePrevious()) {
                totalSum += cur.CurrentValue;
            }

            var expectedTotalSum = 0L;
            var cur2 = sm1.GetCursor();
            while (cur2.MovePrevious()) {
                expectedTotalSum += cur2.CurrentValue;
            }
            expectedTotalSum *= 5;

            sw.Stop();
            Assert.AreEqual(expectedTotalSum, totalSum, "Sums are not equal");
            Console.WriteLine("Elapsed msec: {0}", sw.ElapsedMilliseconds);
            Console.WriteLine("Total sum: {0}", totalSum);

        }
Beispiel #55
0
        public void CouldZipManyIntsx500() {

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

            var sw = new Stopwatch();

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

            sm1.Add(0, 0);

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

            var series = new[]
            {
                sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1,  sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1,
                sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1,  sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1,
                sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1,  sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1,
                sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1,  sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1,
                sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1,  sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1,

                sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1,  sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1,
                sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1,  sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1,
                sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1,  sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1,
                sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1,  sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1,
                sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1,  sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1,

                sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1,  sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1,
                sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1,  sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1,
                sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1,  sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1,
                sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1,  sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1,
                sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1,  sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1,

                sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1,  sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1,
                sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1,  sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1,
                sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1,  sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1,
                sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1,  sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1,
                sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1,  sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1,

                sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1,  sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1,
                sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1,  sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1,
                sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1,  sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1,
                sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1,  sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1,
                sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1,  sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1, sm1,
            };

            sw.Start();

            var sum = series.Zip((k, varr) => varr.Sum());
            var totalSum = 0L;
            foreach (var kvp in sum) {
                totalSum += kvp.Value;
            }
            sw.Stop();
            Console.WriteLine("Elapsed msec: {0}", sw.ElapsedMilliseconds);
            Console.WriteLine("Total sum: {0}", totalSum);
            Console.WriteLine("Number of series: {0}", series.Length);


        }
Beispiel #56
0
        public void CouldZipManyNonContinuousInRealTime() {

            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, sm2 };

            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);


        }
Beispiel #57
0
        public void CouldCloneZipLagSeries() {
            

            var count = 1000;
            var sm = new SortedMap<int, double>();
            for (int i = 0; i < count; i++) {
                sm.Add(i, i);
            }

            // slow implementation
            var sw = new Stopwatch();
            sw.Start();
            var zipLag = sm.ZipLag(1, (cur, prev) => cur + prev); //.ToSortedMap();

            var zc = zipLag.GetCursor();
            zc.MoveNext();
            var zc2 = zc.Clone();
            Assert.AreEqual(zc.CurrentKey, zc2.CurrentKey);
            zc.MoveNext();
            zc2.MoveNext();
            Assert.AreEqual(zc.CurrentKey, zc2.CurrentKey);
            zc.MovePrevious();
            zc2.MovePrevious();
            Assert.AreEqual(zc.CurrentKey, zc2.CurrentKey);


            for (int i = 1; i < count; i++)
            {
                var expected = i + i - 1;
                double actual;
                var ok = zc.TryGetValue(i, out actual);
                Assert.AreEqual(expected, actual);
            }

            var sm2 = new SortedMap<int, double>();
            var zc3 = sm2.ZipLag(1, (cur, prev) => cur + prev).GetCursor();

            var t = Task.Run(async () =>
            {
                var c = 1; // first key is missing because we cannot create state at it
                while (await zc3.MoveNext(CancellationToken.None))
                {
                    var expected = c + c - 1;
                    Assert.AreEqual(expected, zc3.CurrentValue);
                    c++;
                }
            });

            for (int i = 0; i < count; i++) {
                sm2.Add(i, i);
            }
            sm2.IsMutable = false; // without it MoveNextAsync will wait forever
            t.Wait();
        }
Beispiel #58
0
        public void CouldCalculateAverageOnMovingWindowWithStep() {
            var sm = new SortedMap<DateTime, double>();

            var count = 100000;

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

            // slow implementation
            var sw = new Stopwatch();
            sw.Start();
            var ma = sm.Window(20, 2);//.ToSortedMap();
            var c = 1;
            foreach (var m in ma) {
                var innersm = m.Value;//.ToSortedMap();
                if (innersm.Values.Average() != c + 8.5) {
                    Console.WriteLine(m.Value.Values.Average());
                    throw new ApplicationException("Invalid value");
                }
                c++;
                c++;
            }
            sw.Stop();
            Console.WriteLine($"Final c: {c}");
            Console.WriteLine("Window MA, elapsed: {0}, ops: {1}", sw.ElapsedMilliseconds, (int)((double)count / (sw.ElapsedMilliseconds / 1000.0)));
            Console.WriteLine("Calculation ops: {0}", (int)((double)count * 20.0 / (sw.ElapsedMilliseconds / 1000.0)));
        }
Beispiel #59
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);

        }
Beispiel #60
-1
		public Dictionary<string, IReadOnlyOrderedMap<DateTime, double>> GetImplementation() {

			var implemetations = new Dictionary<string, IReadOnlyOrderedMap<DateTime, double>>();
			var sm_irregular_small = new SortedMap<DateTime, double>();
			var sm_irregular_big = new SortedMap<DateTime, double>();
			var sm_regular_small = new SortedMap<DateTime, double>();
			var sm_regular_big = new SortedMap<DateTime, double>();

			var scm_irregular_small = new SortedChunkedMap<DateTime, double>();
			var scm_irregular_big = new SortedChunkedMap<DateTime, double>();
			var scm_regular_small = new SortedChunkedMap<DateTime, double>();
			var scm_regular_big = new SortedChunkedMap<DateTime, double>();

			sm_irregular_small.Add(DateTime.Today.AddDays(-2), -2.0);
			sm_irregular_big.Add(DateTime.Today.AddDays(-2), -2.0);
			scm_irregular_small.Add(DateTime.Today.AddDays(-2), -2.0);
			scm_irregular_big.Add(DateTime.Today.AddDays(-2), -2.0);

			for (int i = 0; i < _big; i++)
			{
				if (i < _small)
				{
					sm_irregular_small.Add(DateTime.Today.AddDays(i), i);
					sm_regular_small.Add(DateTime.Today.AddDays(i), i);
					scm_irregular_small.Add(DateTime.Today.AddDays(i), i);
					scm_regular_small.Add(DateTime.Today.AddDays(i), i);
				}

				sm_irregular_big.Add(DateTime.Today.AddDays(i), i);
				sm_regular_big.Add(DateTime.Today.AddDays(i), i);
				scm_irregular_big.Add(DateTime.Today.AddDays(i), i);
				scm_regular_big.Add(DateTime.Today.AddDays(i), i);
			}
			// SM regular
			implemetations.Add("sm_irregular_small", sm_irregular_small);
			implemetations.Add("sm_regular_small", sm_regular_small);
			implemetations.Add("scm_irregular_small", scm_irregular_small);
			implemetations.Add("scm_regular_small", scm_regular_small);
			implemetations.Add("sm_irregular_big", sm_irregular_big);
			implemetations.Add("sm_regular_big", sm_regular_big);
			implemetations.Add("scm_irregular_big", scm_irregular_big);
			implemetations.Add("scm_regular_big", scm_regular_big);
			return implemetations;
		}