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); } }
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); } } }
private static void StressTest() { s_segmentSize = 1_000; s_iterations = 1_000_000; Print($"Speed test: 6 threads rent, copy, copy, check and return {s_segmentSize} segment size {s_iterations} times"); Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); s_arraySegmentPool = new ArraySegmentPool <byte>(s_segmentSize, 1, 1_000); System.Threading.Thread thread1 = new System.Threading.Thread(StressTestWorker) { IsBackground = true }; System.Threading.Thread thread2 = new System.Threading.Thread(StressTestWorker) { IsBackground = true }; System.Threading.Thread thread3 = new System.Threading.Thread(StressTestWorker) { IsBackground = true }; System.Threading.Thread thread4 = new System.Threading.Thread(StressTestWorker) { IsBackground = true }; System.Threading.Thread thread5 = new System.Threading.Thread(StressTestWorker) { IsBackground = true }; System.Threading.Thread thread6 = new System.Threading.Thread(StressTestWorker) { IsBackground = true }; thread1.Start(1); thread2.Start(2); thread3.Start(3); thread4.Start(4); thread5.Start(5); thread6.Start(6); thread1.Join(); thread2.Join(); thread3.Join(); thread4.Join(); thread5.Join(); thread6.Join(); Print($"Elapsed:{stopwatch.ElapsedMilliseconds}ms. Capacity:{s_arraySegmentPool.Capacity} Count:{s_arraySegmentPool.Count} Fails:{s_arraySegmentPool.FailsCount}", ConsoleColor.Yellow); if (s_arraySegmentPool.Capacity <= 18 & s_arraySegmentPool.Count == 0 & s_arraySegmentPool.FailsCount < s_iterations * 3) { Print("TEST PASSED", ConsoleColor.Green); } else { Print("TEST FAILED", ConsoleColor.Red); } }