-
-
Notifications
You must be signed in to change notification settings - Fork 957
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b3b2d91
commit 91f3f7e
Showing
24 changed files
with
1,036 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
--- | ||
uid: docs.baselines | ||
name: Running with VSTest | ||
--- | ||
|
||
# Running with VSTest | ||
BenchmarkDotNet has support for discovering and executing benchmarks through VSTest. This provides an alternative user experience to running benchmarks with the CLI and may be preferable for those who like their IDE's VSTest integrations that they may have used when running unit tests. | ||
|
||
Below is an example of running some benchmarks from the BenchmarkDotNet samples project in Visual Studio's Test Explorer. | ||
|
||
![](../../images/vs-testexplorer-demo.png) | ||
|
||
## About VSTest | ||
|
||
VSTest is one of the most popular test platforms in use in the .NET ecosystem, with test frameworks such as MSTest, xUnit, and NUnit providing support for it. Many IDEs, including Visual Studio and Rider, provide UIs for running tests through VSTest which some users may find more accessible than running them through the command line. | ||
|
||
It may seem counterintuitive to run performance tests on a platform that is designed for unit tests that expect a boolean outcome of "Passed" or "Failed", however VSTest provides good value as a protocol for discovering and executing tests. In addition, we can still make use of this boolean output to indicate if the benchmark had validation errors that caused them to fail to run. | ||
|
||
## Caveats and things to know | ||
- The VSTest adapter will not call your application's entry point. | ||
- If you use the entry point to customize how your benchmarks are run, you will need to do this through other means such as an assembly-level `IConfigSource`. | ||
- For more about this, please read: [Setting a default configuration](#setting-a-default-configuration). | ||
- The benchmark measurements may be affected by the VSTest host and your IDE | ||
- If you want to have more accurate performance results, it is recommended to run benchmarks through the CLI instead without other processes on the machine impacting performance. | ||
- This does not mean that the measurements are useless though, it will still be able to provide useful measurements during development when comparing different approaches. | ||
- The test adapter will not display or execute benchmarks if optimizations are disabled. | ||
- Please ensure you are compiling in Release mode or with `Optimize` set to true. | ||
- Using an `InProcess` toolchain will let you run your benchmarks with optimizations disabled and will let you attach the debugger as well. | ||
- The test adapter will generate an entry point for you automatically | ||
- The generated entry point will pass the command line arguments and the current assembly into `BenchmarkSwitcher`, so you can still use it in your CLI as well as in VSTest. | ||
- This means you can delete your entry point and only need to define your benchmarks. | ||
- If you want to use a custom entry point, you can still do so by setting `GenerateProgramFile` to `false` in your project file. | ||
|
||
## How to use it | ||
|
||
You need to install two packages into your benchmark project: | ||
|
||
- `BenchmarkDotNet.TestAdapter`: Implements the VSTest protocol for BenchmarkDotNet | ||
- `Microsoft.NET.Test.Sdk`: Includes all the pieces needed for the VSTest host to run and load the VSTest adapter. | ||
|
||
As mentioned in the caveats section, `BenchmarkDotNet.TestAdapter` will generate an entry point for you automatically, so if you have an entry point already you will either need to delete it or set `GenerateProgramFile` to `false` in your project file to continue using your existing one. | ||
|
||
After doing this, you can set your build configuration to `Release`, run a build, and you should be able to see the benchmarks in your IDE's VSTest integration. | ||
|
||
## Setting a default configuration | ||
|
||
Previously, it was common for the default configuration to be defined inside the entry point. Since the entry point is not used when running benchmarks through VSTest, the default configuration must be specified using a `Config` attribute instead that is set on the assembly. | ||
|
||
First, create a class that extends `ManualConfig` or `IConfig` which sets the default configuration you want: | ||
|
||
```csharp | ||
class MyDefaultConfig : ManualConfig | ||
{ | ||
public MyDefaultConfig() | ||
{ | ||
AddJob(Job.Dry); | ||
AddLogger(Loggers.ConsoleLogger.Default); | ||
AddValidator(JitOptimizationsValidator.DontFailOnError); | ||
} | ||
} | ||
``` | ||
|
||
Then, set an assembly attribute with the following. | ||
|
||
```csharp | ||
[assembly: Config(typeof(MyDefaultConfig))] | ||
``` | ||
|
||
By convention, assembly attributes are usually defined inside `AssemblyInfo.cs` in a directory called `Properties`. | ||
|
||
## Viewing the results | ||
The full output from BenchmarkDotNet that you would have been used to seeing in the past will be sent to the "Tests" output of your IDE. Use this view if you want to see the tabular view that compares multiple benchmarks with each other, or if you want to see the results for each individual iteration. | ||
|
||
One more place where you can view the results is in each individual test's output messages. In Visual Studio this can be viewed by clicking on the test in the Test Explorer after running it, and looking at the Test Detail Summary. Since this only displays statistics for a single benchmark case, it does not show the tabulated view that compares multiple benchmark cases, but instead displays a histogram and various other useful statistics. Not all IDEs support displaying these output messages, so you may only be able to view the results using the "Tests" output. |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
src/BenchmarkDotNet.TestAdapter/BenchmarkCaseExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
using BenchmarkDotNet.Attributes; | ||
using BenchmarkDotNet.Characteristics; | ||
using BenchmarkDotNet.Exporters; | ||
using BenchmarkDotNet.Extensions; | ||
using BenchmarkDotNet.Running; | ||
using Microsoft.TestPlatform.AdapterUtilities; | ||
using Microsoft.VisualStudio.TestPlatform.ObjectModel; | ||
using System; | ||
|
||
namespace BenchmarkDotNet.TestAdapter | ||
{ | ||
/// <summary> | ||
/// A set of extensions for BenchmarkCase to support converting to VSTest TestCase objects. | ||
/// </summary> | ||
internal static class BenchmarkCaseExtensions | ||
{ | ||
/// <summary> | ||
/// Converts a BDN BenchmarkCase to a VSTest TestCase. | ||
/// </summary> | ||
/// <param name="benchmarkCase">The BenchmarkCase to convert.</param> | ||
/// <param name="assemblyPath">The dll or exe of the benchmark project.</param> | ||
/// <param name="includeJobInName">Whether or not the display name should include the job name.</param> | ||
/// <returns>The VSTest TestCase.</returns> | ||
internal static TestCase ToVsTestCase(this BenchmarkCase benchmarkCase, string assemblyPath, bool includeJobInName = false) | ||
{ | ||
var benchmarkMethod = benchmarkCase.Descriptor.WorkloadMethod; | ||
var fullClassName = benchmarkCase.Descriptor.Type.GetCorrectCSharpTypeName(); | ||
var benchmarkMethodName = benchmarkCase.Descriptor.WorkloadMethod.Name; | ||
var benchmarkFullMethodName = $"{fullClassName}.{benchmarkMethodName}"; | ||
|
||
// Display name has arguments as well. | ||
var displayMethodName = FullNameProvider.GetMethodName(benchmarkCase); | ||
if (includeJobInName) | ||
displayMethodName += $" [{benchmarkCase.GetUnrandomizedJobDisplayInfo()}]"; | ||
|
||
var displayName = $"{fullClassName}.{displayMethodName}"; | ||
|
||
var vsTestCase = new TestCase(benchmarkFullMethodName, VsTestAdapter.ExecutorUri, assemblyPath) | ||
{ | ||
DisplayName = displayName, | ||
Id = GetTestCaseId(benchmarkCase) | ||
}; | ||
|
||
var benchmarkAttribute = benchmarkMethod.ResolveAttribute<BenchmarkAttribute>(); | ||
if (benchmarkAttribute != null) | ||
{ | ||
vsTestCase.CodeFilePath = benchmarkAttribute.SourceCodeFile; | ||
vsTestCase.LineNumber = benchmarkAttribute.SourceCodeLineNumber; | ||
} | ||
|
||
var categories = DefaultCategoryDiscoverer.Instance.GetCategories(benchmarkMethod); | ||
foreach (var category in categories) | ||
vsTestCase.Traits.Add("Category", category); | ||
|
||
vsTestCase.Traits.Add("", "BenchmarkDotNet"); | ||
|
||
return vsTestCase; | ||
} | ||
|
||
/// <summary> | ||
/// If an ID is not provided, a random string is used for the ID. This method will identify if randomness was | ||
/// used for the ID and return the Job's DisplayInfo with that randomness removed so that the same benchmark | ||
/// can be referenced across multiple processes. | ||
/// </summary> | ||
/// <param name="benchmarkCase">The benchmark case.</param> | ||
/// <returns>The benchmark case' job's DisplayInfo without randomness.</returns> | ||
internal static string GetUnrandomizedJobDisplayInfo(this BenchmarkCase benchmarkCase) | ||
{ | ||
var jobDisplayInfo = benchmarkCase.Job.DisplayInfo; | ||
if (!benchmarkCase.Job.HasValue(CharacteristicObject.IdCharacteristic) && benchmarkCase.Job.ResolvedId.StartsWith("Job-", StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
// Replace Job-ABCDEF with Job | ||
jobDisplayInfo = "Job" + jobDisplayInfo.Substring(benchmarkCase.Job.ResolvedId.Length); | ||
} | ||
|
||
return jobDisplayInfo; | ||
} | ||
|
||
/// <summary> | ||
/// Gets an ID for a given BenchmarkCase that is uniquely identifiable from discovery to execution phase. | ||
/// </summary> | ||
/// <param name="benchmarkCase">The benchmark case.</param> | ||
/// <returns>The test case ID.</returns> | ||
internal static Guid GetTestCaseId(this BenchmarkCase benchmarkCase) | ||
{ | ||
var testIdProvider = new TestIdProvider(); | ||
testIdProvider.AppendString(VsTestAdapter.ExecutorUriString); | ||
testIdProvider.AppendString(benchmarkCase.Descriptor.DisplayInfo); | ||
testIdProvider.AppendString(benchmarkCase.GetUnrandomizedJobDisplayInfo()); | ||
testIdProvider.AppendString(benchmarkCase.Parameters.DisplayInfo); | ||
return testIdProvider.GetId(); | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/BenchmarkDotNet.TestAdapter/BenchmarkDotNet.TestAdapter.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<Import Project="..\..\build\common.props" /> | ||
<PropertyGroup> | ||
<TargetFrameworks>netstandard2.0;net462</TargetFrameworks> | ||
<AssemblyTitle>BenchmarkDotNet.TestAdapter</AssemblyTitle> | ||
<AssemblyName>BenchmarkDotNet.TestAdapter</AssemblyName> | ||
<PackageId>BenchmarkDotNet.TestAdapter</PackageId> | ||
<ProduceReferenceAssembly>True</ProduceReferenceAssembly> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.TestPlatform.AdapterUtilities" Version="17.7.2" /> | ||
<PackageReference Include="Microsoft.TestPlatform.ObjectModel" Version="17.7.2" /> | ||
<PackageReference Include="Microsoft.TestPlatform.TranslationLayer" Version="17.7.2" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\BenchmarkDotNet\BenchmarkDotNet.csproj" /> | ||
</ItemGroup> | ||
|
||
<!-- Include files in nuget package for generating entry point --> | ||
<ItemGroup> | ||
<Compile Remove="Package\EntryPoint.*" /> | ||
<None Include="Package\EntryPoint.*" Pack="true" PackagePath="entrypoints\" /> | ||
<None Include="Package\BenchmarkDotNet.TestAdapter.props" Pack="true" PackagePath="build\" /> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using BenchmarkDotNet.Extensions; | ||
using BenchmarkDotNet.Helpers; | ||
using BenchmarkDotNet.Running; | ||
using BenchmarkDotNet.Toolchains; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
|
||
namespace BenchmarkDotNet.TestAdapter | ||
{ | ||
/// <summary> | ||
/// A class used for enumerating all the benchmarks in an assembly. | ||
/// </summary> | ||
internal static class BenchmarkEnumerator | ||
{ | ||
/// <summary> | ||
/// Returns all the BenchmarkRunInfo objects from a given assembly. | ||
/// </summary> | ||
/// <param name="assemblyPath">The dll or exe of the benchmark project.</param> | ||
/// <returns>The benchmarks inside the assembly.</returns> | ||
public static BenchmarkRunInfo[] GetBenchmarksFromAssemblyPath(string assemblyPath) | ||
{ | ||
var assembly = Assembly.LoadFrom(assemblyPath); | ||
|
||
var isDebugAssembly = assembly.IsJitOptimizationDisabled() ?? false; | ||
|
||
return GenericBenchmarksBuilder.GetRunnableBenchmarks(assembly.GetRunnableBenchmarks()) | ||
.Select(type => | ||
{ | ||
var benchmarkRunInfo = BenchmarkConverter.TypeToBenchmarks(type); | ||
if (isDebugAssembly) | ||
{ | ||
// If the assembly is a debug assembly, then only display them if they will run in-process | ||
// This will allow people to debug their benchmarks using VSTest if they wish. | ||
benchmarkRunInfo = new BenchmarkRunInfo( | ||
benchmarkRunInfo.BenchmarksCases.Where(c => c.GetToolchain().IsInProcess).ToArray(), | ||
benchmarkRunInfo.Type, | ||
benchmarkRunInfo.Config); | ||
} | ||
return benchmarkRunInfo; | ||
}) | ||
.Where(runInfo => runInfo.BenchmarksCases.Length > 0) | ||
.ToArray(); | ||
} | ||
} | ||
} |
Oops, something went wrong.