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
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
27 changes: 24 additions & 3 deletions src/BenchmarkDotNet/ConsoleArguments/ConfigParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static class ConfigParser

[SuppressMessage("ReSharper", "StringLiteralTypo")]
[SuppressMessage("ReSharper", "CoVariantArrayConversion")]
private static readonly IReadOnlyDictionary<string, IExporter[]> AvailableExporters =
private static readonly IDictionary<string, IExporter[]> AvailableExporters =
new Dictionary<string, IExporter[]>(StringComparer.InvariantCultureIgnoreCase)
{
{ "csv", new[] { CsvExporter.Default } },
Expand All @@ -72,6 +72,21 @@ public static class ConfigParser
{ "fullxml", new[] { XmlExporter.Full } }
};


private static bool TryCreateCustomExporter(string customExporterName)
{
try
{
var customExporter = Activator.CreateInstance(Type.GetType(customExporterName));
AvailableExporters.Add(customExporterName, new IExporter[] { (IExporter)customExporter });
return true;
}
catch (Exception)
{
return false;
}
}

public static (bool isSuccess, IConfig config, CommandLineOptions options) Parse(string[] args, ILogger logger, IConfig? globalConfig = null)
{
(bool isSuccess, IConfig config, CommandLineOptions options) result = default;
Expand Down Expand Up @@ -253,11 +268,17 @@ private static bool Validate(CommandLineOptions options, ILogger logger)
}

foreach (string exporter in options.Exporters)
if (!AvailableExporters.ContainsKey(exporter))
{
if (AvailableExporters.ContainsKey(exporter) || TryCreateCustomExporter(exporter))
{
continue;
}
else
{
logger.WriteLineError($"The provided exporter \"{exporter}\" is invalid. Available options are: {string.Join(", ", AvailableExporters.Keys)}.");
logger.WriteLineError($"The provided exporter \"{exporter}\" is invalid. Available options are: {string.Join(", ", AvailableExporters.Keys)} or custom exporter by assembly-qualified name.");
return false;
}
}

if (options.CliPath.IsNotNullButDoesNotExist())
{
Expand Down
22 changes: 22 additions & 0 deletions tests/BenchmarkDotNet.Tests/ConfigParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,37 @@
using Perfolizer.Horology;
using Perfolizer.Mathematics.SignificanceTesting;
using Perfolizer.Mathematics.Thresholds;
using BenchmarkDotNet.Exporters.Json;
using BenchmarkDotNet.Exporters.Xml;

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

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

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

Assert.Equal(3, config.GetExporters().Count());
Assert.Contains(typeof(CustomExporterTestClass).Name, config.GetExporters().Select(e => e.Name));
Assert.Contains(HtmlExporter.Default, config.GetExporters());
Assert.Contains(XmlExporter.Default, config.GetExporters());

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
Expand Down
Loading