diff --git a/tracer/test/benchmarks/Benchmarks.Trace/Program.cs b/tracer/test/benchmarks/Benchmarks.Trace/Program.cs index b732073b9fed..abf1503a1924 100644 --- a/tracer/test/benchmarks/Benchmarks.Trace/Program.cs +++ b/tracer/test/benchmarks/Benchmarks.Trace/Program.cs @@ -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("RunStringAspectBenchmark"); +#endif + Console.WriteLine($"Execution context: "); Console.WriteLine("CurrentCulture is {0}.", CultureInfo.CurrentCulture.Name); @@ -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(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); } } }