/// <summary> /// Constructs a sorted data structure of the given capacity to store top pip's performance information. /// </summary> public PerProcessPipPerformanceInformationStore(int maxNumberOfBatches, int ariaLimitBytes) { m_ariaCharLimit = ariaLimitBytes; m_maxNumberOfBatches = maxNumberOfBatches; int capacity = maxNumberOfBatches == 0 ? 0 : (m_ariaCharLimit / PerProcessPipMessageSizeBytes) * (maxNumberOfBatches + 1); // maxNumberOfBatches + 1 avoids reporting fewer pips due to the approximation done by PerPipMessageSizeBytes m_sortedTopPipPerformanceInfo = new ConcurrentBoundedSortedCollection <int, PerProcessPipPerformanceInformation>(capacity, Comparer <int> .Create((a, b) => a.CompareTo(b))); // To keep track of top N pips by execution times. ConcurrentBoundedSortedCollection only works with ascending order since it assumes the minimum is at index 0 }
public void BoundedExhaustiveTest(int[] arr, int capacity) { // expected: top 'capacity' elements in the sorted version of 'arr' var expectedResult = arr.OrderBy(i => i).Reverse().Take(capacity).Reverse().ToArray(); var sl = new ConcurrentBoundedSortedCollection<int, int>(capacity); Parallel.ForEach(arr, e => sl.TryAdd(e, e)); var slArray = sl.Select(kvp => kvp.Key).ToArray(); XAssert.ArrayEqual(expectedResult, slArray); }
public void AddWithSortValueBelowDefault() { ConcurrentBoundedSortedCollection<int, int> cbsl = new ConcurrentBoundedSortedCollection<int, int>(10); cbsl.TryAdd(-30, 0); Assert.Equal(1, cbsl.Count()); int value = cbsl.First().Value; Assert.Equal(0, value); }
public void AddBeyondCapacity() { ConcurrentBoundedSortedCollection<int, string> cbsl = new ConcurrentBoundedSortedCollection<int, string>(3); cbsl.TryAdd(1, "1"); cbsl.TryAdd(5, "5"); cbsl.TryAdd(3, "3"); cbsl.TryAdd(2, "2"); // Should remove "1" cbsl.TryAdd(4, "4"); // Should remove "2" Assert.Equal(cbsl.Capacity, cbsl.Count()); Assert.Equal("3", cbsl.ElementAt(0).Value); Assert.Equal("4", cbsl.ElementAt(1).Value); Assert.Equal("5", cbsl.ElementAt(2).Value); }
public void AddToCapacity() { ConcurrentBoundedSortedCollection<long, int> cbsl = new ConcurrentBoundedSortedCollection<long, int>(4); cbsl.TryAdd(1, 1); cbsl.TryAdd(3, 3); cbsl.TryAdd(-1, -1); cbsl.TryAdd(0, 0); Assert.Equal(4, cbsl.Count()); Assert.Equal(-1, cbsl.ElementAt(0).Value); Assert.Equal(0, cbsl.ElementAt(1).Value); Assert.Equal(1, cbsl.ElementAt(2).Value); Assert.Equal(3, cbsl.ElementAt(3).Value); }
/// <summary> /// Renders all alements found in <paramref name="sortedList"/>. /// Elements are sorted by weight (in descending order). /// Each element is rendered on a separate line using the following format string: $"[{weight}] {path}. /// The 'weigth' fragment is padded so that all 'path's are aligned on the left. /// </summary> protected static string CollapseDictionaryTimeLogs(ConcurrentBoundedSortedCollection <long, string> sortedList, Func <long, string> weightRenderer) { var maxRenderedWeightLength = sortedList.Any() ? sortedList.Max(kvp => weightRenderer(kvp.Key).Length) : 0; StringBuilder stringBuilder = new StringBuilder(); foreach (KeyValuePair <long, string> pair in sortedList.Reverse()) { var renderedWeight = weightRenderer(pair.Key); var paddedDurationStr = renderedWeight.PadLeft(maxRenderedWeightLength); stringBuilder.Append(I($"{Environment.NewLine} [{paddedDurationStr}] {pair.Value}")); } return(stringBuilder.ToString()); }
public void AddParallelManyElements(int capacity) { Random r = new Random(); ConcurrentBoundedSortedCollection<int, int> cbsl = new ConcurrentBoundedSortedCollection<int, int>(capacity); // Build array of random ints int arraySize = 400; int[] valArray = new int[arraySize]; for (int i = 0; i < arraySize; i++) { valArray[i] = r.Next(); } Parallel.ForEach(valArray, i => cbsl.TryAdd(i, i)); XAssert.ArrayEqual(valArray.OrderByDescending(i => i).Take(capacity).Reverse().ToArray(), cbsl.Select(kvp => kvp.Key).ToArray()); }
public void BoundedExhaustiveTest(int n) { var arr = Enumerable.Range(0, n).ToArray(); var perms = GenAllPermutations(arr).ToArray(); Assert.All( Enumerable.Range(1, n + 1), // test for all possible capacities from 1 to n+1 capacity => { var top = arr.OrderBy(i => i).Reverse().Take(capacity).Reverse().ToArray(); Assert.All( perms, // test for all possible permutations perm => { var sl = new ConcurrentBoundedSortedCollection <int, int>(capacity); Parallel.ForEach(perm, e => sl.TryAdd(e, e)); var slArray = sl.Select(kvp => kvp.Key).ToArray(); XAssert.ArrayEqual(top, slArray); }); }); }