Skip to content

Make FromUrl and FromSource more friendly #1910

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

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
2 changes: 1 addition & 1 deletion src/BenchmarkDotNet/Running/BenchmarkConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static partial class BenchmarkConverter
public static BenchmarkRunInfo TypeToBenchmarks(Type type, IConfig config = null)
{
if (type.IsGenericTypeDefinition)
throw new ArgumentException($"{type.Name} is generic type definition, use BenchmarkSwitcher for it"); // for "open generic types" should be used BenchmarkSwitcher
throw new InvalidBenchmarkDeclarationException($"{type.Name} is generic type definition, use BenchmarkSwitcher for it"); // for "open generic types" should be used BenchmarkSwitcher

// We should check all methods including private to notify users about private methods with the [Benchmark] attribute
var bindingFlags = BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
Expand Down
13 changes: 11 additions & 2 deletions src/BenchmarkDotNet/Running/BenchmarkRunnerDirty.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
Expand Down Expand Up @@ -65,14 +66,22 @@ public static Summary[] Run(BenchmarkRunInfo[] benchmarkRunInfos)
return RunWithExceptionHandling(() => RunWithDirtyAssemblyResolveHelper(benchmarkRunInfos));
}

/// <summary>
/// Supported only on Full .NET Framework. Not recommended.
/// </summary>
[PublicAPI]
[EditorBrowsable(EditorBrowsableState.Never)]
public static Summary RunUrl(string url, IConfig config = null)
{
using (DirtyAssemblyResolveHelper.Create())
return RunWithExceptionHandling(() => RunUrlWithDirtyAssemblyResolveHelper(url, config));
}

/// <summary>
/// Supported only on Full .NET Framework. Not recommended.
/// </summary>
[PublicAPI]
[EditorBrowsable(EditorBrowsableState.Never)]
public static Summary RunSource(string source, IConfig config = null)
{
using (DirtyAssemblyResolveHelper.Create())
Expand Down Expand Up @@ -110,13 +119,13 @@ private static Summary[] RunWithDirtyAssemblyResolveHelper(BenchmarkRunInfo[] be
private static Summary RunUrlWithDirtyAssemblyResolveHelper(string url, IConfig config = null)
=> RuntimeInformation.IsFullFramework
? BenchmarkRunnerClean.Run(BenchmarkConverter.UrlToBenchmarks(url, config)).Single()
: throw new NotSupportedException("Supported only on Full .NET Framework");
: throw new InvalidBenchmarkDeclarationException("Supported only on Full .NET Framework");

[MethodImpl(MethodImplOptions.NoInlining)]
private static Summary RunSourceWithDirtyAssemblyResolveHelper(string source, IConfig config = null)
=> RuntimeInformation.IsFullFramework
? BenchmarkRunnerClean.Run(BenchmarkConverter.SourceToBenchmarks(source, config)).Single()
: throw new NotSupportedException("Supported only on Full .NET Framework");
: throw new InvalidBenchmarkDeclarationException("Supported only on Full .NET Framework");

private static Summary RunWithExceptionHandling(Func<Summary> run)
{
Expand Down
7 changes: 7 additions & 0 deletions src/BenchmarkDotNet/Running/ClassicBenchmarkConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Environments;
using BenchmarkDotNet.Loggers;
using BenchmarkDotNet.Portability;
using Microsoft.CSharp;

namespace BenchmarkDotNet.Running
Expand All @@ -15,6 +16,9 @@ public static partial class BenchmarkConverter
{
public static BenchmarkRunInfo[] UrlToBenchmarks(string url, IConfig config = null)
{
if (!RuntimeInformation.IsFullFramework)
throw new NotSupportedException("Supported only on Full .NET Framework.");

var logger = HostEnvironmentInfo.FallbackLogger;

url = GetRawUrl(url);
Expand Down Expand Up @@ -49,6 +53,9 @@ public static BenchmarkRunInfo[] UrlToBenchmarks(string url, IConfig config = nu

public static BenchmarkRunInfo[] SourceToBenchmarks(string source, IConfig config = null)
{
if (!RuntimeInformation.IsFullFramework)
throw new NotSupportedException("Supported only on Full .NET Framework.");

string benchmarkContent = source;
CompilerResults compilerResults;
using (var cSharpCodeProvider = new CSharpCodeProvider()) {
Expand Down