public void CouldAcquireAndReleaseBuffers() { var path = TestUtils.GetPath(); #pragma warning disable 219 var count = 1000; #pragma warning restore 219 var pool = new SharedMemoryPool(path, 126, (Wpid)123, rmMaxBufferLength: SharedMemoryPool.RmMaxPoolBufferLength); for (int i = 1100; i < 1000000; i++) { var mem = pool.RentMemory(i); mem.InvariantCheck(); //Assert.GreaterOrEqual(mem.Length, i); //Assert.IsTrue(mem.Length >= i); //Assert.IsTrue(mem.Length >= mem.Length); //Assert.AreEqual(pool.DataSizeToBucketSize(mem.Length), mem.Length); //Assert.IsTrue(pool.BucketSizeToDataSize(mem.Length) == mem.Length); //Assert.IsTrue(BitUtil.IsAligned((long)mem.Pointer, 2048)); if (i % 1000 == 0) { Console.WriteLine($"Requested data: {i}, pool: {mem.LengthPow2}, data: {mem.Length}, data/avail: {(i * 100.0 / mem.Length):0.00}%"); } pool.Return(mem); } //using (Benchmark.Run("SharedMemoryBuffer acquire/release", count)) //{ // for (int i = 0; i < count; i++) // { // var buffer = pool.Acquire(2000); // var h = buffer.Pin(); // h.Dispose(); // var buffer2 = pool.Acquire(2000); // buffer2.Pin(); // buffer2.Unpin(); // } //} // Benchmark.Dump(); // pool.Dispose(); }
public void CouldRentAndReturnBuffers2() { var path = TestUtils.GetPath(); var count = 10_000; var pool = new SharedMemoryPool(path, 126, (Wpid)123, rmMaxBufferLength: SharedMemoryPool.RmMaxPoolBufferLength); using (Benchmark.Run("SharedMemoryBuffer acquire/release", count)) { for (int i = 1; i < count; i++) { // Rent returns a buffer with zero refcount // Return expects zero refcount // With positive RefCount we do not return, // first we unpin according to app logic, e.g. archive a chunk // and only then return var buffer = (SharedMemory)pool.Rent(i); var buffer2 = (SharedMemory)pool.Rent(i); // var existing = pool.UnsafeGetBuffer(buffer.BufferRef); // switching to refcount mode, decrement to zero causes return to pool of finalize var h = buffer.Pin(); h.Dispose(); if (!buffer._isPooled) { Assert.Fail("!buffer._isPooled"); } if (!pool.Return(buffer2, true)) { Assert.Fail(); } } } Benchmark.Dump(); pool.Dispose(); }
public void CouldRentAndReturnBuffers() { var path = TestUtils.GetPath(); var pool = new SharedMemoryPool(path, 126, (Wpid)123, rmMaxBufferLength: SharedMemoryPool.RmMaxPoolBufferLength); for (int i = 1000; i < 1024 * 1024; i++) { var mem = pool.RentMemory(i); mem.InvariantCheck(); Assert.IsTrue(mem.Length >= i); if (i > SharedMemoryPool.Pow2ModeSizeLimit) { Assert.IsTrue(mem.LengthPow2 >= i); Assert.IsTrue(mem.Length - mem.LengthPow2 == 4088); // Assert.AreEqual(pool.DataSizeToBucketSize(mem.Length), mem.LengthPow2); } Assert.IsTrue(mem.Length > mem.LengthPow2); var lenX = (long)mem.PointerPow2 + mem.LengthPow2 - (long)mem.Pointer; Assert.AreEqual(mem.Length, lenX); if (i > 4096) { Assert.IsTrue(BitUtil.IsAligned((long)mem.PointerPow2, 4096)); Assert.IsTrue(BitUtil.IsPowerOfTwo(mem.LengthPow2)); } else { Assert.IsTrue(BitUtil.IsAligned((long)mem.PointerPow2, 2048)); } if (i % 1000 == 0) { Console.WriteLine($"Requested: {i}, RM pool: {mem.LengthPow2}, data: {mem.Length}, pow2/avail: {Math.Round(mem.LengthPow2 * 100.0 / mem.Length, 1)}%"); } pool.Return(mem, false); } }
public void PrintPow2Sizes() { var path = TestUtils.GetPath(); var count = 23; var pool = new SharedMemoryPool(path, 10 * 1024, (Wpid)1, rmMaxBufferLength: SharedMemoryPool.RmMaxPoolBufferLength); using (Benchmark.Run("Rent/Release Buffers", count)) { for (int i = 1; i <= count; i++) { var len = 1 << i; var buffer = pool.RentMemory(len); Console.WriteLine($"{i} - {len} - {buffer.Length} - { Math.Round(100.0 - len * 100.0 / buffer.Length, 2)} - BR: [{buffer.BufferRef}] RefCount: [{buffer.ReferenceCount}]"); pool.Return(buffer, false); } } pool.Dispose(); }