GitXplorerGitXplorer
m

import-timer

public
11 stars
1 forks
0 issues

Commits

List of commits on branch main.
Unverified
3a6056c79cd83754e4662369503c147fbc5dc56c

Oop

mmuellerzr committed 7 months ago
Unverified
65c7778fa055336aa8a8d63754d37bf9d9b672f8

Update readme

mmuellerzr committed 7 months ago
Unverified
bc0af048ee9414d24c373591271d7eb040648803

Rename

mmuellerzr committed 7 months ago
Unverified
92fc541889b4425cd6e6e8584733b37c5885da44

Finish tests

mmuellerzr committed 7 months ago
Unverified
4770c45bb0b2af4dd3c3e004e80080d11b13f696

Make it not need a file

mmuellerzr committed 7 months ago
Unverified
cabd8d4c971970370c78c809599be4ca22d417f1

Fix missing import

mmuellerzr committed 7 months ago

README

The README file for this repository.

import-timer

Pragmatic approach to parsing importtime import profiles

While tuna is a fantastic way to visualize slowdowns in your python code, it lacks a native python-parsing API to allow for CI-based checks to determine or automatically flag if code is suddenly much slower (such as due to new imports).

import-timer acts as a middle-ground. Create a profile using python -X importtime, then read it in with our utilities to help you parse and interrogate the graph by hand, establish baselines, and then let you apply these easily later in your tests.

Installation

pip install import-timer

How to Use

  1. In an interactive instance, create an importtime log using python -X such as:
import subprocess
output = subprocess.run(["python3", "-X", "importtime", "-c", command], capture_output=True, text=True)
  1. Load in the logs via import-timer:
from import-timer import read_import_profile

data = read_import_profile(output)
  1. Interrogate your log using our helpers.

Supported functionality:

calculate_total_time

Will calculate the total time each tree in the data takes up by summing all of the times calculated down each node. This is an inplace operation, and will add a new key (total_time) to each node.

from import_timer import read_import_profile, calculate_total_time

data = read_import_profile(...)
total_time = calculate_total_time(data)
print(total_time)

sort_nodes_by_total_time

Will sort all children of the passed in tree inplace based on the total time

from import_timer import read_import_profile, calculate_total_time

data = read_import_profile(...)
calculate_total_time(data)
sort_nodes_by_total_time(data)
print(data["children"][0]["total_time"])

get_paths_above_threshold

A useful function that will take all of the nodes in data and then limit them to a threshold of which paths to return. Helpful in cases where you only want import paths which take up >30% of the time, or a particular minimum amount of seconds to start debugging

from import_timer import read_import_profile, calculate_total_time

data = read_import_profile(...)
calculate_total_time(data)
percentage_threshold = 20  # Threshold as a percentage of total time
threshold_time = total_time * (percentage_threshold / 100)  # Convert percentage to actual time threshold
max_depth = 7 # How deep in the tree do we want to report back paths
important_paths = get_paths_above_threshold(
    data_copy, threshold_time, max_depth
)
print(important_paths)

Explore our tests/ to see more, or this PR where this library was first utilized