Salta ai contenuti

Comandi speedscope

speedscope è un visualizzatore rapido, interattivo e basato sul web per profili di prestazioni. Supporta l’importazione di dati da molti profiler e fornisce tre modalità di visualizzazione: ordine temporale, left heavy (grafico a fiamma tradizionale) e vista sandwich per l’analisi delle singole funzioni.

Installazione

Linux/Ubuntu

# Install via npm (globally)
npm install -g speedscope

# Or use npx without installing
npx speedscope profile.json

# Verify installation
speedscope --version

# speedscope runs locally in your browser — no data is sent to any server

Web-Based (No Install)

# Open https://speedscope.app in your browser
# Drag and drop a profile file onto the page
# Or use the file picker to select a profile

# All processing happens client-side in the browser

Utilizzo CLI

# Open a profile in the default browser
speedscope profile.json

# Open a specific file format
speedscope perf.data.json
speedscope profile.pb.gz
speedscope chrome_trace.json

# Pipe data directly from a profiler
py-spy record --format speedscope -o - -- python app.py | speedscope -

# Open multiple profiles for comparison
speedscope before.json after.json

Formati supportati

FormatSource ToolFile Extension
speedscope JSONNative format.speedscope.json
pprofGo pprof, py-spy.pb.gz, .pb
Chrome TraceChrome DevTools, Node.js.json
Firefox ProfileFirefox Profiler.json
Brendan Gregg foldedFlameGraph stackcollapse.folded, .txt
callgrindValgrind, cachegrindcallgrind.out.*
InstrumentsXcode Instruments.trace
Linux perfperf script.perf, .txt
DTraceDTrace collapsed stacks.txt
Haskell profGHC profiler.prof
Safari TimelineSafari Web Inspector.json

Generazione profili per speedscope

From py-spy (Python)

# Generate speedscope-compatible output
py-spy record --format speedscope -o profile.speedscope.json -- python my_script.py

# From a running process
sudo py-spy record --format speedscope -o profile.speedscope.json --pid 1234

From perf (Linux)

# Record with perf
perf record -F 99 -g -- ./my_program
perf script > out.perf

# Convert to folded stacks (speedscope reads these)
stackcollapse-perf.pl out.perf > out.folded

# Open in speedscope
speedscope out.folded

From Go pprof

# Generate a CPU profile
go tool pprof -proto http://localhost:6060/debug/pprof/profile?seconds=30 > cpu.pb.gz

# Open in speedscope
speedscope cpu.pb.gz

# Or generate from a Go test
go test -cpuprofile cpu.pb.gz -bench .
speedscope cpu.pb.gz

From Chrome DevTools

# 1. Open Chrome DevTools > Performance tab
# 2. Click Record, reproduce the issue, click Stop
# 3. Click the down arrow (Export) to save as JSON
# 4. Open the JSON file in speedscope
speedscope chrome_profile.json

From Node.js

# Generate a V8 CPU profile
node --cpu-prof --cpu-prof-dir=./profiles my_app.js

# Open the generated .cpuprofile file
speedscope profiles/*.cpuprofile

# Using clinic.js flame
npx clinic flame -- node my_app.js
# The output HTML contains data that can be exported

From Ruby (stackprof)

# Generate a stackprof profile
# In Ruby code:
# StackProf.run(mode: :cpu, out: 'stackprof.dump') { do_work }

# Convert to speedscope format
stackprof --flamegraph stackprof.dump > flamegraph.json
speedscope flamegraph.json

Modalità di visualizzazione

Time Order View

# Shows events in chronological order (left to right = time)
# - X-axis represents wall-clock time
# - Y-axis represents call stack depth
# - Useful for understanding execution flow and timing
# - Shows when specific functions were called
# - Helps identify phases and patterns in execution
# - Navigate: scroll horizontally to move through time

Left Heavy View

# Traditional flame graph (aggregated by call path)
# - Identical stack frames are merged and sorted by weight
# - X-axis represents total time (wider = more time)
# - Y-axis represents call stack depth
# - Best for finding hotspots and optimization targets
# - Similar to Brendan Gregg flame graphs
# - Navigate: click to zoom into a subtree

Sandwich View

# Focus on a single function showing callers and callees
# - Select any function to see its full context
# - Top section: who called this function (callers)
# - Bottom section: what this function calls (callees)
# - Center: the selected function
# - Great for understanding a specific function's role
# - Navigate: click function names to re-center

Scorciatoie da tastiera

ShortcutAction
1Switch to Time Order view
2Switch to Left Heavy view
3Switch to Sandwich view
f or /Open search
EnterConfirm search
EscapeClose search or reset zoom
rReset view / zoom
nNext search match
Shift+nPrevious search match
+ / =Zoom in
-Zoom out
0Fit to window
h / Left ArrowPan left
l / Right ArrowPan right
Mouse wheelZoom in/out at cursor

speedscope JSON Format

{
  "$schema": "https://www.speedscope.app/file-format-schema.json",
  "shared": {
    "frames": [
      {"name": "main", "file": "app.py", "line": 10},
      {"name": "process_data", "file": "app.py", "line": 25},
      {"name": "transform", "file": "utils.py", "line": 42}
    ]
  },
  "profiles": [
    {
      "type": "sampled",
      "name": "CPU Profile",
      "unit": "milliseconds",
      "startValue": 0,
      "endValue": 5000,
      "samples": [[0, 1, 2], [0, 1], [0, 1, 2]],
      "weights": [10, 10, 10]
    }
  ]
}

Suggerimenti e workflow

# Quick A/B comparison workflow
# 1. Profile before optimization
py-spy record --format speedscope -o before.json -- python my_script.py

# 2. Make code changes, then profile again
py-spy record --format speedscope -o after.json -- python my_script.py

# 3. Open both in speedscope tabs for comparison
speedscope before.json &
speedscope after.json &

# Converting between formats
# Folded stacks to speedscope: just open directly
speedscope my_folded_stacks.txt

# Multiple profiles in one file
# speedscope JSON supports multiple profiles in the "profiles" array
# Each appears as a separate tab in the viewer

# Self-hosting speedscope
# Clone and serve the static files
git clone https://github.com/jlfwong/speedscope.git
cd speedscope && python3 -m http.server 8080
# Open http://localhost:8080

Riferimento rapido

FeatureDescription
Time OrderChronological execution view
Left HeavyAggregated flame graph view
SandwichCaller/callee focus view
SearchHighlight matching frames
ZoomClick frames or use scroll wheel
ExportSave current view as PNG
Drag & DropLoad files by dropping onto the page
PrivacyAll processing is client-side