Skip to content

Performance Debugging

Amin Torabi edited this page Sep 23, 2024 · 1 revision

Performance Debugging with PyInstrument

Overview

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.

Installing PyInstrument

To install PyInstrument, run the following command:

pip install pyinstrument

Using PyInstrument in T-Route

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.

  1. Import and Initialize the Profiler:

    from pyinstrument import Profiler
    
    profiler = Profiler()
  2. Start the Profiler:

    Place profiler.start() at the beginning of the section of code you want to profile. For example, at the beginning of main_v04 in T-Route:

    profiler.start()
  3. 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.

  4. 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.

  5. 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.

Example Usage in T-Route

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))

Interpreting the Results

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.

Benefits of Using PyInstrument

  • 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:

image

Clone this wiki locally