Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions test/benchmarks/Benchmarks.Trace/Benchmarks.Trace.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.12.1" />
<PackageReference Include="JetBrains.Profiler.Api" Version="1.1.7" />
<PackageReference Include="System.Net.Http" Version="4.3.4" />
</ItemGroup>

Expand Down
67 changes: 65 additions & 2 deletions test/benchmarks/Benchmarks.Trace/Program.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Running;
using Datadog.Trace.BenchmarkDotNet;
Expand All @@ -8,9 +13,67 @@ internal class Program
{
private static void Main(string[] args)
{
var config = DefaultConfig.Instance.AddExporter(DatadogExporter.Default);
if (args?.Any(a => a == "-jetbrains") == true)
{
ExecuteWithJetbrainsTools(args);
}
else
{
var config = DefaultConfig.Instance.AddExporter(DatadogExporter.Default);
BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args, config);
}
}

private static void ExecuteWithJetbrainsTools(string[] args)
{
HashSet<string> hashSet = new HashSet<string>(args.Where(a => a != "-jetbrains").Select(a => a.ToLowerInvariant()));
var benchmarkTypes = typeof(Program).Assembly.GetTypes().Where(t => hashSet.Contains(t.Name.ToLowerInvariant()));
foreach (var benchmarkType in benchmarkTypes)
{
foreach (var method in benchmarkType.GetMethods())
{
if (method.GetCustomAttributes(false).Any(att => att is BenchmarkAttribute))
{
var benchmarkInstance = Activator.CreateInstance(benchmarkType);
var groupName = string.Format("{0}.{1}", benchmarkType.FullName, method.Name);
Console.WriteLine("Running: " + groupName);
for (var i = 0; i < 1000; i++)
{
method.Invoke(benchmarkInstance, null);
}
GC.Collect();
GC.WaitForPendingFinalizers();

if ((JetBrains.Profiler.Api.MeasureProfiler.GetFeatures() & JetBrains.Profiler.Api.MeasureFeatures.Ready) != 0)
{
Console.WriteLine(" Collecting Data...");
JetBrains.Profiler.Api.MeasureProfiler.StartCollectingData(groupName);
for (var i = 0; i < 100_000; i++)
{
method.Invoke(benchmarkInstance, null);
}
JetBrains.Profiler.Api.MeasureProfiler.StopCollectingData();
JetBrains.Profiler.Api.MeasureProfiler.SaveData(groupName);
GC.Collect();
GC.WaitForPendingFinalizers();
}

BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args, config);
if ((JetBrains.Profiler.Api.MemoryProfiler.GetFeatures() & JetBrains.Profiler.Api.MemoryFeatures.Ready) != 0)
{
Console.WriteLine(" Getting memory snapshot...");
JetBrains.Profiler.Api.MemoryProfiler.ForceGc();
JetBrains.Profiler.Api.MemoryProfiler.CollectAllocations(true);
for (var i = 0; i < 100_000; i++)
{
method.Invoke(benchmarkInstance, null);
}
JetBrains.Profiler.Api.MemoryProfiler.GetSnapshot(groupName);
JetBrains.Profiler.Api.MemoryProfiler.CollectAllocations(false);
}
}
}
Console.WriteLine("Done.");
}
}
}
}