Skip to content

Commit 4d3477c

Browse files
Evangelinkgithub-actions[bot]Copilot
authored
[perf-improver] Add benchmark coverage for CollectionAssert.AreEquivalent (#11331)
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent a61e785 commit 4d3477c

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using BenchmarkDotNet.Attributes;
5+
6+
using Microsoft.VisualStudio.TestTools.UnitTesting;
7+
8+
namespace MSTest.Performance.Benchmarks;
9+
10+
/// <summary>
11+
/// Measures <see cref="CollectionAssert.AreEquivalent{T}(System.Collections.Generic.IEnumerable{T}?, System.Collections.Generic.IEnumerable{T}?, System.Collections.Generic.IEqualityComparer{T}?, string?)"/>
12+
/// on its success path, which is the hot path exercised by every passing <c>CollectionAssert.AreEquivalent</c>
13+
/// call in a test suite. Internally this counts element occurrences via dictionaries built per invocation
14+
/// (see <c>CollectionAssert.Helpers.cs</c> <c>GetElementCounts</c>/<c>FindMismatchedElement</c>), so this
15+
/// benchmark tracks the cost of that allocation pattern as collection size grows.
16+
/// </summary>
17+
[MemoryDiagnoser]
18+
public class CollectionAssertEquivalenceBenchmarks
19+
{
20+
private int[] _smallExpected = null!;
21+
private int[] _smallActual = null!;
22+
private int[] _largeExpected = null!;
23+
private int[] _largeActual = null!;
24+
25+
[GlobalSetup]
26+
public void Setup()
27+
{
28+
_smallExpected = [1, 2, 3, 4, 5];
29+
_smallActual = [5, 4, 3, 2, 1];
30+
31+
_largeExpected = Enumerable.Range(0, 1000).ToArray();
32+
int[] largeActual = Enumerable.Range(0, 1000).ToArray();
33+
Random.Shared.Shuffle(largeActual);
34+
_largeActual = largeActual;
35+
}
36+
37+
[Benchmark(Baseline = true)]
38+
public void AreEquivalent_SmallCollection()
39+
=> CollectionAssert.AreEquivalent(_smallExpected, _smallActual, EqualityComparer<int>.Default, string.Empty);
40+
41+
[Benchmark]
42+
public void AreEquivalent_LargeCollection()
43+
=> CollectionAssert.AreEquivalent(_largeExpected, _largeActual, EqualityComparer<int>.Default, string.Empty);
44+
}

0 commit comments

Comments
 (0)