Stats Components#

The core stats component is designed to allow for tracking of statistics withing the libraries and across a crawler implementation. Examples of its use can be seen in the various nft commands.

StatsService#

StatsService is a memory implementation of a stats service. It can process incrementing a stat, recording individual timings, or summarizing all timings as a single value. Timing methods are synchronous context managers for ease of implementation.

Complete Simple Example#

from time import sleep

from blockcrawler.core.stats import StatsService


def run():
    stats_service = StatsService()

    with stats_service.timer("timer"):
        sleep(0.1)
        stats_service.increment("counter")

    with stats_service.ms_counter("ms_counter"):
        sleep(0.1)
        stats_service.increment("counter")

    print("timer:", stats_service.get_timings("timer"))
    print("ms_counter:", stats_service.get_count("ms_counter"))
    print("counter:", stats_service.get_count("counter"))


if __name__ == "__main__":
    run()

Executing the above example will result in something similar to following being displayed in the console:

timer: [103774451]
ms_counter: 101
counter: 2

Download the example and try it yourself.