Scripts2026年7月3日·1 分钟阅读

VizTracer — Trace and Visualize Python Code Execution

A low-overhead Python tracer that records every function call and renders an interactive timeline for debugging and performance analysis.

Agent 就绪

Agent 可直接安装

这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
VizTracer
直接安装命令
npx -y tokrepo@latest install 5f928744-76db-11f1-9bc6-00163e2b0d79 --target codex

先 dry-run 确认安装计划,再运行此命令。

Introduction

VizTracer records every function entry and exit in a Python program and produces an interactive Chrome-style trace timeline. It helps developers understand execution flow, find performance bottlenecks, and debug complex concurrency issues by visualizing exactly what happens and when.

What VizTracer Does

  • Records every function call with timestamps and duration
  • Generates interactive flame charts viewable in the browser
  • Supports multi-process, multi-thread, and async tracing
  • Allows custom events and instant markers for domain-specific tracing
  • Filters by function name, module, or file to reduce noise

Architecture Overview

VizTracer hooks into Python's sys.setprofile mechanism using a C extension for minimal overhead. Each function entry and exit is recorded as a trace event with nanosecond timestamps. The trace data is stored in a circular buffer and exported to Chrome Trace Event Format (JSON). The built-in vizviewer launches a local web server using Perfetto UI for interactive exploration.

Self-Hosting & Configuration

  • Install with pip: pip install viztracer
  • Run directly: viztracer my_script.py generates result.json
  • View traces with vizviewer result.json or upload to ui.perfetto.dev
  • Filter tracing with --include_files or --exclude_files patterns
  • Set trace buffer size with --tracer_entries for long-running programs

Key Features

  • Sub-microsecond overhead per function call via C extension
  • Chrome Trace Event Format output compatible with Perfetto and Chrome DevTools
  • Multi-process tracing with automatic subprocess follow
  • Custom event logging with VizTracer.log_instant() and VizTracer.log_event()
  • Flamegraph generation alongside timeline view

Comparison with Similar Tools

  • cProfile — Function-level aggregate stats; VizTracer shows a full execution timeline
  • Pyinstrument — Statistical sampling with call trees; VizTracer records every call
  • py-spy — Attaches to running processes; VizTracer instruments from the start
  • Scalene — Focuses on CPU/memory hotspots; VizTracer focuses on execution flow visualization

FAQ

Q: How much overhead does VizTracer add? A: Typically 2-5x slowdown, which is low for a tracing profiler that records every function call.

Q: Can I trace multiprocess applications? A: Yes. Use --log_multiprocess to automatically trace child processes.

Q: What viewer should I use? A: The built-in vizviewer uses Perfetto UI. You can also open trace files in Chrome DevTools.

Q: Can I add custom trace events? A: Yes. Use the log_instant and log_event APIs to add markers and spans to the timeline.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产