public async void CouldReadReadOnlyChildWhileAddingToParent() { // TODO if we change the first element to -1 and add from 0, some weird assertion related to regular keys fails var total = 1000; var scm = new SortedChunkedMap<int, int>(50); scm.IsSynchronized = true; scm.AddLast(1, 1); var addTask = Task.Run(() => { for (int i = 2; i < total + 2; i++) { scm.Add(i, i); //scm[i] = i; Thread.Sleep(5); } scm.Complete(); // this will trigger a false return of MoveNextAsync() }); var reader = scm.ReadOnly(); Console.WriteLine("Writer IsReadOnly: {0}", scm.IsReadOnly); Console.WriteLine("Reader IsReadOnly: {0}", reader.IsReadOnly); var cnt = 0; using (var c = reader.GetCursor()) { var couldMove = await c.MoveNext(CancellationToken.None); while (couldMove) { if (cnt % 100 == 0) Console.WriteLine("{0} - {1}", c.CurrentKey, c.CurrentValue); cnt++; couldMove = await c.MoveNext(CancellationToken.None); } } addTask.Wait(); Assert.AreEqual(cnt, total + 1); Console.WriteLine("Writer IsReadOnly: {0}", scm.IsReadOnly); Console.WriteLine("Reader IsReadOnly: {0}", reader.IsReadOnly); //(scm as IPersistentOrderedMap<int, int>).Dispose(); }
public async void CouldReadReadOnlyChildWhileAddingToParent() { // TODO if we change the first element to -1 and add from 0, some weird assertion related to regular keys fails var total = 150; var scm = new SortedChunkedMap <int, int>(50); scm.IsSynchronized = true; scm.AddLast(1, 1); var cc = 1; var addTask = Task.Run(() => { for (int i = 2; i < total + 2; i++) { cc++; if (cc == 50) { Console.WriteLine("Next bucket"); } scm.Add(i, i); //scm[i] = i; Thread.Sleep(5); } scm.Complete(); // this will trigger a false return of MoveNextAsync() }); //Thread.Sleep(5000000); var reader = scm.ReadOnly(); Console.WriteLine("Writer IsReadOnly: {0}", scm.IsReadOnly); Console.WriteLine("Reader IsReadOnly: {0}", reader.IsReadOnly); var cnt = 0; using (var c = reader.GetCursor()) { var couldMove = await c.MoveNext(CancellationToken.None); while (couldMove) { if (cnt % 100 == 0) { Console.WriteLine("{0} - {1}", c.CurrentKey, c.CurrentValue); } cnt++; couldMove = await c.MoveNext(CancellationToken.None); } } addTask.Wait(); Thread.Sleep(200); Assert.AreEqual(cnt, total + 1); Console.WriteLine("Writer IsReadOnly: {0}", scm.IsReadOnly); Console.WriteLine("Reader IsReadOnly: {0}", reader.IsReadOnly); //(scm as IPersistentSeries<int, int>).Dispose(); }
public void CouldUseDoExtensionMethod() { var count = 100; var sw = new Stopwatch(); sw.Start(); var sm = new SortedChunkedMap <DateTime, double>(); sm.IsSynchronized = true; var addTask = Task.Run(async() => { await Task.Delay(50); for (int i = 0; i < count; i++) { sm.Add(DateTime.UtcNow.Date.AddSeconds(i), i); } // signal data completion sm.Complete(); }); var cached = sm; //.Cache(); double sum = 0.0; var doTask = cached.Do((k, v) => { Console.WriteLine($"{k} - {v}"); sum += v; }); addTask.Wait(); doTask.Wait(); //Thread.Sleep(100); sw.Stop(); double expectedSum = 0.0; for (int i = 0; i < count; i++) { expectedSum += i; } Assert.AreEqual(expectedSum, sum); 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 CouldCalculateSMAInRealTime() { var totalCount = 10000; var sm = new SortedChunkedMap <int, double>(); var tcs = new TaskCompletionSource <bool>(); Task.Run(async() => { for (int i = 0; i < 20; i++) { sm.Add(i, i); if (i == 0) { tcs.SetResult(true); } } await Task.Delay(100); for (int i = 20; i < totalCount; i++) { //await Task.Delay(1); // 15 msec sm.Add(i, i); } sm.Complete(); }); var sma = sm.SMA(10, true); var c = sma.GetCursor(); //Thread.Sleep(200); Assert.IsTrue(tcs.Task.Result); Assert.IsTrue(c.MoveNext(CancellationToken.None).Result); Assert.IsTrue(c.MoveNext()); var ii = 2; while (c.MoveNext(CancellationToken.None).Result) // //Console.WriteLine("Key: {0}, value: {1}", c.CurrentKey, c.CurrentValue); { ii++; } Assert.AreEqual(totalCount, ii); Console.WriteLine("finished"); }
public void AsyncCoursorOnEmptyMapWaitsForValues() { var scm = new SortedChunkedMap <int, double>(); var scmSma = scm.SMA(20, true); var c = scmSma.GetCursor(); var task = c.MoveNext(CancellationToken.None); var moved = task.Wait(100); // timeout Assert.IsFalse(moved); scm.Add(1, 1.0); Assert.IsTrue(task.Result); Console.WriteLine("moved " + task.Result); task = c.MoveNext(CancellationToken.None); moved = task.Wait(100); // timeout Assert.IsFalse(moved); scm.Complete(); Assert.IsFalse(task.Result); Console.WriteLine("moved " + task.Result); }
public void TGVSpeed() { for (int size = 0; size < 3; size++) { var count = (int)(1024 * Math.Pow(2, size)); const int mult = 1000; var sl = new SortedList <DateTime, int>(); var sm = new SortedMap <DateTime, int>(count); var scm = new SortedChunkedMap <DateTime, int>(); var start = DateTime.Today.ToUniversalTime(); for (int i = 0; i < count; i++) { if (i != 2) // make irregular { sl.Add(start.AddTicks(i), i); sm.Add(start.AddTicks(i), i); scm.Add(start.AddTicks(i), i); } } Assert.IsFalse(sm.IsCompleted); Assert.IsFalse(sm.IsCompleted); Assert.IsTrue(sm.IsSynchronized); sm.Complete(); Assert.IsTrue(sm.IsCompleted); Assert.IsTrue(sm.IsCompleted); Assert.IsFalse(sm.IsSynchronized); scm.Complete(); for (int r = 0; r < 20; r++) { //var sum1 = 0L; //using (Benchmark.Run("SL", count * mult, true)) //{ // for (int j = 0; j < mult; j++) // { // for (int i = 0; i < count; i++) // { // if (sl.TryGetValue(start.AddTicks(i), out var v)) // { // sum1 += v; // } // } // } //} //Assert.True(sum1 > 0); var sum2 = 0L; using (Benchmark.Run("SM", count * mult, true)) { for (int j = 0; j < mult; j++) { for (int i = 0; i < count; i++) { if (sm.TryGetValueUnchecked(start.AddTicks(i), out var v)) { sum2 += v; } } } } //Assert.True(sum2 > 0); //Assert.AreEqual(sum1, sum2); //var sum3 = 0L; //using (Benchmark.Run("SCM", count * mult, true)) //{ // for (int j = 0; j < mult; j++) // { // for (int i = 0; i < count; i++) // { // if (scm.TryGetValue(start.AddTicks(i), out var v)) // { // sum3 += v; // } // } // } //} //Assert.True(sum3 > 0); //Assert.AreEqual(sum2, sum3); } Benchmark.Dump($"Size = {Math.Pow(2, size)}k elements"); } }
public void CouldReadSortedMapNewValuesWhileTheyAreAddedUsingCursor() { var count = 1000000; var sw = new Stopwatch(); //var mre = new ManualResetEventSlim(true); sw.Start(); //var sm = new SortedMap<DateTime, double>(); var sm = new SortedChunkedMap<DateTime, double>(); //sm.Add(DateTime.UtcNow.Date.AddSeconds(-2), 0); sm.IsSynchronized = true; for (int i = 0; i < 5; i++) { sm.Add(DateTime.UtcNow.Date.AddSeconds(i), i); } var addTask = Task.Run(async () => { await Task.Delay(10); for (int i = 5; i < count; i++) { //mre.Wait(); sm.Add(DateTime.UtcNow.Date.AddSeconds(i), i); //await Task.Delay(1); //NOP(500); } sm.Complete(); }); double sum = 0.0; int cnt = 0; var sumTask = Task.Run(async () => { var c = sm.GetCursor(); while (c.MoveNext()) { if ((int)c.CurrentValue != cnt) { Console.WriteLine("Wrong sequence"); } else { //Console.WriteLine("Sync move"); } sum += c.CurrentValue; cnt++; } //Assert.AreEqual(10, sum); await Task.Delay(30); while (await c.MoveNext(CancellationToken.None)) { //mre.Reset(); sum += c.CurrentValue; if ((int)c.CurrentValue != cnt) { //Console.WriteLine("Wrong sequence"); Assert.Fail("Wrong sequence"); } else { //Console.WriteLine("Async move"); } cnt++; //mre.Set(); } Console.WriteLine("Finished 1"); }); double sum2 = 0.0; var sumTask2 = Task.Run(async () => { var c = sm.GetCursor(); while (c.MoveNext()) { sum2 += c.CurrentValue; } Assert.AreEqual(10, sum2); await Task.Delay(50); while (await c.MoveNext(CancellationToken.None)) { //mre.Reset(); sum2 += c.CurrentValue; //mre.Set(); } Console.WriteLine("Finished 2"); }); double sum3 = 0.0; var sumTask3 = Task.Run(async () => { var c = sm.GetCursor(); while (c.MoveNext()) { sum3 += c.CurrentValue; } Assert.AreEqual(10, sum3); await Task.Delay(100); while (await c.MoveNext(CancellationToken.None)) { //mre.Reset(); sum3 += c.CurrentValue; //mre.Set(); } Console.WriteLine("Finished 3"); }); sumTask.Wait(); sumTask2.Wait(); sumTask3.Wait(); addTask.Wait(); 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)); double expectedSum = 0.0; for (int i = 0; i < count; i++) { expectedSum += i; } if (expectedSum != sum) Console.WriteLine("Sum 1 is wrong"); if (expectedSum != sum2) Console.WriteLine("Sum 2 is wrong"); if (expectedSum != sum3) Console.WriteLine("Sum 3 is wrong"); Assert.AreEqual(expectedSum, sum, "Sum 1"); Assert.AreEqual(expectedSum, sum2, "Sum 2"); Assert.AreEqual(expectedSum, sum3, "Sum 3"); }
public void CouldUseDoExtensionMethodOnRange() { var count = 100; var sw = new Stopwatch(); sw.Start(); var sm = new SortedChunkedMap<DateTime, double>(); sm.IsSynchronized = true; var addTask = Task.Run(async () => { await Task.Delay(50); for (int i = 0; i < count; i++) { sm.Add(DateTime.UtcNow.Date.AddSeconds(i), i); } // signal data completion sm.Complete(); }); var cached = sm.Cache().After(DateTime.UtcNow.Date.AddSeconds(0), Lookup.EQ); double sum = 0.0; cached.Do((k, v) => { sum += v; Console.WriteLine($"{k} : {v}"); }); addTask.Wait(); Thread.Sleep(100); sw.Stop(); double expectedSum = 0.0; for (int i = 0; i < count; i++) { expectedSum += i; } Assert.AreEqual(expectedSum, sum); Console.WriteLine("Elapsed msec: {0}", sw.ElapsedMilliseconds - 50); Console.WriteLine("Ops: {0}", Math.Round(0.000001 * count * 1000.0 / (sw.ElapsedMilliseconds * 1.0), 2)); }