Skip to main content

A Python package for profiling and measuring function performance.

Project description

PySniffer

pypi Release Release

Overview

PySniffer is a powerful and easy-to-use Python package designed to profile and measure the performance of functions. This tool is invaluable for developers who need to optimize their code by gaining insights into execution time, CPU time, and peak memory usage. The decorator automatically logs detailed profiling data, allowing for easy analysis and debugging.

Features

  • Execution Time Measurement: Captures the total wall-clock time taken for the function to execute.
  • CPU Time Measurement: Records the CPU time consumed during the function execution, identifying CPU-bound operations.
  • Memory Usage Profiling: Tracks and logs the peak memory usage, providing insights into the memory efficiency of the function.
  • Profiling Statistics: Utilizes the cProfile module to gather detailed statistics on function calls, sorted by various criteria such as cumulative time.
  • Customizable Logging:
    • Log Level: Set the verbosity of the logs with different logging levels (DEBUG, INFO, WARNING, ERROR, CRITICAL).
    • Log File: Specify the log file path to store the profiling data.
    • Append or Overwrite: Choose whether to append to the existing log file or overwrite it with new data.
  • Flexible Output: Logs can be directed to both the console and a log file, providing flexibility in how the profiling information is consumed and stored.

Installation

You can install PySniffer from PyPI using pip:

pip install pysniffer

Usage

To use the benchmarking decorator, simply apply it to any function you want to profile. Customize the behavior using the decorator's parameters to suit your specific needs.

Quick Usage

from pysniffer import benchmark

@benchmark()
def example_function():
    # Function implementation
    pass

example_function()

Full Usage Example

For more customization, you can specify parameters as follows:

import logging
from pysniffer import benchmark

@benchmark(sort_by=pstats.SortKey.TIME, lines_to_print=20, log_level=logging.DEBUG, log_file="detailed_profile.log", append_log=False)
def example_function():
    # Function implementation
    pass

example_function()

Parameters

  • sort_by (pstats.SortKey): Sorting key for profiling statistics. Default is pstats.SortKey.CUMULATIVE.
  • lines_to_print (int): Number of lines of profiling statistics to print. Default is 10.
  • log_level (logging.LEVEL): Logging level for profiling data. Default is logging.INFO.
  • log_file (str): Path to the log file for profiling data. Default is "profile.log".
  • append_log (bool): Whether to append profiling data to the log file or overwrite it. Default is True.

Benefits

  • Performance Optimization: Identify bottlenecks and optimize the performance of your functions.
  • Resource Management: Monitor and manage memory usage effectively.
  • Detailed Insights: Gain in-depth understanding of function behavior through detailed profiling statistics.
  • Ease of Use: Simple integration into existing code with customizable parameters for tailored profiling.

License

PySniffer is licensed under the MIT License. See the LICENSE file for more details.

Contributing

Contributions are welcome! Please fork the repository and submit a pull request with your changes.

Acknowledgements

This project leverages Python's cProfile, pstats, and tracemalloc modules for profiling and memory tracking.

Contact

For any questions or suggestions, please feel free to open an issue on the GitHub repository or contact the project maintainers.


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

pysniffer-0.1.6.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

pysniffer-0.1.6-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file pysniffer-0.1.6.tar.gz.

File metadata

  • Download URL: pysniffer-0.1.6.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.9

File hashes

Hashes for pysniffer-0.1.6.tar.gz
Algorithm Hash digest
SHA256 958c223e14fcce0f8d32af679938f4ace29b7df738f9bfc1d5adfb676aab321f
MD5 fda6802b9a8fc8a3b12a2f5e19502fe0
BLAKE2b-256 73efbbcb22e787c2a7103071247b26cf39fd3b9e93b729b95e9b1a2a18d64752

See more details on using hashes here.

File details

Details for the file pysniffer-0.1.6-py3-none-any.whl.

File metadata

  • Download URL: pysniffer-0.1.6-py3-none-any.whl
  • Upload date:
  • Size: 6.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.9

File hashes

Hashes for pysniffer-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 178c9731b6191900c6fda03dcf90bbee46c8234af3d5525463daaebbf11e9650
MD5 8d3fa32aab7da164f02d46ce6ac8af36
BLAKE2b-256 d7e7282ca45b8f95a77d0910589b98930dbb615e747bcd0d85b352e8b0f93f96

See more details on using hashes here.

Supported by

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