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}"); }
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); } }
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); } }