Exemplo n.º 1
0
        public void RentReturnPinnedSlicesRetainablePool()
        {
            var maxBuffers = 128 / 64; // 2

            var pool = new RetainableMemoryPool <byte>(null, 32 * 1024,
                                                       1024 * 1024, maxBuffers, 0);

            var list = new List <RetainableMemory <byte> >();

            using (Benchmark.Run("FullCycle"))
            {
                for (int i = 0; i < maxBuffers * 2; i++)
                {
                    list.Add(pool.RentMemory(64 * 1024));
                }

                for (int i = 0; i < maxBuffers; i++)
                {
                    ((IDisposable)list[i]).Dispose();
                }

                for (int i = 2; i < maxBuffers * 2; i++)
                {
                    pool.Return(list[i]);
                }
            }

            pool.Dispose();
        }
Exemplo n.º 2
0
        public void RentReturnBenchmarkRetainablePoolOverCapacity()
        {
            var count    = 1_000_000;
            var capacity = 25;
            var batch    = capacity * 2;
            var pool     = new RetainableMemoryPool <byte>((p, l) =>
            {
                var am = ArrayMemory <byte> .Create(l, pin: true);
                // Attach pool
                am._pool = p;
                return(am);
            }, 16,
                                                           1024 * 1024, capacity, 0);

            var list = new List <RetainableMemory <byte> >(batch);

            using (Benchmark.Run("FullCycle", count * batch))
            {
                for (int i = 0; i < count; i++)
                {
                    for (int j = 0; j < batch; j++)
                    {
                        list.Add(pool.RentMemory(32 * 1024));
                    }

                    foreach (var arrayMemory in list)
                    {
                        pool.Return(arrayMemory);
                    }
                    list.Clear();
                }
            }

            pool.Dispose();
        }
Exemplo n.º 3
0
        public void ConcurrentRentReturn()
        {
            var count = 100_000_000;

            var pool = new RetainableMemoryPool <byte>(null, 16,
                                                       1024 * 1024, 50, 2);

            var tasks     = new List <Task>();
            var taskCount = 1; // 6x2 cores


            var mre = new ManualResetEventSlim(false);

            Action action = () =>
            {
                mre.Wait();
                for (int i = 0; i < count; i++)
                {
                    var memory = pool.RentMemory(32 * 1024);
                    //(memory.Pin(0)).Dispose();
                    //if (memory.IsDisposed || memory.IsRetained)
                    //{
                    //    Assert.Fail();
                    //}
                    pool.Return(memory);
                    //if (i % 1000000 == 0)
                    //{
                    //    Console.WriteLine(i);
                    //}
                }
            };

            for (int i = 0; i < taskCount; i++)
            {
                tasks.Add(Task.Run(action));
            }

            //Console.WriteLine("Set affinity and press enter");
            //Console.ReadLine();

            using (Benchmark.Run("FullCycle", count * taskCount))
            {
                mre.Set();

                Task.WhenAll(tasks).Wait();
            }

            pool.Dispose();
        }
Exemplo n.º 4
0
        public void CouldDisposePoolWithFreeSpace()
        {
            var maxBuffers = 10;

            var pool = new RetainableMemoryPool <byte>(null, 32 * 1024,
                                                       1024 * 1024, maxBuffers, 0);

            using (Benchmark.Run("FullCycle"))
            {
                for (int i = 0; i < maxBuffers / 2; i++)
                {
                    var memory = pool.RentMemory(64 * 1024);
                    pool.Return(memory);
                }
            }

            pool.Dispose();
        }
Exemplo n.º 5
0
        public void RentReturnDefaultRetainablePoolBench()
        {
#if !DEBUG
            var count = 10_000_000;
#else
            var count = 1_000;
#endif

            var pool = new RetainableMemoryPool <byte>(null, 16,
                                                       1024 * 1024, 50, 2);

            for (int i = 0; i < 1000; i++)
            {
                var memory = pool.RentMemory(32 * 1024);
                memory.Increment();
                memory.Decrement();
                // ((IDisposable)memory).Dispose();
                //(memory.Pin(0)).Dispose();
                //if (memory.IsDisposed || memory.IsRetained)
                //{
                //    Assert.Fail();
                //}
                // pool.Return(memory);
            }

            for (int _ = 0; _ < 20; _++)
            {
                using (Benchmark.Run("RentReturn", count))
                {
                    for (int i = 0; i < count; i++)
                    {
                        var memory = pool.RentMemory(32 * 1024);
                        //(memory.Pin(0)).Dispose();
                        //if (memory.IsDisposed || memory.IsRetained)
                        //{
                        //    Assert.Fail();
                        //}
                        pool.Return(memory, false);
                    }
                }
            }
            Benchmark.Dump();
            pool.Dispose();
        }
Exemplo n.º 6
0
        public void RentReturnBenchmarkRetainablePool()
        {
            var count = 100_000_000;

            var pool = new RetainableMemoryPool <byte>(null, 16,
                                                       1024 * 1024, 50, 2);

            for (int i = 0; i < 1000; i++)
            {
                var memory = pool.RentMemory(32 * 1024);
                memory.Increment();
                memory.Decrement();
                // ((IDisposable)memory).Dispose();
                //(memory.Pin(0)).Dispose();
                //if (memory.IsDisposed || memory.IsRetained)
                //{
                //    Assert.Fail();
                //}
                // pool.Return(memory);
            }

            using (Benchmark.Run("FullCycle", count))
            {
                for (int i = 0; i < count; i++)
                {
                    var memory = pool.RentMemory(32 * 1024);
                    //(memory.Pin(0)).Dispose();
                    //if (memory.IsDisposed || memory.IsRetained)
                    //{
                    //    Assert.Fail();
                    //}
                    pool.Return(memory);
                }
            }

            pool.Dispose();
        }