Skip to content

Commit

Permalink
[TESTS]Added BenchmarkDotNet debug helper (#4717)
Browse files Browse the repository at this point in the history
* Added BenchmarkDotNet debug helper
  • Loading branch information
daniel-romano-DD authored Nov 3, 2023
1 parent 417ca16 commit f1cf405
Showing 1 changed file with 54 additions and 1 deletion.
55 changes: 54 additions & 1 deletion tracer/test/benchmarks/Benchmarks.Trace/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,23 @@
using BenchmarkDotNet.Exporters.Json;
using BenchmarkDotNet.Filters;
using Benchmarks.Trace.Jetbrains;
using System.Reflection.Metadata;
using BenchmarkDotNet.Attributes;
using System.Reflection;
using Datadog.Trace.Vendors.Newtonsoft.Json.Utilities;
using System.Collections;

namespace Benchmarks.Trace
{
internal class Program
{
private static void Main(string[] args)
private static int Main(string[] args)
{
#if DEBUG
// Debug benchmark classes here
// Example: return Debug<StringAspectsBenchmark>("RunStringAspectBenchmark");
#endif

Console.WriteLine($"Execution context: ");
Console.WriteLine("CurrentCulture is {0}.", CultureInfo.CurrentCulture.Name);

Expand Down Expand Up @@ -67,6 +77,49 @@ private static void Main(string[] args)

Console.WriteLine("Running tests...");
BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args, config);

return Environment.ExitCode;
}

private static int Debug<T>(string methodName, params object[] arguments)
where T : class, new()
{
// Retrieve the Benchmark method
var benchmarkMethod = typeof(T).GetMethod(methodName);
var initMethod = typeof(T).GetMethods().FirstOrDefault(m => Attribute.GetCustomAttribute(m, typeof(IterationSetupAttribute)) != null);
var cleanupMethod = typeof(T).GetMethods().FirstOrDefault(m => Attribute.GetCustomAttribute(m, typeof(IterationCleanupAttribute)) != null);

//Retrieve Arguments
MethodInfo argMethod = null;
var argAttribute = Attribute.GetCustomAttribute(benchmarkMethod, typeof(ArgumentsSourceAttribute));
if (argAttribute != null)
{
var argMethodName = argAttribute.GetType().GetProperty("Name").GetValue(argAttribute) as string;
argMethod = typeof(T).GetMethod(argMethodName);
}

T instance = new T();
if (arguments.Length > 0 || argMethod == null)
{
Debug(instance, benchmarkMethod, arguments, initMethod, cleanupMethod);
}
else
{
var argEnumerable = argMethod?.Invoke(instance, null) as IEnumerable;
foreach (var arg in argEnumerable)
{
Debug(instance, benchmarkMethod, new object[] { arg }, initMethod, cleanupMethod);
}
}

return 0;

}
private static void Debug(object instance, MethodInfo method, object[] args, MethodInfo initMethod, MethodInfo cleanupMethod)
{
initMethod?.Invoke(instance, null);
method.Invoke(instance, args.Length > 0 ? args : null);
cleanupMethod?.Invoke(instance, null);
}
}
}

0 comments on commit f1cf405

Please sign in to comment.