Skip to main content

Simplify logging of timings of selected parts of an application.

Project description

Simplify logging of timings of selected parts of an application.

package version from PyPI build status from GitHub test coverage from Codecov grade from Codacy license

How to use

Recommended initialization is as follows.

import timing

_TIME = timing.get_timing_group(__name__)  # type: timing.TimingGroup

This follows the conventions of logging module.

import logging

_LOG = logging.getLogger(__name__)

Any name can be used instead of __name__. However, if names of format module.sub.sub_sub are used, this will create a timing hierarchy where each timing data is stored in its proper location and can be queried easier.

The resulting _TIME object is used to create individual timers, and will handle storing results in cache, which later can be used to obtain timing statistics.

You can obtain the timer object directly via start(name) method. You’ll need to manually call stop() in this case.

timer = _TIME.start('spam')  # type: timing.Timing
spam()
more_spam()
timer.stop()

You can also obtain the timer object indirectly via measure(name) context manager. The context manager will take care of calling stop() at the end.

with _TIME.measure('ham') as timer:  # type: timing.Timing
    ham()
    more_ham()

And if you want to time many repetitions of the same action (e.g. for statistical significance) you can use measure_many(name[, samples][, threshold]) generator.

You can decide how many times you want to measure via samples parameter and how many seconds at most you want to spend on measurements via threshold parameter

for timer in _TIME.measure_many('eggs', samples=1000):  # type: timing.Timing
    eggs()
    more_eggs()

for timer in _TIME.measure_many('bacon', threshold=0.5):  # type: timing.Timing
    bacon()
    more_bacon()

for timer in _TIME.measure_many('tomatoes', samples=500, threshold=0.5):  # type: timing.Timing
    tomatoes()
    more_tomatoes()

Also, you can use measure and measure(name) as decorator. In this scenario you cannot access the timings directly, but the results will be stored in the timing group object, as well as in the global cache unless you configure the timing to not use the cache.

import timing

_TIME = timing.get_timing_group(__name__)

@_TIME.measure
def recipe():
    ham()
    eggs()
    bacon()

@_TIME.measure('the_best_recipe')
def bad_recipe():
    spam()
    spam()
    spam()

Then, after calling each function the results can be accessed through summary property.

recipe()
bad_recipe()
bad_recipe()

assert _TIME.summary['recipe']['samples'] == 1
assert _TIME.summary['the_best_recipe']['samples'] == 2

The summary property is dynamically computed on first access. Subsequent accesses will not recompute the values, so if you need to access the updated results, call the summarize() method.

recipe()
assert _TIME.summary['recipe']['samples'] == 1

bad_recipe()
bad_recipe()
assert _TIME.summary['the_best_recipe']['samples'] == 2  # will fail
_TIME.summarize()
assert _TIME.summary['the_best_recipe']['samples'] == 2  # ok

Further API and documentation are in development.

See these examples in action in examples.ipynb notebook.

Requirements

Python version 3.11 or later.

Python libraries as specified in requirements.txt.

Building and running tests additionally requires packages listed in test_requirements.txt.

Tested on Linux and OS X.

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

timing-0.5.0.tar.gz (22.4 kB view details)

Uploaded Source

Built Distribution

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

timing-0.5.0-py3-none-any.whl (13.5 kB view details)

Uploaded Python 3

File details

Details for the file timing-0.5.0.tar.gz.

File metadata

  • Download URL: timing-0.5.0.tar.gz
  • Upload date:
  • Size: 22.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.2

File hashes

Hashes for timing-0.5.0.tar.gz
Algorithm Hash digest
SHA256 aa5045323b28f3a79a8806b5f0c9bf0a28130290f6de5b6bb4b88a0430aeaf1d
MD5 1096c91faf9276aacaae1bcd261da137
BLAKE2b-256 d40e913c9d6d36ac9e0dfc9330cdc2fe7d81e37ad424a5e358eb3080661283f6

See more details on using hashes here.

File details

Details for the file timing-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: timing-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 13.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.2

File hashes

Hashes for timing-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9b650ccb9698e6a6200b029827fe6721cd95481179f40de2d0b32202bfe06bc3
MD5 5ed5e0a06ba66cb68e9c60e0c49c0e38
BLAKE2b-256 11e19ee287923bff109ffd37269897d2479e4463fc89c667bd4ded20c4402cd1

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