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.6.tar.gz (7.0 kB view details)

Uploaded Source

Built Distribution

timeguardian-0.0.6-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: timeguardian-0.0.6.tar.gz
  • Upload date:
  • Size: 7.0 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.6.tar.gz
Algorithm Hash digest
SHA256 19766be3a39a6c00a7567efdefca9c2febf9b62e12b8b8594f73b61e19a7a488
MD5 fe9077a392027888483bd40cd948ac65
BLAKE2b-256 34b6783b696a453f3eb389554f50438c0357c2e2a6e1521ea539c999ec21c438

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for timeguardian-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 45c7b4e49796734eb081846fee542622dbbf9a5dacb12916d3b21b3d1df6f87a
MD5 4d35120ef88720c003ac784f6ad850e6
BLAKE2b-256 05e4a86b111667525c513e0b38e12d9eba6cbfa3cca290dd082d801372df2cce

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