Skip to content

Commit 6367ad8

Browse files
karpinsnNik Karpinsky
andauthored
Add documentation for VS Profiler (#2672)
* Add documentation for VS Profiler - This change adds documentation for the Visual Studio profiler. There is a new document under features that explains how to use the nuget package and a new sample under IntroVisualStudioDiagnoser. A new package reference has also been added to the samples project to pull in the Visual Studio specific diagnosers for the sample project. * Addressing PR feedback --------- Co-authored-by: Nik Karpinsky <[email protected]>
1 parent 5f0c47b commit 6367ad8

File tree

8 files changed

+125
-0
lines changed

8 files changed

+125
-0
lines changed

build/cSpell.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"Cygwin",
1313
"Diagnoser",
1414
"diagnosers",
15+
"diagsession",
1516
"disassemblers",
1617
"disassm",
1718
"Jits",
@@ -29,6 +30,7 @@
2930
"Pseudocode",
3031
"runtimes",
3132
"Serilog",
33+
"vsprofiler",
3234
"vstest",
3335
"Tailcall",
3436
"toolchains",

docs/articles/features/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,7 @@
1212
href: etwprofiler.md
1313
- name: EventPipeProfiler
1414
href: event-pipe-profiler.md
15+
- name: VSProfiler
16+
href: vsprofiler.md
1517
- name: VSTest
1618
href: vstest.md

docs/articles/features/vsprofiler.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
---
2+
uid: docs.vsprofiler
3+
name: VS Profiler
4+
---
5+
6+
# Running with Visual Studio profiler
7+
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
8+
9+
![](../../images/vs-profiler-demo.png)
10+
11+
## How it works
12+
13+
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.
14+
15+
## Available Diagnosers
16+
17+
* `[CPUUsageDiagnoser]` - Enables the [CPU Usage tool](https://learn.microsoft.com/visualstudio/profiling/cpu-usage).
18+
* `[DatabaseDiagnoser]` - Enables the [Database tool](https://learn.microsoft.com/visualstudio/profiling/analyze-database)
19+
* `[DotNetCountersDiagnoser]` - Enables the [.NET Counters tool](https://learn.microsoft.com/visualstudio/profiling/dotnet-counters-tool)
20+
* `[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.
21+
* `[EventsDiagnoser]` - Enables the [Events tool](https://learn.microsoft.com/visualstudio/profiling/events-viewer)
22+
* `[FileIODiagnoser]` - Enables the [File IO tool](https://learn.microsoft.com/visualstudio/profiling/use-file-io)
23+
24+
## How to use it?
25+
26+
After installing the [Microsoft.VisualStudio.BenchmarkDotNetDiagnosers](https://www.nuget.org/packages/Microsoft.VisualStudio.DiagnosticsHub.BenchmarkDotNetDiagnosers) NuGet package add the following code as a benchmark:
27+
28+
```cs
29+
using System;
30+
using System.Security.Cryptography;
31+
using BenchmarkDotNet.Attributes;
32+
using BenchmarkDotNet.Running;
33+
using Microsoft.VSDiagnostics;
34+
35+
namespace MyBenchmarks
36+
{
37+
[CPUUsageDiagnoser]
38+
public class Md5VsSha256
39+
{
40+
private const int N = 10000;
41+
private readonly byte[] data;
42+
43+
private readonly SHA256 sha256 = SHA256.Create();
44+
private readonly MD5 md5 = MD5.Create();
45+
46+
public Md5VsSha256()
47+
{
48+
data = new byte[N];
49+
new Random(42).NextBytes(data);
50+
}
51+
52+
[Benchmark]
53+
public byte[] Sha256() => sha256.ComputeHash(data);
54+
55+
[Benchmark]
56+
public byte[] Md5() => md5.ComputeHash(data);
57+
}
58+
59+
public class Program
60+
{
61+
public static void Main(string[] args)
62+
{
63+
var summary = BenchmarkRunner.Run(typeof(Program).Assembly);
64+
}
65+
}
66+
}
67+
```
68+
69+
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.
70+
71+
![](../../images/vs-profiler-demo.png)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
uid: BenchmarkDotNet.Samples.IntroVisualStudioProfiler
3+
---
4+
5+
## Sample: Visual Studio Profiler
6+
7+
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.
8+
9+
### Source code
10+
11+
[!code-csharp[IntroVisualStudioDiagnoser.cs](../../../samples/BenchmarkDotNet.Samples/IntroVisualStudioDiagnoser.cs)]
12+
13+
### Output
14+
The output will contain a path to the collected diagsession and automatically open in Visual Studio when launched from it.
15+
16+
```markdown
17+
// * Diagnostic Output - VSDiagnosticsDiagnoser *
18+
Collection result moved to 'C:\Work\BenchmarkDotNet\samples\BenchmarkDotNet.Samples\bin\Release\net8.0\BenchmarkDotNet.Artifacts\BenchmarkDotNet_IntroVisualStudioProfiler_20241205_192056.diagsession'.
19+
Session : {d54ebddb-2d6d-404f-b1da-10acbc89635f}
20+
Stopped
21+
Exported diagsession file: C:\Work\BenchmarkDotNet\samples\BenchmarkDotNet.Samples\bin\Release\net8.0\BenchmarkDotNet.Artifacts\BenchmarkDotNet_IntroVisualStudioProfiler_20241205_192056.diagsession.
22+
Opening diagsession in VisualStudio: 15296
23+
```

docs/articles/samples/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@
124124
href: IntroTagColumn.md
125125
- name: IntroTailcall
126126
href: IntroTailcall.md
127+
- name: IntroVisualStudioProfiler
128+
href: IntroVisualStudioProfiler.md
127129
- name: IntroWasm
128130
href: IntroWasm.md
129131
- name: IntroUnicode

docs/images/vs-profiler-demo.png

59.4 KB
Loading

samples/BenchmarkDotNet.Samples/BenchmarkDotNet.Samples.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
<PackageReference Include="System.Drawing.Common" Version="4.7.2" />
2424
<!-- The Test SDK is required only for the VSTest Adapter to work -->
2525
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
26+
<!-- This package enables the Visual Studio Profiler integration IntroVisualStudioProfiler.cs -->
27+
<PackageReference Include="Microsoft.VisualStudio.DiagnosticsHub.BenchmarkDotNetDiagnosers" Version="17.13.35606.1" />
2628
</ItemGroup>
2729
<ItemGroup>
2830
<ProjectReference Include="..\..\src\BenchmarkDotNet.Diagnostics.dotTrace\BenchmarkDotNet.Diagnostics.dotTrace.csproj" />
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using BenchmarkDotNet.Attributes;
3+
using Microsoft.VSDiagnostics;
4+
5+
namespace BenchmarkDotNet.Samples
6+
{
7+
// Enables profiling with the CPU Usage tool
8+
// See: https://learn.microsoft.com/visualstudio/profiling/profiling-with-benchmark-dotnet
9+
[CPUUsageDiagnoser]
10+
public class IntroVisualStudioProfiler
11+
{
12+
private readonly Random rand = new Random(42);
13+
14+
[Benchmark]
15+
public void BurnCPU()
16+
{
17+
for (int i = 0; i < 100000; ++i)
18+
{
19+
rand.Next(1, 100);
20+
}
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)