Skip to main content

TimeGuardian is a package for execution time measurement

Project description

TimeGuardian

TimeGuardian is a Python package designed for measuring and logging the execution time and memory usage of functions. This package is particularly useful for performance monitoring and optimization in Python applications.

Features

  • Decorators for timing functions and logging memory usage.
  • Customizable logging for execution time and memory usage with rich formatting.
  • Conditional logging based on execution time and memory usage thresholds.

Installation

Install TimeGuardian directly from pip:

pip install timeguardian

Or, install from the source code:

cd path/to/TimeGuardian
pip install .

Usage

Import TimeGuardian from the timeguardian package and use it as a decorator on your functions to measure their execution time and memory usage.

Basic Usage

from timeguardian import TimeGuardian

# To measure execution time and/or memory usage
@TimeGuardian.measure
def my_function():
    # function implementation

# Custom name logging
@TimeGuardian.measure(name="CustomName")
def another_function():
    # function implementation


# Custom logging with name, time and memory
@TimeGuardian.measure(name="CustomName", elapsed=True, memory=True) #(elapsed in ms, memory in bytes)
def another_function():
    # function implementation

Advanced Usage

Monitor performance with conditional logging:

from timeguardian import TimeGuardian

# Monitor and log only if execution time or memory usage exceeds the specified limits (time in ms, memory in bytes)
@TimeGuardian.monitor(elapsed=200, memory=1024)
def monitored_function():
    # function implementation

Setting Global Units for Time and Memory Measurement

The TimeGuardian package allows you to set global units for time and memory measurements. This feature enables you to customize how the execution time and memory usage are reported.

Usage

  1. Set the Units: Use TimeGuardian.set_time_unit(unit) and TimeGuardian.set_memory_unit(unit) to set the global units for time and memory measurement. The unit parameter should be a string indicating the desired unit (e.g., 'ms' for milliseconds, 's' for seconds, 'bytes' for bytes, 'KB' for kilobytes, 'MB' for megabytes).

  2. Decorate Functions: Apply the @TimeGuardian.measure decorator to your functions. This decorator will use the globally set units to measure and log the execution time and memory usage.

Example

from decorators import TimeGuardian

# Set the units for time and memory measurements
TimeGuardian.set_time_unit('ms')  # Set time unit to milliseconds
TimeGuardian.set_memory_unit('MB')  # Set memory unit to megabytes

@TimeGuardian.measure(elapsed=True, memory=True, name="Sample Function")
def sample_function():
    # Function implementation
    ...

# Call the decorated function
sample_function()

In this example, sample_function is measured and logged in milliseconds for time and megabytes for memory usage.

Contributing

Contributions to TimeGuardian are welcome!

License

This project is licensed under the MIT License - see the LICENSE file for details.

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

timeguardian-0.0.7.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

timeguardian-0.0.7-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file timeguardian-0.0.7.tar.gz.

File metadata

  • Download URL: timeguardian-0.0.7.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.18

File hashes

Hashes for timeguardian-0.0.7.tar.gz
Algorithm Hash digest
SHA256 09dfc439b17e63fb72b2263f57bfd040d77bdafb3f3645ee73baa94016220d8e
MD5 840cb71d29f56a1c892e0c1d8d053117
BLAKE2b-256 c4f1a93becdabc8a2f6aa3c6e8d72d53d793c79bc193f223fac2e76b726a595c

See more details on using hashes here.

File details

Details for the file timeguardian-0.0.7-py3-none-any.whl.

File metadata

File hashes

Hashes for timeguardian-0.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 e63f5963f961117a1469b4b55b40f04cd7f18ee78512ecfa8088a319c82d223f
MD5 5dd2c18d05731b680d276213a10dbc29
BLAKE2b-256 05afa002add71454529d98c01ea29c41d283d624de75f7409b4cf94ede822b7c

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