docs/articles/features/vstest.md
BenchmarkDotNet supports 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.
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 it to fail to run.
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.IConfigSource,
as shown here.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.BenchmarkDotNet.TestAdapter: Implements the VSTest protocol for BenchmarkDotNetMicrosoft.NET.Test.Sdk: Includes all the pieces needed for the VSTest host to run and load the VSTest adapter.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.
Here is an example of a .csproj file based on the default Console Application template:<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<!-- Disable entry point generation as this project has it's own entry point -->
<GenerateProgramFile>false</GenerateProgramFile>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BenchmarkDotNet.TestAdapter" Version="0.16.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.1" />
</ItemGroup>
</Project>
Release configuration.If you correctly performed all the steps above, you should be able to run your benchmarks in your IDE using embedded unit testing features. If this doesn't work for you, don't hesitate to file a new GitHub issue.
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 that is set on the assembly instead.
First, create a class that extends ManualConfig or IConfig which sets the default configuration you want:
class MyDefaultConfig : ManualConfig
{
public MyDefaultConfig()
{
AddJob(Job.Dry);
AddLogger(Loggers.ConsoleLogger.Default);
AddValidator(JitOptimizationsValidator.DontFailOnError);
}
}
Then, set an assembly attribute with the following.
[assembly: Config(typeof(MyDefaultConfig))]
By convention, assembly attributes are usually defined inside AssemblyInfo.cs in a directory called Properties.
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. Instead, it 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.