public async Task CouldReadDataStreamWhileWritingFromManyThreads()
        {
            var map = new AppendSeries <int, int>();

            var writeCount = 0L;

            var count  = 1000_000_000;
            var rounds = 1;

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

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


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


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

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

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

            await writeTask;

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

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

            Benchmark.Dump();
            GC.KeepAlive(cursor);
            map.Dispose();
            monitor = false;
        }
Exemple #2
0
        public void CouldAppendSeriesBench()
        {
            if (AdditionalCorrectnessChecks.Enabled)
            {
                Console.WriteLine("AdditionalCorrectnessChecks.Enabled");
            }

            int count  = (int)TestUtils.GetBenchCount(10_000_000, 100_000);
            int rounds = (int)TestUtils.GetBenchCount(100, 10);

            var sl = new SortedList <int, int>();

            //var cursor = sa.GetAsyncCursor();
            //cursor.MoveNextAsync();
            var di = new Dictionary <int, int>();

            //for (int r = 0; r < rounds; r++)
            //{
            //    using (Benchmark.Run("SL.Add", count))
            //    {
            //        for (int i = r * count; i < (r + 1) * count; i++)
            //        {
            //            if (i == r * count + 3)
            //            {
            //                continue;
            //            }

            //            sl.Add(i, i);
            //        }
            //    }
            //    Console.WriteLine($"Added {((r + 1) * count / 1000000):N}");
            //}

            //for (int r = 0; r < rounds; r++)
            //{
            //    using (Benchmark.Run("DI.Add", count))
            //    {
            //        for (int i = r * count; i < (r + 1) * count; i++)
            //        {
            //            if (i == r * count + 3)
            //            {
            //                continue;
            //            }

            //            di.Add(i, i);
            //        }
            //    }
            //    Console.WriteLine($"Added {((r + 1) * count / 1000000):N}");
            //}

            BufferPool <int> .MemoryPool.PrintStats();

            for (int _ = 0; _ < 2; _++)
            {
                var sa = new AppendSeries <int, int>();

                for (int r = 0; r < rounds; r++)
                {
                    using (Benchmark.Run("Append", count))
                    {
                        for (int i = r * count; i < (r + 1) * count; i++)
                        {
                            if (i == r * count + 3)
                            {
                                continue;
                            }

                            if (!sa.DangerousTryAppend(i, i))
                            {
                                Console.WriteLine("Cannot add " + i);
                                return;
                            }
                        }
                    }

                    Console.WriteLine($"Added {((r + 1) * count / 1_000_000):N}");
                }

                Benchmark.Dump();

                BufferPool <int> .MemoryPool.PrintStats();

                sa.Dispose();

                BufferPool <int> .MemoryPool.PrintStats();
            }
        }