/// <summary> /// Get history of offsets with keys as zoned time. Used to convert from zoned to UTC time. /// </summary> /// <returns></returns> public static SortedMap <DateTime, long> GetOffsetsFromZoned(string tzFrom, bool standardOffsetOnly = false) { string tz; if (!Normalizer.TryGetValue(tzFrom.ToLowerInvariant(), out tz)) { tz = tzFrom; } var sortedMap = new SortedMap <DateTime, long>(); if (tz.ToLowerInvariant() == "utc") { sortedMap.Set(new DateTime(0L, DateTimeKind.Unspecified), 0); } else { var givenTz = DateTimeZoneProviders.Tzdb[tz]; var intervals = givenTz.GetZoneIntervals(Instant.FromDateTimeUtc( // https://en.wikipedia.org/wiki/International_Meridian_Conference new DateTime(1884, 10, 22, 12, 0, 0, DateTimeKind.Utc) ), Instant.MaxValue); foreach (var interval in intervals) { var localStart = interval.IsoLocalStart.ToDateTimeUnspecified(); var offset = standardOffsetOnly ? interval.StandardOffset : interval.WallOffset; var offsetTicks = offset.Ticks; sortedMap.TryAddLast(localStart, offsetTicks); } } sortedMap.Complete(); return(sortedMap); }
public async Task MNATest() { var sm = new SortedMap <int, int>(); var count = 1_0; var sum = 0; using (Benchmark.Run("MNA")) { var _ = Task.Run(() => { for (int i = 0; i < count; i++) { sm.TryAddLast(i, i); } sm.Complete(); }); var c = sm.GetCursor(); while (await c.MoveNextAsync()) { sum += c.CurrentValue; } } Assert.IsTrue(sum > 0); Benchmark.Dump(); }
public async Task CouldReadDataStreamWhileWritingFromManyThreads() { var map = new SortedMap <int, int>(); var count = 1_000_000; var rounds = 5; var writeTask = Task.Run(async() => { using (Benchmark.Run("Write", count * rounds, true)) { for (int j = 0; j < rounds; j++) { var t1 = Task.Run(async() => { try { for (int i = j * count; i < (j + 1) * count; i++) { await map.TryAddLast(i, i); Thread.SpinWait(10); } } catch (Exception e) { Console.WriteLine(e); throw; } }); await t1; } } }); AsyncCursor <int, int, SortedMapCursor <int, int> > cursor = null; var cnt = 0L; var readTask = Task.Run(async() => { for (int r = 0; r < 1; r++) { using (cursor = new AsyncCursor <int, int, SortedMapCursor <int, int> >(map.GetCursor())) { using (Benchmark.Run("Read", count * rounds, true)) { try { while (await cursor.MoveNextAsync()) { Interlocked.Increment(ref cnt); } } catch (Exception e) { Console.WriteLine(e); throw; } // Left from coreclr 19161 tests, TODO remove when everything works OK // here is a strong reference to cursor with side effects of printing to console // Console.WriteLine("Last value: " + cursor.Current.Key); // another strong reference after while loop, we dereference it's value and return from task // lastKey1 = cursor.CurrentKey; } } } }); var monitor = true; var t = Task.Run(async() => { try { while (monitor) { await Task.Delay(1000); Console.WriteLine($"Key {cursor.CurrentKey}"); cursor.TryComplete(false); } } catch (Exception e) { Console.WriteLine(e); throw; } }); await writeTask; await map.Complete(); map.NotifyUpdate(true); Console.WriteLine("Read after map complete:" + Interlocked.Read(ref cnt)); await readTask; Console.WriteLine("Read after finish:" + Interlocked.Read(ref cnt)); // Console.WriteLine("Last key: " + lastKey); Benchmark.Dump(); map.Dispose(); monitor = false; }