measure-elapsed-time-with-a-monotonic-clock
Use when timing durations, timeouts or latency — read a monotonic clock that never steps backwards, not wall time, so DST and NTP cannot corrupt the measurement.
- License 许可
- MIT
- 最近更新
- 2026-11-11
- Stars
- ★ 80
- Forks
- 17
01 前言 Front Matter
- name
- measure-elapsed-time-with-a-monotonic-clock
- description
- Use when timing durations, timeouts or latency — read a monotonic clock that never steps backwards, not wall time, so DST and NTP cannot corrupt the measurement.
02 资源文件 3
- 许可证LICENSE1.0 KB
- 参考文档SOURCE.md369 B
- 参考文档TRUST.auto.yaml2.2 KB
03 SKILL.md 正文
Measure elapsed time with a monotonic clock
The wall clock is a moving target: NTP can step it backwards, DST relabels it, and a VM suspend jumps it. Any duration — a timeout, a latency histogram, a stopwatch — must come from a monotonic source that only ever advances.
Procedure
- Pick the monotonic API for the language, not
now():
Python time.monotonic_ns() / perf_counter()
Golang time.Since(start) (uses monotonic component)
Java System.nanoTime()
Node process.hrtime.bigint()
C clock_gettime(CLOCK_MONOTONIC_RAW, &ts)
- Time the interval with monotonic, report the instant with wall time:
import time
from datetime import datetime, timezone
start_mono = time.monotonic()
start_wall = datetime.now(timezone.utc)
do_work()
elapsed = time.monotonic() - start_mono
finished = datetime.now(timezone.utc)
print(f"elapsed={elapsed:.3f}s started={start_wall.isoformat()} finished={finished.isoformat()}")
- Use the monotonic delta for timeouts and SLO math; use the wall instants for logs and correlation.
- On Linux, confirm which clock you are reading —
CLOCK_REALTIMEis wall,CLOCK_MONOTONICexcludes suspend time,CLOCK_BOOTTIMEincludes it:
cat /proc/uptime # monotonic-ish seconds since boot
- In Go, keep the monotonic reading rather than stripping it:
t.Round(0)or a JSON/serialise round trip drops it, after whichSubsilently uses wall time. - In a histogram, bucket by the monotonic delta; using wall time makes a step appear as a negative or huge bucket.
- Test the property: monkeypatch the wall clock backwards and assert the measured duration stays positive.
Pitfalls
time.time()differences go negative when NTP steps the clock back; a timeout computed asnow - start > limitthen never fires.CLOCK_MONOTONICexcludes suspend, so a laptop or VM that slept shows a shorter interval than reality; useCLOCK_BOOTTIMEwhen suspend must count.- Serialising a Go
time.Timethrough JSON strips the monotonic component; the same value measures differently after a round trip. process.hrtime()in Node is monotonic but not epoch-based; adding it toDate.now()mixes two clocks.- A monotonic clock has an arbitrary epoch — never log it as a timestamp, only as a delta.
Verification
python3 -c "import time; a=time.monotonic(); b=time.monotonic(); print('mono delta ok', b>=a)"
Monotonic readings never decrease, and a negative-step test on the wall clock leaves the monotonic measurement positive. Report: "durations measured with monotonic_ns; wall clock used only for log instants; timeout test steps wall clock back and still fires."
04 同仓库更多 Skills
recipe-performance
Diagnose a specific MetaMask user flow's real-app performance and Sentry critical-user-flow telemetry with deterministic mm-harness recipes. Use for CUF boundaries, cold/warm/background-resume behavior, latency regressions, lifecycle tags, duplicate or blocking network requests, long tasks, layout/paint cost, screen redraws, React rerenders, or browser trace analysis where tests alone cannot prove the real app.
mexc-spot-websocket
MEXC Spot WebSocket workflows for public streams, protobuf handling, depth maintenance, listen keys, and private streams without exposing API keys.
mexc-spot-rest
MEXC Spot REST workflows for public data, signed account requests, orders, wallet/admin actions, and secure signing without exposing API keys.
mexc-futures-websocket
MEXC Futures WebSocket workflows for public streams, private account/order/position streams, ping/pong, subscriptions, and depth maintenance.
0xklen-version-and-refresh-sanctions-lists
暂无简介
verify-tests-cover-changed-lines
Use when a PR adds code that the existing suite may not exercise. Measures patch coverage — the share of newly changed lines hit by tests — before approving.