Package: applipy-metrics
Applipy MetricsNote: This is a hard fork ofLightricks/PyFormanceat commitd59501eA Python port of the core portion of aJava Metrics library by Coda Hale, with
inspiration byYUNOMI - Y U NO MEASURE IT?Applipy Metrics is a toolset for performance measurement and statistics, with a
signaling mechanism that allows to issue events in cases of unexpected behaviorCore FeaturesGaugeA gauge metric is an instantaneous reading of a particular value.CounterSimple interface to increment and decrement a value. For example, this can be
used to measure the total number of jobs sent to the queue, as well as the
pending (not yet complete) number of jobs in the queue. Simply increment the
counter when an operation starts and decrement it when it completes.SummaryMeasures the statistical distribution of values in a data stream. Keeps track
of minimum, maximum, mean, standard deviation, etc. It also measures median,
75th, 90th, 95th, 98th, 99th, and 99.9th percentiles. An example use case would
be for looking at the number of daily logins for 99 percent of your days,
ignoring outliers.Regex GroupingUseful when working with APIs. A RegexRegistry allows to group API calls and
measure from a single location instead of having to define different timers in
different places.>>> from applipy_metrics.registry import RegexRegistry
>>> reg = RegexRegistry(pattern='^/api/(?P<model>)/\d+/(?P<verb>)?$')
>>> def rest_api_request(path):
...     with reg.timer(path).time():
...         # do stuff
>>> print(reg.dump_metrics())applipyThis library exposes an applipy module, if applipy is installed.The module can be imported atapplipy_metrics.MetricsModule.It binds aMetricsRegistryinstance, so that other modules can declare a
dependency and use it to create metrics.Configurations availablemetrics.clock: fully qualified name of an object with a member function
namedtime()that returns unix timestamps.  Will be used as the registry
clock, used for summaries.metrics.summary.sample_provider: fully qualified name of a function that
accepts aclockinstance and returns an instance of eitherapplipy_metrics.stats.samples.ExpDecayingSample,applipy_metrics.stats.samples.SlidingTimeWindowSampleorNone.Metrics ReportersIf you want to report your metrics using aapplipy_metrics.reporters.reporter.Reporterinstance, simply bind your
reporter provider/instance to the base reporter type. The MetricsModule will
take care of its lifecycle.ExamplesDecoratorsThe simplest and easiest way to use the Applipy Metrics library.CounterYou can use thecount_callsdecorator to count the number of times a function
is called.>>> from applipy_metrics import counter, count_calls
>>> @count_calls
... def test():
...     pass
... 
>>> for i in range(10):
...     test()
... 
>>> print(counter("test_calls").get_count())
10TimerYou can use thetime_callsdecorator to time the execution of a function and
get distribution data from it.>>> import time
>>> from applipy_metrics import summary, time_calls
>>> @time_calls
... def test():
...     time.sleep(0.1)
... 
>>> for i in range(10):
...     test()
... 
>>> print(summary("test_calls").get_snapshot().get_mean())
0.100820207596With statementYou can also use a timer using the with statementChronometer>>> import time
>>> from applipy_metrics import summary, Chronometer
>>> with Chronometer(on_stop=lambda x: summary("test").add(x)):
...    time.sleep(0.1)
>>> print(summary("test").get_snapshot().get_mean())
0.10114598274230957