This guide outlines the steps to configure and use profiling tools for the C# components of the AgentSimMiddleware project, primarily focusing on the benchmarker console application.
- .NET 8.0 SDK installed.
- Visual Studio 2022 or later (if using Visual Studio Profiler) with the ".NET desktop development" workload installed.
- The
benchmarkerC# project successfully built. (Rundotnet build benchmarker/benchmarker.csproj)
dotnet-trace is a cross-platform command-line tool that can collect diagnostic traces from running .NET applications. It's excellent for CPU usage, GC events, and more.
If you don't have it already, install dotnet-trace as a global tool:
dotnet tool install --global dotnet-trace-
Build the
benchmarkerin Release configuration: Ensure you've built your application inReleasemode for accurate performance data.dotnet build benchmarker/benchmarker.csproj --configuration Release
-
Run
dotnet-traceto collect a trace:There are two main ways to use
dotnet-trace:a) Attach to a running process (Recommended for existing long-running processes):
-
First, start your
benchmarkerapplication:dotnet run --project benchmarker/benchmarker.csproj --configuration Release
Note down the process ID (PID) of the
benchmarkerapplication. You can usually find this in Task Manager (Windows) orps(Linux/macOS). -
In a new terminal, run
dotnet-traceto collect a trace for a specified duration (e.g., 10 seconds):dotnet trace collect -p <PID> --duration 00:00:10 --output benchmarker_trace.nettrace
Replace
<PID>with the actual process ID.
b) Launch the application with
dotnet-trace(Recommended for starting a new profiling session):This method will start your application and immediately begin profiling it.
dotnet trace collect --output benchmarker_trace.nettrace --duration 00:00:10 -- dotnet run --project benchmarker/benchmarker.csproj --configuration Release
This command will run the
benchmarkerfor 10 seconds and save the trace tobenchmarker_trace.nettrace. -
-
Analyze the trace file: The generated
.nettracefile can be opened and analyzed using Visual Studio (requires the "Profiling tools" component) or PerfView (a free performance analysis tool from Microsoft).- Using Visual Studio:
- Open Visual Studio.
- Go to
File > Open > File...and selectbenchmarker_trace.nettrace. - Visual Studio will open the Diagnostic Tools window and provide an analysis of the trace.
- Using Visual Studio:
The Visual Studio Performance Profiler can also be used for C# applications, offering a similar experience to C++ profiling.
-
Open the Solution in Visual Studio:
- Open Visual Studio.
- Go to
File > Open > Project/Solution...and selectbenchmarker/benchmarker.csproj.
-
Set Build Configuration to Release:
- Ensure your project is set to
Releaseconfiguration for accurate results.
- Ensure your project is set to
-
Open Performance Profiler:
- Go to Debug > Performance Profiler (or press
Alt+F2).
- Go to Debug > Performance Profiler (or press
-
Choose Profiling Tools:
- Select "CPU Usage" for a general performance overview. Other tools like "Memory Usage" might also be relevant.
-
Start Profiling:
- Click the Start button. Visual Studio will launch the
benchmarkerapplication. - The console application will run, and profiling data will be collected.
- Once the
benchmarkerfinishes execution, the profiler will generate a report.
- Click the Start button. Visual Studio will launch the
-
Analyze the Report:
- Review the report to identify CPU-intensive methods, hot paths, and other performance bottlenecks within your C# code.