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

Adds a custom exporter option on command line args #2461

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions src/BenchmarkDotNet/ConsoleArguments/CommandLineOptions.cs
Original file line number Diff line number Diff line change
@@ -31,6 +31,9 @@ public class CommandLineOptions
[Option('e', "exporters", Required = false, HelpText = "GitHub/StackOverflow/RPlot/CSV/JSON/HTML/XML")]
public IEnumerable<string> Exporters { get; set; }

[Option( "customExporter", Required = false, HelpText = "The assembly-qualified name of the Custom Exporter type")]
public string CustomExporter { get; set; }

[Option('m', "memory", Required = false, Default = false, HelpText = "Prints memory statistics")]
public bool UseMemoryDiagnoser { get; set; }

18 changes: 18 additions & 0 deletions src/BenchmarkDotNet/ConsoleArguments/ConfigParser.cs
Original file line number Diff line number Diff line change
@@ -49,6 +49,9 @@ public static class ConfigParser
{ "verylong", Job.VeryLongRun }
};

public static void RegisterCustomExporter(string commandLineName, IExporter exporter) => CustomExporters.Add(commandLineName, exporter);
private static readonly IDictionary<string, IExporter> CustomExporters = new Dictionary<string, IExporter>();

[SuppressMessage("ReSharper", "StringLiteralTypo")]
[SuppressMessage("ReSharper", "CoVariantArrayConversion")]
private static readonly IReadOnlyDictionary<string, IExporter[]> AvailableExporters =
@@ -252,6 +255,20 @@ private static bool Validate(CommandLineOptions options, ILogger logger)
}
}

if (!string.IsNullOrWhiteSpace(options.CustomExporter))
{
try
{
var customExporter = Activator.CreateInstance(Type.GetType(options.CustomExporter));
CustomExporters["customExporter"] = (IExporter)customExporter;
}
catch (Exception ex)
{
logger.WriteLineError(ex.ToString());
}

}

foreach (string exporter in options.Exporters)
if (!AvailableExporters.ContainsKey(exporter))
{
@@ -339,6 +356,7 @@ private static IConfig CreateConfig(CommandLineOptions options, IConfig globalCo
config.AddJob(baseJob);

config.AddExporter(options.Exporters.SelectMany(exporter => AvailableExporters[exporter]).ToArray());
config.AddExporter(CustomExporters.Select(c => c.Value).ToArray());

config.AddHardwareCounters(options.HardwareCounters
.Select(counterName => (HardwareCounter)Enum.Parse(typeof(HardwareCounter), counterName, ignoreCase: true))
16 changes: 16 additions & 0 deletions tests/BenchmarkDotNet.Tests/ConfigParserTests.cs
Original file line number Diff line number Diff line change
@@ -25,15 +25,31 @@
using Perfolizer.Horology;
using Perfolizer.Mathematics.SignificanceTesting;
using Perfolizer.Mathematics.Thresholds;
using BenchmarkDotNet.Exporters.Json;

namespace BenchmarkDotNet.Tests
{
public class CustomExporterTestClass : JsonExporterBase { }
public class ConfigParserTests
{
public ITestOutputHelper Output { get; }

public ConfigParserTests(ITestOutputHelper output) => Output = output;

[Theory]
[InlineData("--customExporter", "BenchmarkDotNet.Tests.CustomExporterTestClass, BenchmarkDotNet.Tests")]
public void CustomExporterConfigParsedCorrectly(params string[] args)
{
var config = ConfigParser.Parse(args, new OutputLogger(Output)).config;

var customExporter = config.GetExporters().ToList();
Assert.Equal("CustomExporterTestClass", customExporter[0].Name);
Assert.Empty(config.GetColumnProviders());
Assert.Empty(config.GetDiagnosers());
Assert.Empty(config.GetAnalysers());
Assert.Empty(config.GetLoggers());
}

[Theory]
[InlineData("--job=dry", "--exporters", "html", "rplot")]
[InlineData("--JOB=dry", "--EXPORTERS", "html", "rplot")] // case insensitive