k3stopwatch
Hierarchical timing measurement with aggregated and tracing reports.
StopWatch operates on "spans" representing code scopes for timing measurement. Spans can be nested and placed inside loops for aggregation.
k3stopwatch is a component of pykit3 project: a python3 toolkit set.
Installation
pip install k3stopwatch
Quick Start
import k3stopwatch
sw = k3stopwatch.StopWatch()
with sw.timer('root'):
with sw.timer('task_a'):
# do some work
pass
for i in range(10):
with sw.timer('loop_iteration'):
# timed loop
pass
# Get timing reports
print(k3stopwatch.format_report(sw.get_last_aggregated_report()))
API Reference
k3stopwatch
StopWatch - library for adding timers and tags in your code for performance monitoring https://github.com/dropbox/stopwatch
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. Great for averaging across various root scopes / requests. 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. This is great for digging into a data for a particular request, or constructing waterfall graphs.
StopWatch
Bases: object
StopWatch - main class for storing timer stack and exposing timer functions/contextmanagers to the rest of the code
Source code in k3stopwatch.py
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 | |
__init__(strict_assert=True, export_tracing_func=None, export_aggregated_timers_func=None, max_tracing_spans_for_path=1000, min_tracing_milliseconds=3, time_func=None)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
strict_assert
|
If True, assert on callsite misuse |
True
|
|
export_tracing_func
|
Function to log tracing data when stack empties |
None
|
|
export_aggregated_timers_func
|
Function to log timers when stack empties |
None
|
|
max_tracing_spans_for_path
|
The maximum number of spans to be logged per root scope for each unique path, so we make sure we aren't too excessive on the non-aggregated tracing report |
1000
|
|
min_tracing_milliseconds
|
To reduce the large number of trivial spans, don't trace anything that takes less then this amount of time. |
3
|
|
time_func
|
Function which returns the current time in seconds. Defaults to time.time |
None
|
Source code in k3stopwatch.py
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | |
add_annotation(key, value='1', event_time=None)
Add an annotation to the root scope. Note that we don't do this directly in order to support this case:
if x > 1000: sw.add_annotation('big_work') with sw.timer('root'): do_work(x)
Source code in k3stopwatch.py
304 305 306 307 308 309 310 311 312 313 314 315 316 | |
add_slow_annotation(tag, timelimit)
add annotation that will only be used if root scope takes longer than timelimit amount of seconds Arguments: tag: String tag name for the slowtag timelimit: Lower bound for the root scope after which tag is applied
Source code in k3stopwatch.py
324 325 326 327 328 329 330 331 | |
add_span_annotation(key, value='1', event_time=None)
Add an annotation to the current scope
Source code in k3stopwatch.py
318 319 320 321 322 | |
cancel(name)
Cancels a stopwatch span (must match latest started span). This span will not show up in any reports. Arguments: name: Name of the scope that's being cancelled. Must match the latest start().
Source code in k3stopwatch.py
294 295 296 297 298 299 300 301 302 | |
end(name, end_time=None, bucket=None)
End a stopwatch span (must match latest started span) Arguments: name: Name of the scope that's completed. Must match the latest start() end_time: Time (s) at which the scope completed if set (if not use the current time) bucket: optional enum.Enum value describing bucket for additional reporting. For example, you might bucket all database queries together to see overall how much time is spent in databases.
Source code in k3stopwatch.py
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 | |
format_last_report()
Return formatted report from the last aggregated report. Simply calls format_last_report() on the get_last_aggregated_report()
Source code in k3stopwatch.py
341 342 343 344 | |
get_last_aggregated_report()
Returns the last aggregated report and tags as a 2-tuple
Source code in k3stopwatch.py
337 338 339 | |
get_last_trace_report()
Returns the last trace report from when the last root_scope completed
Source code in k3stopwatch.py
333 334 335 | |
sampling_timer(name, p, *n, **kwargs)
Context manager that will time the context with probability p.
Source code in k3stopwatch.py
194 195 196 197 198 199 200 201 | |
start(name, start_time=None)
Begin a stopwatch span Arguments: name: Name of the span to start start_time: Time (s) at which the scope began if set. (if not, use the current time)
Source code in k3stopwatch.py
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 | |
timer(name, bucket=None, start_time=None, end_time=None)
Context manager to wrap a stopwatch span
Source code in k3stopwatch.py
203 204 205 206 207 208 209 210 211 212 213 214 215 216 | |
TimerData
Bases: object
Simple object that wraps all data needed for a single timer span. The StopWatch object maintains a stack of these timers.
Source code in k3stopwatch.py
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | |
default_export_aggregated_timers(aggregated_report)
Default implementation of aggregated timer logging
Source code in k3stopwatch.py
127 128 129 | |
default_export_tracing(reported_traces)
Default implementation of non-aggregated trace logging
Source code in k3stopwatch.py
122 123 124 | |
format_report(aggregated_report)
returns a pretty printed string of reported values
Source code in k3stopwatch.py
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | |
License
The MIT License (MIT) - Copyright (c) 2015 Zhang Yanpo (张炎泼)