Ejemplo n.º 1
0
        private static void SliceTest()
        {
            s_segmentSize = 100;
            s_iterations  = 0;
            Print($"Slice test: 1 thread rent size:{s_segmentSize}, check, slice, return, check");
            Stopwatch Stopwatch = new Stopwatch();

            Stopwatch.Start();
            s_arraySegmentPool = new ArraySegmentPool <byte>(s_segmentSize, 10);
            bool IsError = false;
            ArraySegment <byte> ArraySegment;

            try
            {
                //case 1
                ArraySegment = s_arraySegmentPool.DangerousRent().Slice(0, s_segmentSize);
                Print($"Segment taken from region:{Array.FindIndex(s_arraySegmentPool.UnderlyingLayoutArray, PredicateFindOne)} ArraySegment.Slice(0, _segment_size)");
                if (ReturnAndCheck(ref ArraySegment) == false)
                {
                    IsError = true;
                    return;
                }
                //case 2
                ArraySegment = s_arraySegmentPool.DangerousRent().Slice(s_segmentSize / 2, s_segmentSize / 2);
                Print($@"Segment taken from region:{Array.FindIndex(s_arraySegmentPool.UnderlyingLayoutArray, PredicateFindOne)} ArraySegment.Slice(_segment_size / 2, _segment_size / 2)");
                if (ReturnAndCheck(ref ArraySegment) == false)
                {
                    IsError = true;
                    return;
                }
                //case 3
                ArraySegment = s_arraySegmentPool.DangerousRent().Slice(s_segmentSize, 0);
                Print($"Segment taken from region:{Array.FindIndex(s_arraySegmentPool.UnderlyingLayoutArray, PredicateFindOne)} ArraySegment.Slice(_segment_size, 0)");
                if (ReturnAndCheck(ref ArraySegment) == false)
                {
                    IsError = false;
                }
                else
                {
                    IsError = true; //Slice ArraySegment to zero not permitted!
                    return;
                }
            }
            finally
            {
                Print($"Elapsed:{Stopwatch.ElapsedMilliseconds}ms. Capacity:{s_arraySegmentPool.Capacity} Count:{s_arraySegmentPool.Count} Fails:{s_arraySegmentPool.FailsCount}", ConsoleColor.Yellow);
                if (IsError == false & s_arraySegmentPool.Capacity == 10 & s_arraySegmentPool.Count == 1 & s_arraySegmentPool.FailsCount == 0)
                {
                    Print("TEST PASSED", ConsoleColor.Green);
                }
                else
                {
                    Print("TEST FAILED", ConsoleColor.Red);
                }
            }
        }
Ejemplo n.º 2
0
        private static void SpeedTest()
        {
            s_segmentSize = 1_000;
            s_iterations  = 50_000_000;
            Print($"Speed test: 1 thread rent and return {s_segmentSize} segment size {s_iterations} times");
            s_arraySegmentPool = new ArraySegmentPool <byte>(s_segmentSize, 1, 1);
            Stopwatch Stopwatch = new Stopwatch();

            Stopwatch.Start();
            for (int i = 1; i <= s_iterations; i++)
            {
                ArraySegment <byte> arraySegment = s_arraySegmentPool.DangerousRent();
                arraySegment[0] = 1;
                s_arraySegmentPool.Return(ref arraySegment);
            }
            Print($"Elapsed:{Stopwatch.ElapsedMilliseconds}ms. Capacity:{s_arraySegmentPool.Capacity} Count:{s_arraySegmentPool.Count} Fails:{s_arraySegmentPool.FailsCount}", ConsoleColor.Yellow);
            if (s_arraySegmentPool.Capacity == 1 & s_arraySegmentPool.Count == 0 & s_arraySegmentPool.FailsCount == 0)
            {
                Print("TEST PASSED", ConsoleColor.Green);
            }
            else
            {
                Print("TEST FAILED", ConsoleColor.Red);
            }
        }
Ejemplo n.º 3
0
        private static void TrimTest()
        {
            s_segmentSize = 1_000;
            s_iterations  = 2_147_483;
            Print($"Trim test: 1 thread rent size:{s_segmentSize} x{s_iterations} times, return all, trim");
            Stopwatch Stopwatch = new Stopwatch();

            Stopwatch.Start();
            List <ArraySegment <byte> > List = new List <ArraySegment <byte> >(s_iterations);

            s_arraySegmentPool = new ArraySegmentPool <byte>(s_segmentSize, 1, s_iterations);
            for (int i = 1; i <= s_iterations; i++)
            {
                List.Add(s_arraySegmentPool.DangerousRent());
            }
            Print($"Rent finished. Elapsed:{Stopwatch.ElapsedMilliseconds}ms. Capacity:{s_arraySegmentPool.Capacity} Count:{s_arraySegmentPool.Count} Fails:{s_arraySegmentPool.FailsCount}");
            for (int i = 0; i < List.Count; i++)
            {
                ArraySegment <byte> ArraySegment = List[i];
                s_arraySegmentPool.Return(ref ArraySegment);
            }
            List.Clear();
            Print($"Return finished. Elapsed:{Stopwatch.ElapsedMilliseconds}ms. Capacity:{s_arraySegmentPool.Capacity} Count:{s_arraySegmentPool.Count} Fails:{s_arraySegmentPool.FailsCount}");
            s_arraySegmentPool.TrimExcess();
            Print($"Trim finished. Elapsed:{Stopwatch.ElapsedMilliseconds}ms. Capacity:{s_arraySegmentPool.Capacity} Count:{s_arraySegmentPool.Count} Fails:{s_arraySegmentPool.FailsCount}");
            Print($"Elapsed:{Stopwatch.ElapsedMilliseconds}ms. Capacity:{s_arraySegmentPool.Capacity} Count:{s_arraySegmentPool.Count} Fails:{s_arraySegmentPool.FailsCount}", ConsoleColor.Yellow);
            if (s_arraySegmentPool.Capacity == 1 & s_arraySegmentPool.Count == 0 & s_arraySegmentPool.FailsCount == 0)
            {
                Print("TEST PASSED", ConsoleColor.Green);
            }
            else
            {
                Print("TEST FAILED", ConsoleColor.Red);
            }
        }
Ejemplo n.º 4
0
        private static void StressTestWorker(object P_number)
        {
            byte      threadNumber = Convert.ToByte(P_number);
            Stopwatch stopwatch    = new Stopwatch();

            byte[] buffer = new byte[s_segmentSize];
            Array.Fill(buffer, threadNumber);
            stopwatch.Start();
            for (int iteration = 0; iteration < s_iterations; iteration++)
            {
                ArraySegment <byte> ArraySegment = s_arraySegmentPool.DangerousRent();
                Array.Copy(buffer, 0, ArraySegment.Array, ArraySegment.Offset, ArraySegment.Count);
                for (int i = 0; i < ArraySegment.Count; i++)
                {
                    if (ArraySegment[i] != threadNumber)
                    {
                        throw new Exception("Test failed");
                    }
                }
                Array.Clear(buffer, 0, buffer.Length);
                ArraySegment.CopyTo(buffer);
                for (int i = 0; i < buffer.Length; i++)
                {
                    if (buffer[i] != threadNumber | ArraySegment[i] != threadNumber)
                    {
                        throw new Exception("Test failed");
                    }
                }
                s_arraySegmentPool.Return(ref ArraySegment);
            }
            Print($"Thread #{threadNumber} finished. ElapsedMilliseconds:{stopwatch.ElapsedMilliseconds}");
        }