Skip to content

Commit 5ac84ae

Browse files
EvangelinkCopilot
andauthored
Add TelemetryCollector assertion benchmarks (#11242)
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent e3293ab commit 5ac84ae

1 file changed

Lines changed: 134 additions & 0 deletions

File tree

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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 assertion telemetry collection, which runs once per Assert, CollectionAssert, and StringAssert call.
12+
/// </summary>
13+
[MemoryDiagnoser]
14+
public class TelemetryCollectorBenchmarks : IDisposable
15+
{
16+
private const string AssertionName = "Assert.AreEqual";
17+
private const int ConcurrentThreadCount = 4;
18+
private const int CallsPerThread = 1_000;
19+
private const int ConcurrentOperationsPerInvoke = ConcurrentThreadCount * CallsPerThread;
20+
private const string TelemetryOptOutEnvironmentVariable = "TESTINGPLATFORM_TELEMETRY_OPTOUT";
21+
22+
private readonly Barrier _startBarrier = new(ConcurrentThreadCount + 1);
23+
private readonly Barrier _completedBarrier = new(ConcurrentThreadCount + 1);
24+
private Thread[] _workerThreads = null!;
25+
private bool _isDisposed;
26+
private bool _restoreTelemetryOptOut;
27+
private string? _telemetryOptOut;
28+
private bool _workersStarted;
29+
private volatile bool _stopWorkers;
30+
31+
[GlobalSetup(Target = nameof(TrackAssertionCall_SingleThreaded))]
32+
public static void SetupSingleThreaded() => ResetAndPrimeCounter();
33+
34+
[GlobalSetup(Target = nameof(TrackAssertionCall_Concurrent))]
35+
public void SetupConcurrent()
36+
{
37+
ResetAndPrimeCounter();
38+
39+
_workerThreads = new Thread[ConcurrentThreadCount];
40+
for (int i = 0; i < _workerThreads.Length; i++)
41+
{
42+
_workerThreads[i] = new Thread(TrackAssertionCalls)
43+
{
44+
IsBackground = true,
45+
Name = "TelemetryCollectorBenchmarkWorker",
46+
};
47+
_workerThreads[i].Start();
48+
}
49+
50+
_workersStarted = true;
51+
}
52+
53+
[GlobalSetup(Target = nameof(TrackAssertionCall_Disabled))]
54+
public void SetupDisabled()
55+
{
56+
TelemetryCollector.DrainAssertionCallCounts();
57+
_telemetryOptOut = Environment.GetEnvironmentVariable(TelemetryOptOutEnvironmentVariable);
58+
_restoreTelemetryOptOut = true;
59+
Environment.SetEnvironmentVariable(TelemetryOptOutEnvironmentVariable, "1");
60+
}
61+
62+
[GlobalCleanup]
63+
public void Cleanup() => Dispose();
64+
65+
public void Dispose()
66+
{
67+
if (_isDisposed)
68+
{
69+
return;
70+
}
71+
72+
_isDisposed = true;
73+
if (_workersStarted)
74+
{
75+
_stopWorkers = true;
76+
_startBarrier.SignalAndWait();
77+
78+
foreach (Thread workerThread in _workerThreads)
79+
{
80+
workerThread.Join();
81+
}
82+
}
83+
84+
_startBarrier.Dispose();
85+
_completedBarrier.Dispose();
86+
TelemetryCollector.DrainAssertionCallCounts();
87+
if (_restoreTelemetryOptOut)
88+
{
89+
Environment.SetEnvironmentVariable(TelemetryOptOutEnvironmentVariable, _telemetryOptOut);
90+
}
91+
92+
GC.SuppressFinalize(this);
93+
}
94+
95+
[Benchmark(Baseline = true)]
96+
public void TrackAssertionCall_SingleThreaded()
97+
=> TelemetryCollector.TrackAssertionCall(AssertionName, isEnabled: true);
98+
99+
[Benchmark(OperationsPerInvoke = ConcurrentOperationsPerInvoke)]
100+
public void TrackAssertionCall_Concurrent()
101+
{
102+
_startBarrier.SignalAndWait();
103+
_completedBarrier.SignalAndWait();
104+
}
105+
106+
[Benchmark]
107+
public void TrackAssertionCall_Disabled()
108+
=> TelemetryCollector.TrackAssertionCall(AssertionName);
109+
110+
private void TrackAssertionCalls()
111+
{
112+
while (true)
113+
{
114+
_startBarrier.SignalAndWait();
115+
if (_stopWorkers)
116+
{
117+
return;
118+
}
119+
120+
for (int i = 0; i < CallsPerThread; i++)
121+
{
122+
TelemetryCollector.TrackAssertionCall(AssertionName, isEnabled: true);
123+
}
124+
125+
_completedBarrier.SignalAndWait();
126+
}
127+
}
128+
129+
private static void ResetAndPrimeCounter()
130+
{
131+
TelemetryCollector.DrainAssertionCallCounts();
132+
TelemetryCollector.TrackAssertionCall(AssertionName, isEnabled: true);
133+
}
134+
}

0 commit comments

Comments
 (0)