-
Notifications
You must be signed in to change notification settings - Fork 50
Performance Debugging
When working with T-Route, certain functions may take longer to execute than expected, creating performance bottlenecks. PyInstrument is a powerful profiling tool that helps you identify these bottlenecks by providing a hierarchical call graph of your code's execution. It breaks down the time spent in each function and sub-function, making it easy to pinpoint where optimizations are needed.
To install PyInstrument, run the following command:
pip install pyinstrument
Here’s a basic example of how to use PyInstrument to profile the T-Route code. You can place the profiler at the start and end of the main function, such as main_v04
, to get a detailed breakdown of execution times.
-
Import and Initialize the Profiler:
from pyinstrument import Profiler profiler = Profiler()
-
Start the Profiler:
Place
profiler.start()
at the beginning of the section of code you want to profile. For example, at the beginning ofmain_v04
in T-Route:profiler.start()
-
Run the Code:
Run the main body of the T-Route code as usual. The profiler will capture all function calls and their execution times during this period.
-
Stop the Profiler and Print the Results:
After the code block you want to analyze, stop the profiler and print the results:
profiler.stop() profiler.print()
This will display a detailed report in the console, showing where time is being spent in the code.
-
Saving the Profiler Output (Optional):
If you want to save the profiling results to a file for later analysis, you can use the following code:
with open('<Location you want to save>/pyinstrument_results.log', 'w') as f: f.write(profiler.output_text(unicode=True))
Replace
<Location you want to save>
with the desired path to store the log file. This will create a log file that breaks down the execution time for each function and all its sub-functions.
from pyinstrument import Profiler
# Initialize the profiler
profiler = Profiler()
# Start profiling
profiler.start()
# Insert the main function of T-Route here, e.g., main_v04()
main_v04()
# Stop profiling
profiler.stop()
# Print the profiling results
profiler.print()
# Optional: Save the results to a file
with open('logs/pyinstrument_results.log', 'w') as f:
f.write(profiler.output_text(unicode=True))
The output from PyInstrument provides a detailed view of the time spent in each function, including all sub-function calls. It allows you to:
- Identify Bottlenecks: See which functions or operations are taking the most time, so you know where to focus your optimization efforts.
- Understand Execution Flow: Visualize the call stack to understand how different parts of the code interact and contribute to the overall execution time.
- Optimize Performance: Use the insights gained from the profiler to refactor or parallelize code sections, improving overall performance.
- Quick Insights: Provides a quick overview of time distribution across functions without requiring any manual instrumentation of code.
- Hierarchical Call Graph: Helps visualize the nested structure of function calls, making it easier to understand complex execution flows.
- Easy Integration: Simple to integrate and use within existing codebases without significant changes.
By incorporating PyInstrument into your T-Route workflow, you can effectively diagnose and resolve performance issues, ensuring the model runs efficiently. Here's a sample of what it will be look like:
- Overview
- Hydrofabric Integration
- Input Forcing
- Domain Data
- Data Formats
- CLI
- BMI Tutorial
- Lower Colorado, TX example
- Larger Domains (e.g. CONUS)