Make a flame graph from the output of `-X importtime`.
Project description
Overview
This is a CLI tool and small library to parse the output of python -X importtime and convert it to other formats. Use it to help visualize and understand your Python import times—for example, by generating flame graphs.
Usage as a CLI tool
This tool expects the raw -X importtime data on stdin and outputs the converted result on stdout.
Typical usage example, with Bash redirection syntax:
# Python outputs its raw importtime data to stderr. Save it to raw_importtime.txt.
python -X importtime -c 'import module_to_test' 2> raw_importtime.txt
# Convert it to "flamegraph.pl" format and save that to converted_importtime.txt.
python -m importtime_convert --output-format flamegraph.pl <raw_importtime.txt >converted_importtime.txt
Or, all in one step:
python -X importtime -c 'import module_to_test' \
2>&1 >/dev/null
| python -X importtime_convert --output-format flamegraph.pl
> converted_importtime.txt
See the available output formats below and python -m importtime_convert --help for full details.
Usage as a library
The following are available from import importtime_convert:
parse(input: typing.TextIO | str) -> list[Import]
parse() takes the raw -X importtime data, as a string or as a file-like object. It returns the parsed import structure as a list of top-level imports. The list is in the order that the interpreter traversed them.
Import
An Import is a dataclass with the following keys:
package: str: The full package path of this import, e.g."foo.bar".cumulative_us: int: The time, in microseconds, that the interpreter spent on this module, including any subimports.self_us: int: The time, in microseconds, that the interpreter spent on this module, not including any subimports.subimports: list[Import]: This module's subimports. The list is in the order that the interpreter traversed them.
Output conversion functions
to_flamegraph_pl(imports: list[Import]) -> strto_json_serializable(imports: list[Import]) -> list[dict[str, Any]]
See the available output formats below for details.
Available output formats
-
--output-format flamegraph.pl(CLI) /to_flamegraph_pl()(API)For flame graph generation tools.
The format is defined by Brendan Gregg's flamegraph.pl script, but other flame graph tools accept it, too. (For example, https://www.speedscope.app/ and https://flamegraph.com/.)
-
--output-format json(CLI) /to_json_serializable()(API)A simple JSON format specific to this tool. Looks like this:
[ { "package": "foo", "self_us": 200, "cumulative_us": 300, "subimports": [ { "package": "bar", "self_us": 100, "cumulative_us": 100, "subimports": [] } ] }, { "package": "baz.baz", "self_us": 100, "cumulative_us": 100, "subimports": [] } ]
See the API docs above for the meanings of the fields.
Feature requests and pull requests are welcome for additional output formats.
Related work and alternatives
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file importtime_convert-1.0.0.tar.gz.
File metadata
- Download URL: importtime_convert-1.0.0.tar.gz
- Upload date:
- Size: 7.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.0.1 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fce1ec78dcfe7e6aae202eba077a47b38543ea888be3f2fbb7360d4ddff10649
|
|
| MD5 |
0aa38f7c7430e2c21b0fa3d0cb8eee7d
|
|
| BLAKE2b-256 |
aa9953be8953938ecd94155b1d88af26152c5adf156faefffe40a0504e0e8ba2
|
Provenance
The following attestation bundles were made for importtime_convert-1.0.0.tar.gz:
Publisher:
deploy.yml on SyntaxColoring/importtime_convert
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
importtime_convert-1.0.0.tar.gz -
Subject digest:
fce1ec78dcfe7e6aae202eba077a47b38543ea888be3f2fbb7360d4ddff10649 - Sigstore transparency entry: 157650998
- Sigstore integration time:
-
Permalink:
SyntaxColoring/importtime_convert@3bc96a66beabbd4f4c825adbf3336ce719afeadf -
Branch / Tag:
refs/heads/main - Owner: https://github.com/SyntaxColoring
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
deploy.yml@3bc96a66beabbd4f4c825adbf3336ce719afeadf -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file importtime_convert-1.0.0-py3-none-any.whl.
File metadata
- Download URL: importtime_convert-1.0.0-py3-none-any.whl
- Upload date:
- Size: 7.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.0.1 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6a3e535d1d79e205d9f8f894fc026725387821966327097ace6215f5b006549a
|
|
| MD5 |
24605b2f60ea9b575b06269712c732fa
|
|
| BLAKE2b-256 |
35599965bc186efe373d3d88b6d51bf9ef812b146d68fc4979fce504c9bc5de4
|
Provenance
The following attestation bundles were made for importtime_convert-1.0.0-py3-none-any.whl:
Publisher:
deploy.yml on SyntaxColoring/importtime_convert
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
importtime_convert-1.0.0-py3-none-any.whl -
Subject digest:
6a3e535d1d79e205d9f8f894fc026725387821966327097ace6215f5b006549a - Sigstore transparency entry: 157650999
- Sigstore integration time:
-
Permalink:
SyntaxColoring/importtime_convert@3bc96a66beabbd4f4c825adbf3336ce719afeadf -
Branch / Tag:
refs/heads/main - Owner: https://github.com/SyntaxColoring
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
deploy.yml@3bc96a66beabbd4f4c825adbf3336ce719afeadf -
Trigger Event:
workflow_dispatch
-
Statement type: