Skip to main content

A Python script profiler using @profile decorators

Project description

Line Profiler Extension for Python (lprof_ext)

lprof_ext is a lightweight Python script profiler that seamlessly integrates with the line_profiler library. It simplifies performance analysis by automatically adding @profile decorators to your Python code and collecting detailed line-by-line performance statistics. This tool is ideal for developers looking to identify bottlenecks and optimize their Python scripts efficiently.

Features

  • Automatic Profiling: Adds @profile decorators to your code without manual intervention.
  • Line-by-Line Analysis: Leverages line_profiler to provide granular performance metrics for each line of code.
  • Easy Integration: Works with any Python script via a simple command-line interface.
  • Lightweight: Minimal dependencies and straightforward setup.
  • Open Source: Freely available for contributions and customization.

Installation

From PyPI

Install the latest stable version of lprof_ext using pip:

pip3 install --no-cache-dir lprof_ext

Prerequisites

  • Python 3.8 or higher
  • line_profiler (automatically installed as a dependency)

Usage

To profile a Python script, run the following command, replacing <entrypoint.py> with the path to your script:

# [Important] - You must run at project root for *.py file scanning.
lprof_ext <entrypoint.py>

Example

Suppose you have a script named example.py:

def compute_fibonacci(n):
    if n <= 1:
        return n
    return compute_fibonacci(n-1) + compute_fibonacci(n-2)

def main():
    result = compute_fibonacci(30)
    print(f"Result: {result}")

if __name__ == "__main__":
    main()

Run the profiler:

lprof_ext example.py

After execution, lprof_ext will output performance statistics into profile.json, showing execution time, number of calls, and percentage of time spent on each line of code in the profiled functions.

Output

The output will be store in JSON format. You can visualize it using our GUI tool prof_gui

docker run --rm -d --name prof_gui \
           -v ./profile.json:/profile.json \
           -p 8080:8080 \
           ruprof/prof_gui:rust

Build from Source

If you prefer to build lprof_ext from source (e.g., to use the latest development version or contribute), follow these steps:

  1. Clone the repository (replace with the actual URL):
git clone https://github.com/RuProf/lprof_ext
cd lprof_ext
  1. Set up a virtual environment
# Choose one of the following environments
# Docker env
docker build -t lprof_ext -f dockerfile .
docker run --rm -it -v $PWD:/w -w /w --entrypoint bash lprof_ext

# or venv 
python3 -m venv .venv
source .venv/bin/activate
uv sync # Install dependencies and build the package using uv
pip3 install .
  1. Verify the installation:
lprof_ext -h

Requirements for Building

  • uv (optional, for dependency management; install via pip install uv)
  • setuptools and wheel (for building the package)

Reference

lprof_ext is built on top of the excellent line_profiler library. For more details on how line-by-line profiling works, refer to the official documentation:

line_profiler

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lprof_ext-0.1.2.tar.gz (8.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lprof_ext-0.1.2-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

File details

Details for the file lprof_ext-0.1.2.tar.gz.

File metadata

  • Download URL: lprof_ext-0.1.2.tar.gz
  • Upload date:
  • Size: 8.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.14

File hashes

Hashes for lprof_ext-0.1.2.tar.gz
Algorithm Hash digest
SHA256 79bfc6bcb62159788254268f57ac60741c07e7ae9180e9eb2128b2d68247c791
MD5 1f28b45084495cdc3f3c7f7eff5af945
BLAKE2b-256 937b36160b323d47f38c3a7d4427b076e7c5188d6cc7698b53fff1084031f9b5

See more details on using hashes here.

File details

Details for the file lprof_ext-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: lprof_ext-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 9.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.14

File hashes

Hashes for lprof_ext-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e47092e57a5854847e3ec86cc8636c9fabd15d4733d538904c2f071c9ea9e9ad
MD5 ea9b9d92e8517ba20c45f0b2a0699254
BLAKE2b-256 5f7dbd319ad8b21ade7f07a21a68695a489ae630ccaa4d189f951057f9af5246

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page