public void ArrayPoolUsesSameArray()
        {
            var config = new ArrayPoolConfig();
            var pool   = config.Create();

            var array1 = pool.GetArrayExact <object>(1);

            pool.ReleaseArray(array1);

            var array2 = pool.GetArrayExact <object>(1);

            Assert.Equal(array1, array2);
        }
        public void ArrayPoolCuncurrentPerformance()
        {
            var config = new ArrayPoolConfig();
            var pool   = config.Create();

            Enumerable.Range(0, 100)
            .AsParallel()
            .WithDegreeOfParallelism(20)
            .ForAll(x =>
            {
                var array = pool.GetArrayExact <object>(1);
                array[0]  = x;
                pool.ReleaseArray(array);
            });
        }
Exemplo n.º 3
0
        public void AllocationTests()
        {
            var input1 = new byte[5];
            var input2 = new byte[60];
            var input3 = new byte[100];
            var input4 = new byte[130];

            // 42 2 1
            var gg1 = GC.CollectionCount(0);
            var gg2 = GC.CollectionCount(1);
            var gg3 = GC.CollectionCount(2);

            using (var pool = new ArrayPoolConfig().Create())
            {
                for (var i = 0; i < 100_000_0; i++)
                {
                    using (var stream = new ValueStream(pool))
                    {
                        stream.Write(input1, 0, input1.Length);
                        stream.Write(input2, 0, input2.Length);
                        stream.Write(input3, 0, input3.Length);
                        stream.Write(input4, 0, input4.Length);
                    }
                }
            }

            var g1 = GC.CollectionCount(0);
            var g2 = GC.CollectionCount(1);
            var g3 = GC.CollectionCount(2);

            for (var i = 0; i < 100_000_0; i++)
            {
                using (var stream = new System.IO.MemoryStream())
                {
                    stream.Write(input1, 0, input1.Length);
                    stream.Write(input2, 0, input2.Length);
                    stream.Write(input3, 0, input3.Length);
                    stream.Write(input4, 0, input4.Length);
                }
            }

            // 565 2 1
            var g11 = GC.CollectionCount(0);
            var g22 = GC.CollectionCount(1);
            var g33 = GC.CollectionCount(2);
        }