百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
C

chronic

> 编程语言
开源

半个分析器,半个计时器,100% 的乐趣。

399 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

半个分析器,半个计时器,100% 的乐趣。

Chronic - Python Instrumentation

Chronic is halfway between a simple timer and a profiler. Add decorators or wrap code in with statements to get the execution time. Chronic keeps track of the call hierarchy to tell you what timed blocks executed within other timed blocks. Attach an event listener to log your timings however you want.

Installation

bash
pip install chronic

Usage

To time a block of code, you can use either the @time decorator or the Timer context manager.

python
import chronic

@chronic.time
def long_running_method():
  # Do something here

with chronic.Timer('crunch_numbers'):
  # Crunch numbers here

chronic.timings

At any point, you can get the information about completed timing info from chronic.timings. This is a proxy to state held in a thread local about all timed blocks captured so far.

python
>>> import chronic
>>> chronic.timings
{
  'long_running_method': {
    'total_elapsed': 3.068,
    'average_elapsed': 3.068,
    'count': 1
  },
  'crunch_numbers': {
    'total_elapsed': 1.884,
    'average_elapsed': 1.884,
    'count': 1
  }
}

chronic.timings is a dict with keys for each timed block of code. If you use the context manager, you must specify a name. When using the decorator, you may optionally specify the name, or chronic will use theh function name as a default.

Each timing is itself a dict with the following keys:

  • total_elapsed: the elapsed execution time of the code (including all subtimings) for all runs of this block. The unit is seconds by default. If you pass in your own clock function, the unit is whatever the unit of the clock.
  • count: the number of times the timed block was run.
  • average_elapsed: the average elapsed time for each run of this block.
  • timings: a dict of all subtimings that were completed while inside this block.

So if you're using Django and want to log all timings to MongoDB, all you need to do is add middleware that looks something like this:

python
import chronic
from pymongo import MongoClient

mongo_client = MongoClient()

class ProfilingMiddleware(object):
    def process_response(self, request, response):
        mongo_client.app.timings.insert(chronic.timings)
        return response

chronic.stack

chronic.stack is a tuple of the names of all timed blocks in the call stack above the current context.

python
>>> import chronic
>>> @chronic.time
>>> def time_two():
>>>     with chronic.Timer('block2'):
>>>         print chronic.stack
>>> time_two()
('time_two', 'block2')

chronic.post_timing

Finally, you can install signal handlers to be called on the completion of all timings. Here's how you might send all timing data to statsd:

python
>>> import chronic
>>> from statsd import StatsClient
>>> statsd = StatsClient()
>>> def send_to_statsd(elapsed, timings, stack):
>>>     statsd.timing('.'.join(stack), elapsed * 1000)
>>> chronic.post_timing.connect(send_to_statsd)

Examples

…

This software is licensed under the MIT license. See the details in the file called LICENSE.

Issues· 1 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Python

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言