Profiling Rust with VS on Windows
Published: 2024-05-23 10:00:00

# Preface

This all began while I was developing a tool in Rust for reading from a database, processing data, and writing back to it. The program was taking too long to run, and I couldn't figure out where the bottleneck was. Online searches for 'how to profile Rust on Windows' mostly pointed to using Visual Studio (with some mentions of WPA), yet I found no straightforward guides for beginners, **especially as a VS Code user**. After some trial and error, I've put together this article.

It's not a full-fledged profiling tutorial, but more a record of my own experiences. I've also experimented with Intel Vtune, which I quite like, but it occupies a hefty 2GB of space. Considering that installing Rust on Windows seems to inevitably involve downloading Visual Studio (I'm not aware of how to avoid it), I've decided to document the profiling process using VS.

This is just a simple tutorial. For more detailed information, please refer to the Visual Studio documentation:
- [First look at profiling tools](https://learn.microsoft.com/en-us/visualstudio/profiling/profiling-feature-tour?view=vs-2022&WT.mc_id=studentamb_228125)
- [Analyze performance by using CPU profiling in the Performance Profiler](https://learn.microsoft.com/en-us/visualstudio/profiling/cpu-usage?view=vs-2022&WT.mc_id=studentamb_228125)
- [Identify hot paths with a flame graph](https://learn.microsoft.com/en-us/visualstudio/profiling/flame-graph?view=vs-2022&WT.mc_id=studentamb_228125)

# Profiling 

First and foremost, add the following lines to your project's `Cargo.toml` file:
```toml
[profile.release]
debug=1
```

After saving, building the release will also include debug information:

```powershell
cargo build --release
    Finished release [optimized + debuginfo] target(s) in 0.10s
```

Next, open Visual Studio (I'm using VS 2022). Since I'm only planning to use VS as a performance analysis tool, click "Continue without code".


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kx5a4nacf6tj086t851o.png)

Then, go to the 'Debug' menu and open 'Performance Profiler'.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8mh3y6swzxh1vw599c04.png)

Select the executable file (exe) as the target for analysis.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iwkay5azkll74vzglih3.png)

Set the working directory to the project's directory, and locate the compiled executable file in the project's `target/release` directory.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7ec0g3sk2m5pev798dx0.png)

Choose 'CPU Usage' and then click 'Start' to begin executing the file and recording. You can wait for the file to finish running or interrupt it yourself. After completion, Visual Studio will automatically generate a report.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p9emqez9xor4ma0sc2st.png)

The generated report includes information on the execution time of functions and their sub-functions, and it also lists the functions that consume the most time. Clicking 'Open details' will open a more detailed report.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/25bwch7pi71fo49silmd.png)

In the upper left corner, you can switch the displayed view. After clicking on a function, Visual Studio will also display the specific code and the time consumed by each line of code. The flame graph can also be used to display code performance details.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m6ktlina0hzsrzo0dmam.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0iisgbkg3151v8mlzc5f.png)

# Reference

- [First look at profiling tools](https://learn.microsoft.com/en-us/visualstudio/profiling/profiling-feature-tour?view=vs-2022&WT.mc_id=studentamb_228125)
- [Analyze performance by using CPU profiling in the Performance Profiler](https://learn.microsoft.com/en-us/visualstudio/profiling/cpu-usage?view=vs-2022&WT.mc_id=studentamb_228125)
- [Identify hot paths with a flame graph](https://learn.microsoft.com/en-us/visualstudio/profiling/flame-graph?view=vs-2022&WT.mc_id=studentamb_228125)