Skip to content

Commit

Permalink
Make FromUrl and FromSource more friendly (#1910)
Browse files Browse the repository at this point in the history
* BenchmarkRunner.FromUrl/FromSource more friendly

* Hide them because they don't work

* Change ArgumentException with InvalidBenchmarkDeclarationException
  • Loading branch information
YegorStepanov authored Aug 24, 2022
1 parent c99ba30 commit 6bb61ce
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/BenchmarkDotNet/Running/BenchmarkConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,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 benchmarkMethods = GetOrderedBenchmarkMethods(type.GetMethods(AllMethodsFlags));
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 @@ -11,6 +11,7 @@
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Environments;
using BenchmarkDotNet.Loggers;
using BenchmarkDotNet.Portability;
using Microsoft.CSharp;

namespace BenchmarkDotNet.Running
Expand All @@ -23,6 +24,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 @@ -63,6 +67,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

0 comments on commit 6bb61ce

Please # to comment.