Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[perf] Update Stress Test to avoid false sharing #5985

Merged
merged 3 commits into from
Nov 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 29 additions & 6 deletions test/OpenTelemetry.Tests.Stress/StressTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void RunSynchronously()
.AddPrometheusHttpListener(o => o.UriPrefixes = new string[] { $"http://localhost:{options.PrometheusInternalMetricsPort}/" })
.Build() : null;

var statistics = new long[options.Concurrency];
var statistics = new MeasurementData[options.Concurrency];
var watchForTotal = Stopwatch.StartNew();

TimeSpan? duration = options.DurationSeconds > 0
Expand Down Expand Up @@ -133,14 +133,14 @@ public void RunSynchronously()
Console.SetCursorPosition(tempCursorLeft, tempCursorTop);
}

var cntLoopsOld = (ulong)statistics.Sum();
var cntLoopsOld = (ulong)statistics.Select(data => data.Count).Sum();
var cntCpuCyclesOld = StressTestNativeMethods.GetCpuCycles();

watch.Restart();
Thread.Sleep(200);
watch.Stop();

cntLoopsTotal = (ulong)statistics.Sum();
cntLoopsTotal = (ulong)statistics.Select(data => data.Count).Sum();
var cntCpuCyclesNew = StressTestNativeMethods.GetCpuCycles();

var nLoops = cntLoopsTotal - cntLoopsOld;
Expand Down Expand Up @@ -172,18 +172,18 @@ public void RunSynchronously()
{
Parallel.For(0, options.Concurrency, (i) =>
{
ref var count = ref statistics[i];
ref var item = ref statistics[i];

while (this.bContinue)
{
this.RunWorkItemInParallel();
count++;
item.Count++;
}
});
});

watchForTotal.Stop();
cntLoopsTotal = (ulong)statistics.Sum();
cntLoopsTotal = (ulong)statistics.Select(data => data.Count).Sum();
var totalLoopsPerSecond = (double)cntLoopsTotal / ((double)watchForTotal.ElapsedMilliseconds / 1000.0);
var cntCpuCyclesTotal = StressTestNativeMethods.GetCpuCycles();
var cpuCyclesPerLoopTotal = cntLoopsTotal == 0 ? 0 : cntCpuCyclesTotal / cntLoopsTotal;
Expand All @@ -206,4 +206,27 @@ protected virtual void WriteRunInformationToConsole()
protected virtual void Dispose(bool isDisposing)
{
}

// Padding to avoid false sharing.
// For most systems, the cache line size should be less than or equal to 128 bytes.
private struct MeasurementData
{
public long Count;

public long Padding1;
public long Padding2;
public long Padding3;
public long Padding4;
public long Padding5;
public long Padding6;
public long Padding7;
public long Padding8;
public long Padding9;
public long Padding10;
public long Padding11;
public long Padding12;
public long Padding13;
public long Padding14;
public long Padding15;
}
}
Loading