Skip to main content

Collection of metrics collection tools, including a simple stopwatch

Project description

statman

Python package PyPI version Known Vulnerabilities

Overview

Statman is a collection of metric collectors to embed within your python application. It includes a registry to easily access your metrics.

Statman => registry Metric => set of classes that can perform metric collection Stopwatch => a metric class responsible for tracking time delta

Install it!

Statman is availble from pypi.

It can be manually installed by:

pip install statman

or by adding the following to your requirements.txt:

statman=*

Use it

Statman (Registry)

Statman offers a registery to make it easily to globally access metrics. Perhaps you will create and register a stopwatch in the depths of your codebase to measure the time to write to a database, and then want to access that result in some other part of your application.

Register

  • register(name, metric) => manually register a new metric

Get

  • get(name) => get a metric by name

Count

  • count() => returns a count of the registered metrics.

Reset

  • reset() => clears all metrics from the registry.

Specialized register / get

  • stopwatch(name) => returns a stopwatch instance. If there is a registered stopwatch with this name, return it. If there is no registered stopwatch with this name, create a new instance, register it, and return it.

Stopwatch

Statman-Stopwatch is for timing operations within your system. Suppose that you are trying to track down where the system is slow. Put a stopwatch around certain critical areas, time those operations, and compare.

Constructor

  • Stopwatch(name=None, autostart=False, initial_delta=None) => create an instance of a stopwatch.
    • If autostart set to true, the stopwatch will automatically start
    • If initial_delta is set to a value, and read of the stopwatch is incremented by this amount. This can be helpful if you adding timings together.
    • name is used for to string / reporting for identification of this metric. Defaults to blank
    • If enable_history is set to true, when a timing is collected (stop invoked), an event is collected. This can be accessed by the history property to examing statistics on this stopwatch

Start

  • start() => starts the stopwatch, let the timing begin!

Read

  • read(units, precision) => reads the stopwatch to determine how much time has elapsed. Returns the time elapsed in seconds.
    • The elapsed time will be returned based upon the units ('m' minutes, 's' seconds, 'ms', milliseconds). Defaults to seconds.
    • If precision is provided, read() will round to the number of decimals places based on precision.
    • Note: read does NOT stop the stopwatch - if the stopwatch is runnning, it will continues to run.
  • time(units, precision) => alias for read()

Stop

  • stop(units, precision) => stops the stopwatch, and returns the time elapsed in seconds
    • See read for the role of units and precision

Reset

  • reset() => restores the stopwatch back to init state and clears start and stop times

Restart

  • restart() => resets the stopwatch, then starts it

History

  • history => if enable_history set during stopwatch construction, the history property returns an instance of a history object, which can be used for examing statistics

Examples

Maually Register Metric

from statman import Statman
Statman.register('expensive-operation-timing',Stopwatch())

stopwatch = Statman.get('expensive-operation-timing')

Stopwatch via Registry

from statman import Statman

Statman.stopwatch('stopwatch-name').start()
# do some expensive operation that you want to measure
Statman.stopwatch('stopwatch-name').read()

print(f'event took {Statman.stopwatch('stopwatch-name').read(precision=1)}s to execute')  # event took 1.0s to execute

Stopwatch: Direct Usage (no registry)

from statman import Stopwatch
sw = Stopwatch()
sw.start()

# do some expensive operation that you want to measure

delta = sw.stop()
print(f'event took {sw.read(precision=1)}s to execute')  # event took 1.0s to execute

Stopwatch: History

from statman import Stopwatch
number_of_events = 1000000

sw = Stopwatch(enable_history=True)
for i in range(0, number_of_events):
    sw.start()
    # do some expensive operation that you want to measure
    sw.stop()

print('number of measurements:', sw.history.count())
print('min:', sw.history.min_value())
print('max:', sw.history.max_value())
print('ave:', sw.history.average_value())
print('mode:', sw.history.mode_value())

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

statman-1.0.0.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

statman-1.0.0-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file statman-1.0.0.tar.gz.

File metadata

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

File hashes

Hashes for statman-1.0.0.tar.gz
Algorithm Hash digest
SHA256 235db94b778636daee85ff70cdcdee0898e5015d59e20cefb961e16de5826c85
MD5 9c14b68d0dc8e8d857a472671aa780d4
BLAKE2b-256 41c84fbe72c5cda7d98b4a3bd396b28486ac9111cdb14d312cfb57882f996f12

See more details on using hashes here.

File details

Details for the file statman-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: statman-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.18

File hashes

Hashes for statman-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0912ba8e556d3072a9ba70c62a23f11346c79532c363be714f9d0577b9ad1e94
MD5 186dfeab887f64b4b749b0ee2af408a9
BLAKE2b-256 767b3baa06c3975ca7c1436f1a020c54ee202d599e9c9cd7287d238f9ae18c5a

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