GithubHelp home page GithubHelp logo

stopwatch's Introduction

stopwatch Build Status

Scoped, nested, aggregated python timing library

StopWatch operates on a notion of "spans" which represent scopes of code for which we want to measure timing. Spans can be nested and placed inside loops for aggregation.

For example:

with sw.timer('root'):
    for i in range(50):
        with sw.timer('inner_task'):
            do_inner_task(i)

StopWatch requires a root scope which upon completion signifies the end of the round of measurements. On a server, you might use a single request as your root scope.

StopWatch produces two kinds of reports.

  1. Aggregated (see _reported_values).
  2. Non-aggregated or "tracing" (see _reported_traces)

Aggregated reports have one value for each timer name. The above example would have 2 reports.

Tracing reports keep one trace every time we enter a scope. The above example would have 51 reports.

By default, StopWatch dumps aggregated report to stdout after the root scope completes and resets internal state.

Installation

pip install dbx-stopwatch

Usage

Basic Usage

import stopwatch

sw = stopwatch.StopWatch()
with sw.timer('root'):
    for i in range(50):
        with sw.timer('inner_task'):
            time.sleep(0.1)
    with sw.timer('outer_task'):
        time.sleep(1.0)
print stopwatch.format_report(sw.get_last_aggregated_report())

yields

************************
*** StopWatch Report ***
************************
root                    6206.833 (100%)
                    inner_task             50  5200.742 (84%)
                    outer_task              1  1002.351 (16%)
Tags:

Multiple reports

sw = stopwatch.StopWatch()

while request_in_queue():
    r = get_request()
    with sw.timer('request'):
        process(request)

Tag your reports

while request_in_queue():
    r = get_request()
    with sw.timer('request'):
        sw.addtag(r.get_endpoint_name())
        sw.addslowtag('500ms', 0.500)  # Tag only if request takes >= 500ms
        process(request)

Report to your own backend

sw = stopwatch.StopWatch(
    export_aggregated_timers_func=my_export_aggregated_timers,
    export_tracing_func=my_export_tracing_func,
)

Contributing

Contributions are welcome. Tests can be run with tox. Lint with flake8 You'll have to agree to Dropbox's CLA.

Issues

If you encounter any problems, please file an issue along with a detailed description.

stopwatch's People

Contributors

nipunn1313 avatar twk avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.