Skip to content

Latest commit

 

History

History
94 lines (63 loc) · 4.13 KB

File metadata and controls

94 lines (63 loc) · 4.13 KB

C# Profiling Guide using dotnet-trace and Visual Studio Profiler

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.

Prerequisites

  • .NET 8.0 SDK installed.
  • Visual Studio 2022 or later (if using Visual Studio Profiler) with the ".NET desktop development" workload installed.
  • The benchmarker C# project successfully built. (Run dotnet build benchmarker/benchmarker.csproj)

1. Profiling with dotnet-trace (Command-Line Tool)

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.

Installation

If you don't have it already, install dotnet-trace as a global tool:

dotnet tool install --global dotnet-trace

Steps to Profile the benchmarker Application:

  1. Build the benchmarker in Release configuration: Ensure you've built your application in Release mode for accurate performance data.

    dotnet build benchmarker/benchmarker.csproj --configuration Release
  2. Run dotnet-trace to 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 benchmarker application:

      dotnet run --project benchmarker/benchmarker.csproj --configuration Release

      Note down the process ID (PID) of the benchmarker application. You can usually find this in Task Manager (Windows) or ps (Linux/macOS).

    • In a new terminal, run dotnet-trace to 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 benchmarker for 10 seconds and save the trace to benchmarker_trace.nettrace.

  3. Analyze the trace file: The generated .nettrace file 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:
      1. Open Visual Studio.
      2. Go to File > Open > File... and select benchmarker_trace.nettrace.
      3. Visual Studio will open the Diagnostic Tools window and provide an analysis of the trace.

2. Profiling with Visual Studio Profiler (IDE Integrated)

The Visual Studio Performance Profiler can also be used for C# applications, offering a similar experience to C++ profiling.

Steps to Run a Performance Profiling Session:

  1. Open the Solution in Visual Studio:

    • Open Visual Studio.
    • Go to File > Open > Project/Solution... and select benchmarker/benchmarker.csproj.
  2. Set Build Configuration to Release:

    • Ensure your project is set to Release configuration for accurate results.
  3. Open Performance Profiler:

    • Go to Debug > Performance Profiler (or press Alt+F2).
  4. Choose Profiling Tools:

    • Select "CPU Usage" for a general performance overview. Other tools like "Memory Usage" might also be relevant.
  5. Start Profiling:

    • Click the Start button. Visual Studio will launch the benchmarker application.
    • The console application will run, and profiling data will be collected.
    • Once the benchmarker finishes execution, the profiler will generate a report.
  6. Analyze the Report:

    • Review the report to identify CPU-intensive methods, hot paths, and other performance bottlenecks within your C# code.