jztools.profiling#

Thread-safe profiling utilities to measure time, counts and rates.

Classes

Count([decay, format_string, lock])

Similar to Scalar but also accumulates updates and exposes a += operator.

ProfilerGroup(*args, **kwargs)

Rate(*args[, update_interval])

Count that also keeps track of rate.

Scalar([decay, format_string, lock])

Keeps track of the min, max, and avg updates.

Time(*args, **kwargs)

Context manager that accumulates wall time and keeps track of statistics (min,max,avg,total) of each update. Updates happen with each call to in-place adition or context manager exit.

time_and_print([msg, bang, signal_enter])

Times a function or code context and prints the time.

class jztools.profiling.Scalar(decay=0.6, format_string=None, lock=None)#

Bases: object

Keeps track of the min, max, and avg updates.

sc = Scalar(decay=1.0)
sc.update(1.0) #min, avg, max = 1.0, 1.0,  1.0
sc.update(0.5) #min, avg, max = 0.5, 0.75, 1.0
sc.update(1.5) #min, avg, max = 0.5, 1.5,  1.5
Parameters:

decay – Decay to use in weighted average.

max = -inf#

Maximum value of updates.

min = inf#

Minimum value of updates.

updates = 0#

Total number of updates.

avg = 0.0#

Running average of update values.

update(val)#

All object updates are assumed to be done through this method. Even Time calls update() in the __exit__() method when used as a context manager.

class jztools.profiling.Count(decay=0.6, format_string=None, lock=None)#

Bases: Scalar

Similar to Scalar but also accumulates updates and exposes a += operator. Values can be real numbers and not only integers. In effect, the class accumulates whatever values are passed to it.

sc = Count(decay=1.0)
# the update() method and += operator are equivalent
sc += 1.0 # min, avg, max, total = 1.0, 1.0,  1.0, 1.0
sc += 0.5 # min, avg, max, total = 0.5, 0.75, 1.0, 1.5
sc += 1.5 # min, avg, max, total = 0.5, 1.5,  1.5, 3.0
Parameters:

decay – Decay to use in weighted average.

total = 0.0#

Sum of all updates.

update(val)#

All object updates are assumed to be done through this method. Even Time calls update() in the __exit__() method when used as a context manager.

class jztools.profiling.time_and_print(msg=None, bang='**********', signal_enter=False)#

Bases: AbstractContextManager

Times a function or code context and prints the time.

As context manager: with time_and_print():

As decorator: @time_and_print() def function(): pass

__call__(function)#

Call self as a function.

class jztools.profiling.Time(*args, **kwargs)#

Bases: Count, AbstractContextManager

Context manager that accumulates wall time and keeps track of statistics (min,max,avg,total) of each update. Updates happen with each call to in-place adition or context manager exit.

Attempting to enter an already-entered context manager will raise an error.

t = Time()

# First update.
with t:
    sleep(1) # t.elapsed == 1

# No update
sleep(1) # t.elapsed == 1

# Second update.
with t:
    sleep(1) # t.elapsed == 2


# Third update.
t += 2 # t.elapsed == 4


#
with t:
    with t: # Raises an error.
        pass
Parameters:

decay – Decay to use in weighted average.

property elapsed#

Similar to self.total, except that in can be called inside a context and will include partial context time. Outside a context, same as total.

class jztools.profiling.Rate(*args, update_interval=1.0, **kwargs)#

Bases: Scalar, AbstractContextManager

Count that also keeps track of rate. Counts can be real numbers and not just integers. Rates are computed every “update_interval” seconds using a spawned thread and tracked using a running average with decay (as any Scalar()).

#
r = Rate()
with r:
    time.sleep(2.0)
    r+=3
    # r.total ~= 3/2

Besides the arguments of Scalar, also accepts

Parameters:

update_interval – Determines how often the spawned thread will update rate.

update(k)#

All object updates are assumed to be done through this method. Even Time calls update() in the __exit__() method when used as a context manager.