-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Add documentation for VS Profiler #2672
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
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,71 @@ | ||
| --- | ||
| uid: docs.vsprofiler | ||
| name: VS Profiler | ||
| --- | ||
|
|
||
| # Running with Visual Studio profiler | ||
| Visual Studio supports [profiler integration with BenchmarkDotNet](https://learn.microsoft.com/visualstudio/profiling/profiling-with-benchmark-dotnet) on Windows through its [Microsoft.VisualStudio.BenchmarkDotNetDiagnosers](https://www.nuget.org/packages/Microsoft.VisualStudio.DiagnosticsHub.BenchmarkDotNetDiagnosers) NuGet package. Once installed, Visual Studio specific diagnosers will capture performance data in runs and automatically open traces if launched through Visual Studio | ||
|
|
||
|  | ||
|
|
||
| ## How it works | ||
|
|
||
| First, install the [Microsoft.VisualStudio.BenchmarkDotNetDiagnosers](https://www.nuget.org/packages/Microsoft.VisualStudio.DiagnosticsHub.BenchmarkDotNetDiagnosers) NuGet package in your benchmarking project. Next add one or more of the Visual Studio diagnosers to your benchmark to capture the relevant profiling information while benchmarking. Lastly, run your benchmarks and a diagsession will be generated. If run from Visual Studio the diagsession will automatically be opened. | ||
|
|
||
| ## Available Diagnosers | ||
|
|
||
| * `[CPUUsageDiagnoser]` - Enables the [CPU Usage tool](https://learn.microsoft.com/visualstudio/profiling/cpu-usage). | ||
| * `[DatabaseDiagnoser]` - Enables the [Database tool](https://learn.microsoft.com/visualstudio/profiling/analyze-database) | ||
| * `[DotNetCountersDiagnoser]` - Enables the [.NET Counters tool](https://learn.microsoft.com/visualstudio/profiling/dotnet-counters-tool) | ||
| * `[DotNetObjectAllocDiagnoser]` - Enables the [.NET Object Allocation tool](https://learn.microsoft.com/visualstudio/profiling/dotnet-alloc-tool). When using this tool, you must also specify `[DotNetObjectAllocJobConfiguration]` on the benchmark. If this is missing the run will fail and you will receive an error indicating you need to add it. | ||
| * `[EventsDiagnoser]` - Enables the [Events tool](https://learn.microsoft.com/visualstudio/profiling/events-viewer) | ||
| * `[FileIODiagnoser]` - Enables the [File IO tool](https://learn.microsoft.com/visualstudio/profiling/use-file-io) | ||
|
|
||
| ## How to use it? | ||
|
|
||
| After installing the [Microsoft.VisualStudio.BenchmarkDotNetDiagnosers](https://www.nuget.org/packages/Microsoft.VisualStudio.DiagnosticsHub.BenchmarkDotNetDiagnosers) NuGet package add the following code as a benchmark: | ||
|
|
||
| ```cs | ||
| using System; | ||
| using System.Security.Cryptography; | ||
| using BenchmarkDotNet.Attributes; | ||
| using BenchmarkDotNet.Running; | ||
| using Microsoft.VSDiagnostics; | ||
|
|
||
| namespace MyBenchmarks | ||
| { | ||
| [CPUUsageDiagnoser] | ||
| public class Md5VsSha256 | ||
| { | ||
| private const int N = 10000; | ||
| private readonly byte[] data; | ||
|
|
||
| private readonly SHA256 sha256 = SHA256.Create(); | ||
| private readonly MD5 md5 = MD5.Create(); | ||
|
|
||
| public Md5VsSha256() | ||
| { | ||
| data = new byte[N]; | ||
| new Random(42).NextBytes(data); | ||
| } | ||
|
|
||
| [Benchmark] | ||
| public byte[] Sha256() => sha256.ComputeHash(data); | ||
|
|
||
| [Benchmark] | ||
| public byte[] Md5() => md5.ComputeHash(data); | ||
| } | ||
|
|
||
| public class Program | ||
| { | ||
| public static void Main(string[] args) | ||
| { | ||
| var summary = BenchmarkRunner.Run(typeof(Program).Assembly); | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| In this case we have added the `[CpuUsageDiagnoser]` to capture a CPU sampling trace. From here run the benchmark in Visual Studio (Ctrl+F5), and after the benchmark run the resulting diagsession will be displayed. Double clicking on one of the benchmark rows shown under the Benchmarks tab will filter the time selection to the specific benchmark allowing you to better isolate and investigate. | ||
|
|
||
|  | ||
This file contains hidden or 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,23 @@ | ||
| --- | ||
| uid: BenchmarkDotNet.Samples.IntroVisualStudioProfiler | ||
| --- | ||
|
|
||
| ## Sample: Visual Studio Profiler | ||
|
|
||
| Using the [Microsoft.VisualStudio.BenchmarkDotNetDiagnosers](https://www.nuget.org/packages/Microsoft.VisualStudio.DiagnosticsHub.BenchmarkDotNetDiagnosers) NuGet package you can capture performance profiles of your benchmarks that can be opened in Visual Studio. | ||
|
|
||
| ### Source code | ||
|
|
||
| [!code-csharp[IntroVisualStudioDiagnoser.cs](../../../samples/BenchmarkDotNet.Samples/IntroVisualStudioDiagnoser.cs)] | ||
|
|
||
| ### Output | ||
| The output will contain a path to the collected diagsession and automatically open in Visual Studio when launched from it. | ||
|
|
||
| ```markdown | ||
| // * Diagnostic Output - VSDiagnosticsDiagnoser * | ||
| Collection result moved to 'C:\Work\BenchmarkDotNet\samples\BenchmarkDotNet.Samples\bin\Release\net8.0\BenchmarkDotNet.Artifacts\BenchmarkDotNet_IntroVisualStudioProfiler_20241205_192056.diagsession'. | ||
| Session : {d54ebddb-2d6d-404f-b1da-10acbc89635f} | ||
| Stopped | ||
| Exported diagsession file: C:\Work\BenchmarkDotNet\samples\BenchmarkDotNet.Samples\bin\Release\net8.0\BenchmarkDotNet.Artifacts\BenchmarkDotNet_IntroVisualStudioProfiler_20241205_192056.diagsession. | ||
| Opening diagsession in VisualStudio: 15296 | ||
| ``` |
This file contains hidden or 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
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 hidden or 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
23 changes: 23 additions & 0 deletions
23
samples/BenchmarkDotNet.Samples/IntroVisualStudioDiagnoser.cs
This file contains hidden or 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,23 @@ | ||
| using System; | ||
| using BenchmarkDotNet.Attributes; | ||
| using Microsoft.VSDiagnostics; | ||
|
|
||
| namespace BenchmarkDotNet.Samples | ||
| { | ||
| // Enables profiling with the CPU Usage tool | ||
| // See: https://learn.microsoft.com/visualstudio/profiling/profiling-with-benchmark-dotnet | ||
| [CPUUsageDiagnoser] | ||
karpinsn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| public class IntroVisualStudioProfiler | ||
| { | ||
| private readonly Random rand = new Random(42); | ||
|
|
||
| [Benchmark] | ||
| public void BurnCPU() | ||
| { | ||
| for (int i = 0; i < 100000; ++i) | ||
| { | ||
| rand.Next(1, 100); | ||
| } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.