Skip to main content

Performance gauging tools

Project description

lag

Performance gauging tools.

Light weight, pure-python and only builtins (no further dependencies than python itself).

To install: pip install lag

Examples

TimedContext is the base context manager of other context manager timers that add some functionality to it: CumulativeTimings, TimerAndFeedback, TimerAndCallback.

TimedContext

Starts a counter on enter and stores the elapsed time on exit.

>>> from lag import TimedContext, round_up_to_two_digits
>>> from time import sleep
>>> with TimedContext() as tc:
...     sleep(0.5)
>>> round_up_to_two_digits(tc.elapsed)

CumulativeTimings

Context manager that is meant to be used in a loop to time and accumulate both timings and relevant data.

It's a context manager, but also a list (which will contain the an accumulation of the timings the instance encountered).

>>> from lag import CumulativeTimings
>>> from time import sleep
>>>
>>> cumul_timing = CumulativeTimings()
>>>
>>> for i in range(4):
...     with cumul_timing:
...         sleep(i * 0.2)
>>>
>>>  # round_up_to_two_digits it needed here because of the variability of the system clock timing
>>> round_up_to_two_digits(cumul_timing.elapsed) == 0.6
True
>>> list(map(round_up_to_two_digits, cumul_timing))
[0.0, 0.2, 0.4, 0.6]

You can also add some data to the accumulation by calling the instance of CumulativeTimings Note: Calling cumul_timing to tell it to store some data for a loop step does have an overhead, so

>>> from lag import CumulativeTimings
>>> from time import sleep
>>> cumul_timing = CumulativeTimings()
>>> for i in range(4):
...     with cumul_timing:
...         sleep(i * 0.2)
...     cumul_timing.append_data(f"index: {i}")
>>>
>>> list(zip(map(round_up_to_two_digits, cumul_timing), cumul_timing.data_store))
[(0.0, 'index: 0'), (0.2, 'index: 1'), (0.4, 'index: 2'), (0.6, 'index: 3')]

time_multiple_calls and time_arg_combinations

These functions use CumulativeTimings to time a function call repeatedly with different inputs.

time_multiple_calls feeds collections of arguments to a function, measures how much time it takes to run, and output the timings (and possible function inputs and outputs).

>>> from lag import time_multiple_calls
>>> from time import sleep
>>> def func(i, j):
...     t = i * j
...     sleep(t)
...     return t
>>> timings, args = time_multiple_calls(func, [(0.2, 0.3), (0.5, 0.8), (0.5, 2)])
>>>
>>> list(map(round_up_to_two_digits, timings))
[0.06, 0.4, 1.0]
>>> args
[(0.2, 0.3, 0.06), (0.5, 0.8, 0.4), (0.5, 2, 1.0)]

`time_arg_combinations' uses the above to feed combinations of arguments to a function.

>>> from lag import time_arg_combinations
>>> from time import sleep
>>> def func(i, j):
...     t = i * j
...     sleep(t)
...     return t
>>> timings, args = time_arg_combinations(func, args_base=([0.1, 0.2], [2, 5]))
>>>
>>> list(map(round_up_to_two_digits, timings))
[0.2, 0.5, 0.4, 1.0]
>>> args
[(0.1, 2, 0.2), (0.1, 5, 0.5), (0.2, 2, 0.4), (0.2, 5, 1.0)]

TimerAndFeedback

Context manager that will serve as a timer, with custom feedback prints (or logging, etc.)

>>> from lag import TimerAndFeedback
>>> from time import sleep
>>> with TimerAndFeedback():
...     time.sleep(0.5)
Took 0.5 seconds
>>> with TimerAndFeedback("doing something...", "... finished doing that thing"):
...     time.sleep(0.5)
doing something...
... finished doing that thing
Took 0.5 seconds
>>> with TimerAndFeedback(verbose=False) as feedback:
...     time.sleep(1)
>>> # but you still have access to some stats through feedback object (like elapsed, started, etc.)

TimerAndCallback

Context manager that will serve as a timer, with a custom callback called on exit

The callback is usually meant to have some side effect like logging or storing information.

>>> # run some loop, accumulating timing
>>> from lag import TimerAndCallback
>>> from time import sleep
>>> cumul = list()
>>> for i in range(4):
...    with TimerAndCallback(cumul.append) as t:
...        sleep(i * 0.2)
>>> # since system timing is not precise, we'll need to round our numbers to assert them, so:
>>> # See that you can always see what the timing was in the elapsed attribute
>>> assert round_up_to_two_digits(t.elapsed) == 0.6
>>> # but the point of this demo is to show that cumul now holds all the timings
>>> assert list(map(round_up_to_two_digits, cumul)) == [0.0, 0.2, 0.4, 0.6]

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

lag-0.0.2.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

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

lag-0.0.2-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file lag-0.0.2.tar.gz.

File metadata

  • Download URL: lag-0.0.2.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.6

File hashes

Hashes for lag-0.0.2.tar.gz
Algorithm Hash digest
SHA256 a847de98f970940db402551c957d58cc49379571ce6b5a60195f200be5251102
MD5 cc4e28da929eb4a8eed6ff2ea12012e4
BLAKE2b-256 a874ec04ab1da0d7796ed28224dbb624a66a1596a415b2a21c77c8e6074b0599

See more details on using hashes here.

File details

Details for the file lag-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: lag-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.6

File hashes

Hashes for lag-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 1ce244de4818480034c8a61d38a1ec4e0c42886f9196a989baf09c3261f233f6
MD5 0a7960be757a071f1943c98cbde41f84
BLAKE2b-256 9f02bbb456f1b213d4f0067ed52572e09261fb308eb3e464279b5669647081f6

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