Example #1
0
        public void CouldWriteInParallelAllValuesNonZero()
        {
#pragma warning disable 618
            Settings.DoAdditionalCorrectnessChecks = false;
#pragma warning restore 618
            var path          = TestUtils.GetPath();
            var repoName      = "CouldWriteAndReadLog0";
            var processConfig = new ProcessConfig(path);

            StartupConfig.StreamLogBufferPoolFlags = LMDBEnvironmentFlags.NoSync;
            StartupConfig.StreamBlockIndexFlags    = LMDBEnvironmentFlags.NoSync;

            var slm = new StreamLogManager(processConfig, repoName, null, 10 * 1024, true, true);

            var log0 = new NotificationLog(slm);

            // will disable packing
            log0.ActiveBuffer.Increment();

            var count = 2 * 1024 * 1024;

            List <Task> tasks     = new List <Task>();
            var         taskCount = 4;

            for (int j = 0; j < taskCount; j++)
            {
                tasks.Add(Task.Run(() =>
                {
                    try
                    {
                        using (Benchmark.Run("Log0.Append", count * taskCount))
                        {
                            for (long i = 1; i <= count; i++)
                            {
                                log0.Append((StreamLogNotification)(ulong)i);
                                // Thread.Yield();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("EX: " + ex);
                    }
                }));
            }

            Task.WhenAll(tasks).Wait();

            Benchmark.Dump();

            using (Benchmark.Run("Log0 MoveNext", count * taskCount))
            {
                var chunkCursor = log0.BlockIndex.GetBlockRecordCursor(StreamLogId.Log0Id);
                var readCount   = 0;
                while (chunkCursor.MoveNext())
                {
                    var record = chunkCursor.Current.Value;
                    if ((long)record.Version >= count * taskCount)
                    {
                        break;
                    }

                    if (record.IsPacked)
                    {
                        break;
                    }

                    var chunk = log0.RentChunkFromRecord(record);

                    for (ulong i = 0; i < 1024 * 1024; i++)
                    {
                        var position = NotificationLog.Log0ItemPosition(chunk.Pointer, (long)(chunk.FirstVersion + i), out _);
                        var value    = *(ulong *)position;
                        if (value == 0)
                        {
                            Assert.Fail("Zero value at: " + (chunk.FirstVersion + i));
                        }

                        readCount++;
                    }

                    chunk.DisposeFree();
                }

                //var c = log0.GetContainerCursor(false);

                //while (c.MoveNext())
                //{
                //    if (c.CurrentValue.ReadUInt64(0) == 0)
                //    {
                //        // Console.WriteLine($"c.CurrentValue == 0 at {c.CurrentKey}");
                //        Assert.Fail($"c.CurrentValue == 0 at {c.CurrentKey}");
                //    }

                //    readCount++;
                //}

                //c.Dispose();
                // Assert.AreEqual(count * taskCount, readCount);
                Console.WriteLine("READ COUNT M: " + readCount * 1.0 / (1024 * 1024));
            }

            // readerTask.Wait();

            slm.BufferPool.PrintBuffersAfterPoolDispose = true;
            log0.Dispose();
            slm.Dispose();
        }