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
- Analyze performance by using CPU profiling in the Performance Profiler
- Identify hot paths with a flame graph
Profiling
First and foremost, add the following lines to your project's Cargo.toml
file:
[profile.release]
debug=1
After saving, building the release will also include debug information:
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".
Then, go to the 'Debug' menu and open 'Performance Profiler'.
Select the executable file (exe) as the target for analysis.
Set the working directory to the project's directory, and locate the compiled executable file in the project's target/release
directory.
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.
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.
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.