gh-140677 Add heatmap visualization to Tachyon sampling profiler (#140680)

Co-authored-by: Ivona Stojanovic <stojanovic.i@hotmail.com>
This commit is contained in:
Pablo Galindo Salgado 2025-12-02 20:33:40 +00:00 committed by GitHub
parent d3c888b4ec
commit 8801c6dec7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
18 changed files with 3939 additions and 326 deletions

View file

@ -7,7 +7,8 @@
from .collector import Collector
from .pstats_collector import PstatsCollector
from .stack_collector import CollapsedStackCollector
from .heatmap_collector import HeatmapCollector
from .gecko_collector import GeckoCollector
from .string_table import StringTable
__all__ = ("Collector", "PstatsCollector", "CollapsedStackCollector", "GeckoCollector", "StringTable")
__all__ = ("Collector", "PstatsCollector", "CollapsedStackCollector", "HeatmapCollector", "GeckoCollector", "StringTable")

View file

@ -0,0 +1,22 @@
import importlib.resources
def get_combined_css(component: str) -> str:
template_dir = importlib.resources.files(__package__)
base_css = (template_dir / "_shared_assets" / "base.css").read_text(encoding="utf-8")
if component == "flamegraph":
component_css = (
template_dir / "_flamegraph_assets" / "flamegraph.css"
).read_text(encoding="utf-8")
elif component == "heatmap":
component_css = (template_dir / "_heatmap_assets" / "heatmap.css").read_text(
encoding="utf-8"
)
else:
raise ValueError(
f"Unknown component: {component}. Expected 'flamegraph' or 'heatmap'."
)
return f"{base_css}\n\n{component_css}"

View file

@ -1,136 +1,20 @@
/* ==========================================================================
Flamegraph Viewer - CSS
Python-branded profiler with dark/light theme support
Flamegraph Viewer - Component-Specific CSS
DEPENDENCY: Requires _shared_assets/base.css to be loaded first
This file extends the shared foundation with flamegraph-specific styles.
========================================================================== */
/* --------------------------------------------------------------------------
CSS Variables & Theme System
Layout Overrides (Flamegraph-specific)
-------------------------------------------------------------------------- */
:root {
/* Typography */
--font-sans: "Source Sans Pro", "Lucida Grande", "Lucida Sans Unicode",
"Geneva", "Verdana", sans-serif;
--font-mono: 'SF Mono', 'Monaco', 'Consolas', 'Liberation Mono', monospace;
/* Python brand colors (theme-independent) */
--python-blue: #3776ab;
--python-blue-light: #4584bb;
--python-blue-lighter: #5592cc;
--python-gold: #ffd43b;
--python-gold-dark: #ffcd02;
--python-gold-light: #ffdc5c;
/* Heat palette - defined per theme below */
/* Layout */
--sidebar-width: 280px;
--sidebar-collapsed: 44px;
--topbar-height: 52px;
--statusbar-height: 32px;
/* Transitions */
--transition-fast: 0.15s ease;
--transition-normal: 0.25s ease;
}
/* Light theme (default) - Python yellow-to-blue heat palette */
:root, [data-theme="light"] {
--bg-primary: #ffffff;
--bg-secondary: #f8f9fa;
--bg-tertiary: #e9ecef;
--border: #e9ecef;
--border-subtle: #f0f2f5;
--text-primary: #2e3338;
--text-secondary: #5a6c7d;
--text-muted: #8b949e;
--accent: #3776ab;
--accent-hover: #2d5aa0;
--accent-glow: rgba(55, 118, 171, 0.15);
--shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.08);
--shadow-md: 0 4px 12px rgba(0, 0, 0, 0.1);
--shadow-lg: 0 8px 24px rgba(0, 0, 0, 0.15);
--header-gradient: linear-gradient(135deg, #3776ab 0%, #4584bb 100%);
/* Light mode heat palette - blue to yellow to orange to red (cold to hot) */
--heat-1: #d6e9f8;
--heat-2: #a8d0ef;
--heat-3: #7ba3d1;
--heat-4: #ffe6a8;
--heat-5: #ffd43b;
--heat-6: #ffb84d;
--heat-7: #ff9966;
--heat-8: #ff6347;
}
/* Dark theme - teal-to-orange heat palette */
[data-theme="dark"] {
--bg-primary: #0d1117;
--bg-secondary: #161b22;
--bg-tertiary: #21262d;
--border: #30363d;
--border-subtle: #21262d;
--text-primary: #e6edf3;
--text-secondary: #8b949e;
--text-muted: #6e7681;
--accent: #58a6ff;
--accent-hover: #79b8ff;
--accent-glow: rgba(88, 166, 255, 0.15);
--shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.3);
--shadow-md: 0 4px 12px rgba(0, 0, 0, 0.4);
--shadow-lg: 0 8px 24px rgba(0, 0, 0, 0.5);
--header-gradient: linear-gradient(135deg, #21262d 0%, #30363d 100%);
/* Dark mode heat palette - dark blue to teal to yellow to orange (cold to hot) */
--heat-1: #1e3a5f;
--heat-2: #2d5580;
--heat-3: #4a7ba7;
--heat-4: #5a9fa8;
--heat-5: #7ec488;
--heat-6: #c4de6a;
--heat-7: #f4d44d;
--heat-8: #ff6b35;
}
/* --------------------------------------------------------------------------
Base Styles
-------------------------------------------------------------------------- */
*, *::before, *::after {
box-sizing: border-box;
}
html, body {
margin: 0;
padding: 0;
height: 100%;
overflow: hidden;
}
body {
font-family: var(--font-sans);
font-size: 14px;
line-height: 1.6;
color: var(--text-primary);
background: var(--bg-primary);
transition: background var(--transition-normal), color var(--transition-normal);
}
/* --------------------------------------------------------------------------
Layout Structure
-------------------------------------------------------------------------- */
.app-layout {
display: flex;
flex-direction: column;
height: 100vh;
}
@ -141,78 +25,9 @@ .main-content {
}
/* --------------------------------------------------------------------------
Top Bar
Search Input (Flamegraph-specific)
-------------------------------------------------------------------------- */
.top-bar {
height: 56px;
background: var(--header-gradient);
display: flex;
align-items: center;
padding: 0 16px;
gap: 16px;
flex-shrink: 0;
box-shadow: 0 2px 10px rgba(55, 118, 171, 0.25);
border-bottom: 2px solid var(--python-gold);
}
/* Brand / Logo */
.brand {
display: flex;
align-items: center;
gap: 12px;
color: white;
text-decoration: none;
flex-shrink: 0;
}
.brand-logo {
display: flex;
align-items: center;
justify-content: center;
width: 28px;
height: 28px;
flex-shrink: 0;
}
/* Style the inlined SVG/img inside brand-logo */
.brand-logo svg,
.brand-logo img {
width: 28px;
height: 28px;
display: block;
object-fit: contain;
filter: drop-shadow(0 1px 2px rgba(0, 0, 0, 0.2));
}
.brand-info {
display: flex;
flex-direction: column;
line-height: 1.15;
}
.brand-text {
font-weight: 700;
font-size: 16px;
letter-spacing: -0.3px;
text-shadow: 0 1px 2px rgba(0, 0, 0, 0.15);
}
.brand-subtitle {
font-weight: 500;
font-size: 10px;
opacity: 0.9;
text-transform: uppercase;
letter-spacing: 0.5px;
}
.brand-divider {
width: 1px;
height: 16px;
background: rgba(255, 255, 255, 0.3);
}
/* Search */
.search-wrapper {
flex: 1;
max-width: 360px;
@ -308,39 +123,6 @@ .search-wrapper.has-value .search-clear {
display: flex;
}
/* Toolbar */
.toolbar {
display: flex;
align-items: center;
gap: 6px;
margin-left: auto;
}
.toolbar-btn {
display: flex;
align-items: center;
justify-content: center;
width: 32px;
height: 32px;
padding: 0;
font-size: 15px;
color: white;
background: rgba(255, 255, 255, 0.12);
border: 1px solid rgba(255, 255, 255, 0.18);
border-radius: 6px;
cursor: pointer;
transition: all var(--transition-fast);
}
.toolbar-btn:hover {
background: rgba(255, 255, 255, 0.22);
border-color: rgba(255, 255, 255, 0.35);
}
.toolbar-btn:active {
transform: scale(0.95);
}
/* --------------------------------------------------------------------------
Sidebar
-------------------------------------------------------------------------- */
@ -667,11 +449,6 @@ .efficiency-fill::after {
animation: shimmer 2s ease-in-out infinite;
}
@keyframes shimmer {
0% { left: -100%; }
100% { left: 100%; }
}
/* --------------------------------------------------------------------------
Thread Stats Grid (in Sidebar)
-------------------------------------------------------------------------- */
@ -974,56 +751,6 @@ .d3-flame-graph rect.search-dim {
opacity: 0.25;
}
/* --------------------------------------------------------------------------
Status Bar
-------------------------------------------------------------------------- */
.status-bar {
height: var(--statusbar-height);
background: var(--bg-secondary);
border-top: 1px solid var(--border);
display: flex;
align-items: center;
padding: 0 16px;
gap: 16px;
font-family: var(--font-mono);
font-size: 11px;
color: var(--text-secondary);
flex-shrink: 0;
}
.status-item {
display: flex;
align-items: center;
gap: 5px;
}
.status-item::before {
content: '';
width: 4px;
height: 4px;
background: var(--python-gold);
border-radius: 50%;
}
.status-item:first-child::before {
display: none;
}
.status-label {
color: var(--text-muted);
}
.status-value {
color: var(--text-primary);
font-weight: 500;
}
.status-value.accent {
color: var(--accent);
font-weight: 600;
}
/* --------------------------------------------------------------------------
Tooltip
-------------------------------------------------------------------------- */
@ -1137,38 +864,7 @@ .tooltip-hint {
}
/* --------------------------------------------------------------------------
Animations
-------------------------------------------------------------------------- */
@keyframes fadeIn {
from { opacity: 0; }
to { opacity: 1; }
}
@keyframes slideUp {
from {
opacity: 0;
transform: translateY(12px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
/* --------------------------------------------------------------------------
Focus States (Accessibility)
-------------------------------------------------------------------------- */
button:focus-visible,
select:focus-visible,
input:focus-visible {
outline: 2px solid var(--python-gold);
outline-offset: 2px;
}
/* --------------------------------------------------------------------------
Responsive
Responsive (Flamegraph-specific)
-------------------------------------------------------------------------- */
@media (max-width: 900px) {
@ -1185,20 +881,12 @@ @media (max-width: 900px) {
width: var(--sidebar-collapsed);
}
.brand-subtitle {
display: none;
}
.search-wrapper {
max-width: 220px;
}
}
@media (max-width: 600px) {
.toolbar-btn:not(.theme-toggle) {
display: none;
}
.search-wrapper {
max-width: 160px;
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,349 @@
// Tachyon Profiler - Heatmap JavaScript
// Interactive features for the heatmap visualization
// Aligned with Flamegraph viewer design patterns
// ============================================================================
// State Management
// ============================================================================
let currentMenu = null;
let colorMode = 'self'; // 'self' or 'cumulative' - default to self
let coldCodeHidden = false;
// ============================================================================
// Theme Support
// ============================================================================
function toggleTheme() {
const html = document.documentElement;
const current = html.getAttribute('data-theme') || 'light';
const next = current === 'light' ? 'dark' : 'light';
html.setAttribute('data-theme', next);
localStorage.setItem('heatmap-theme', next);
// Update theme button icon
const btn = document.getElementById('theme-btn');
if (btn) {
btn.innerHTML = next === 'dark' ? '&#9788;' : '&#9790;'; // sun or moon
}
// Rebuild scroll marker with new theme colors
buildScrollMarker();
}
function restoreUIState() {
// Restore theme
const savedTheme = localStorage.getItem('heatmap-theme');
if (savedTheme) {
document.documentElement.setAttribute('data-theme', savedTheme);
const btn = document.getElementById('theme-btn');
if (btn) {
btn.innerHTML = savedTheme === 'dark' ? '&#9788;' : '&#9790;';
}
}
}
// ============================================================================
// Utility Functions
// ============================================================================
function createElement(tag, className, textContent = '') {
const el = document.createElement(tag);
if (className) el.className = className;
if (textContent) el.textContent = textContent;
return el;
}
function calculateMenuPosition(buttonRect, menuWidth, menuHeight) {
const viewport = { width: window.innerWidth, height: window.innerHeight };
const scroll = {
x: window.pageXOffset || document.documentElement.scrollLeft,
y: window.pageYOffset || document.documentElement.scrollTop
};
const left = buttonRect.right + menuWidth + 10 < viewport.width
? buttonRect.right + scroll.x + 10
: Math.max(scroll.x + 10, buttonRect.left + scroll.x - menuWidth - 10);
const top = buttonRect.bottom + menuHeight + 10 < viewport.height
? buttonRect.bottom + scroll.y + 5
: Math.max(scroll.y + 10, buttonRect.top + scroll.y - menuHeight - 10);
return { left, top };
}
// ============================================================================
// Menu Management
// ============================================================================
function closeMenu() {
if (currentMenu) {
currentMenu.remove();
currentMenu = null;
}
}
function showNavigationMenu(button, items, title) {
closeMenu();
const menu = createElement('div', 'callee-menu');
menu.appendChild(createElement('div', 'callee-menu-header', title));
items.forEach(linkData => {
const item = createElement('div', 'callee-menu-item');
const funcDiv = createElement('div', 'callee-menu-func');
funcDiv.textContent = linkData.func;
if (linkData.count !== undefined && linkData.count > 0) {
const countBadge = createElement('span', 'count-badge');
countBadge.textContent = linkData.count.toLocaleString();
countBadge.title = `${linkData.count.toLocaleString()} samples`;
funcDiv.appendChild(document.createTextNode(' '));
funcDiv.appendChild(countBadge);
}
item.appendChild(funcDiv);
item.appendChild(createElement('div', 'callee-menu-file', linkData.file));
item.addEventListener('click', () => window.location.href = linkData.link);
menu.appendChild(item);
});
const pos = calculateMenuPosition(button.getBoundingClientRect(), 350, 300);
menu.style.left = `${pos.left}px`;
menu.style.top = `${pos.top}px`;
document.body.appendChild(menu);
currentMenu = menu;
}
// ============================================================================
// Navigation
// ============================================================================
function handleNavigationClick(button, e) {
e.stopPropagation();
const navData = button.getAttribute('data-nav');
if (navData) {
window.location.href = JSON.parse(navData).link;
return;
}
const navMulti = button.getAttribute('data-nav-multi');
if (navMulti) {
const items = JSON.parse(navMulti);
const title = button.classList.contains('caller') ? 'Choose a caller:' : 'Choose a callee:';
showNavigationMenu(button, items, title);
}
}
function scrollToTargetLine() {
if (!window.location.hash) return;
const target = document.querySelector(window.location.hash);
if (target) {
target.scrollIntoView({ behavior: 'smooth', block: 'start' });
}
}
// ============================================================================
// Sample Count & Intensity
// ============================================================================
function getSampleCount(line) {
let text;
if (colorMode === 'self') {
text = line.querySelector('.line-samples-self')?.textContent.trim().replace(/,/g, '');
} else {
text = line.querySelector('.line-samples-cumulative')?.textContent.trim().replace(/,/g, '');
}
return parseInt(text) || 0;
}
function getIntensityClass(ratio) {
if (ratio > 0.75) return 'vhot';
if (ratio > 0.5) return 'hot';
if (ratio > 0.25) return 'warm';
return 'cold';
}
// ============================================================================
// Scroll Minimap
// ============================================================================
function buildScrollMarker() {
const existing = document.getElementById('scroll_marker');
if (existing) existing.remove();
if (document.body.scrollHeight <= window.innerHeight) return;
const allLines = document.querySelectorAll('.code-line');
const lines = Array.from(allLines).filter(line => line.style.display !== 'none');
const markerScale = window.innerHeight / document.body.scrollHeight;
const lineHeight = Math.min(Math.max(3, window.innerHeight / lines.length), 10);
const maxSamples = Math.max(...Array.from(lines, getSampleCount));
const scrollMarker = createElement('div', '');
scrollMarker.id = 'scroll_marker';
let prevLine = -99, lastMark, lastTop;
lines.forEach((line, index) => {
const samples = getSampleCount(line);
if (samples === 0) return;
const lineTop = Math.floor(line.offsetTop * markerScale);
const lineNumber = index + 1;
const intensityClass = maxSamples > 0 ? getIntensityClass(samples / maxSamples) : 'cold';
if (lineNumber === prevLine + 1 && lastMark?.classList.contains(intensityClass)) {
lastMark.style.height = `${lineTop + lineHeight - lastTop}px`;
} else {
lastMark = createElement('div', `marker ${intensityClass}`);
lastMark.style.height = `${lineHeight}px`;
lastMark.style.top = `${lineTop}px`;
scrollMarker.appendChild(lastMark);
lastTop = lineTop;
}
prevLine = lineNumber;
});
document.body.appendChild(scrollMarker);
}
// ============================================================================
// Toggle Controls
// ============================================================================
function updateToggleUI(toggleId, isOn) {
const toggle = document.getElementById(toggleId);
if (toggle) {
const track = toggle.querySelector('.toggle-track');
const labels = toggle.querySelectorAll('.toggle-label');
if (isOn) {
track.classList.add('on');
labels[0].classList.remove('active');
labels[1].classList.add('active');
} else {
track.classList.remove('on');
labels[0].classList.add('active');
labels[1].classList.remove('active');
}
}
}
function toggleColdCode() {
coldCodeHidden = !coldCodeHidden;
applyHotFilter();
updateToggleUI('toggle-cold', coldCodeHidden);
buildScrollMarker();
}
function applyHotFilter() {
const lines = document.querySelectorAll('.code-line');
lines.forEach(line => {
const selfSamples = line.querySelector('.line-samples-self')?.textContent.trim();
const cumulativeSamples = line.querySelector('.line-samples-cumulative')?.textContent.trim();
let isCold;
if (colorMode === 'self') {
isCold = !selfSamples || selfSamples === '';
} else {
isCold = !cumulativeSamples || cumulativeSamples === '';
}
if (isCold) {
line.style.display = coldCodeHidden ? 'none' : 'flex';
} else {
line.style.display = 'flex';
}
});
}
function toggleColorMode() {
colorMode = colorMode === 'self' ? 'cumulative' : 'self';
const lines = document.querySelectorAll('.code-line');
lines.forEach(line => {
let bgColor;
if (colorMode === 'self') {
bgColor = line.getAttribute('data-self-color');
} else {
bgColor = line.getAttribute('data-cumulative-color');
}
if (bgColor) {
line.style.background = bgColor;
}
});
updateToggleUI('toggle-color-mode', colorMode === 'cumulative');
if (coldCodeHidden) {
applyHotFilter();
}
buildScrollMarker();
}
// ============================================================================
// Initialization
// ============================================================================
document.addEventListener('DOMContentLoaded', function() {
// Restore UI state (theme, etc.)
restoreUIState();
// Apply background colors
document.querySelectorAll('.code-line[data-bg-color]').forEach(line => {
const bgColor = line.getAttribute('data-bg-color');
if (bgColor) {
line.style.background = bgColor;
}
});
// Initialize navigation buttons
document.querySelectorAll('.nav-btn').forEach(button => {
button.addEventListener('click', e => handleNavigationClick(button, e));
});
// Initialize line number permalink handlers
document.querySelectorAll('.line-number').forEach(lineNum => {
lineNum.style.cursor = 'pointer';
lineNum.addEventListener('click', e => {
window.location.hash = `line-${e.target.textContent.trim()}`;
});
});
// Initialize toggle buttons
const toggleColdBtn = document.getElementById('toggle-cold');
if (toggleColdBtn) {
toggleColdBtn.addEventListener('click', toggleColdCode);
}
const colorModeBtn = document.getElementById('toggle-color-mode');
if (colorModeBtn) {
colorModeBtn.addEventListener('click', toggleColorMode);
}
// Build scroll marker
setTimeout(buildScrollMarker, 200);
// Setup scroll-to-line behavior
setTimeout(scrollToTargetLine, 100);
});
// Close menu when clicking outside
document.addEventListener('click', e => {
if (currentMenu && !currentMenu.contains(e.target) && !e.target.classList.contains('nav-btn')) {
closeMenu();
}
});
// Handle hash changes
window.addEventListener('hashchange', () => setTimeout(scrollToTargetLine, 50));
// Rebuild scroll marker on resize
window.addEventListener('resize', buildScrollMarker);

View file

@ -0,0 +1,111 @@
// Tachyon Profiler - Heatmap Index JavaScript
// Index page specific functionality
// ============================================================================
// Theme Support
// ============================================================================
function toggleTheme() {
const html = document.documentElement;
const current = html.getAttribute('data-theme') || 'light';
const next = current === 'light' ? 'dark' : 'light';
html.setAttribute('data-theme', next);
localStorage.setItem('heatmap-theme', next);
// Update theme button icon
const btn = document.getElementById('theme-btn');
if (btn) {
btn.innerHTML = next === 'dark' ? '&#9788;' : '&#9790;'; // sun or moon
}
}
function restoreUIState() {
// Restore theme
const savedTheme = localStorage.getItem('heatmap-theme');
if (savedTheme) {
document.documentElement.setAttribute('data-theme', savedTheme);
const btn = document.getElementById('theme-btn');
if (btn) {
btn.innerHTML = savedTheme === 'dark' ? '&#9788;' : '&#9790;';
}
}
}
// ============================================================================
// Type Section Toggle (stdlib, project, etc)
// ============================================================================
function toggleTypeSection(header) {
const section = header.parentElement;
const content = section.querySelector('.type-content');
const icon = header.querySelector('.type-icon');
if (content.style.display === 'none') {
content.style.display = 'block';
icon.textContent = '\u25BC';
} else {
content.style.display = 'none';
icon.textContent = '\u25B6';
}
}
// ============================================================================
// Folder Toggle
// ============================================================================
function toggleFolder(header) {
const folder = header.parentElement;
const content = folder.querySelector('.folder-content');
const icon = header.querySelector('.folder-icon');
if (content.style.display === 'none') {
content.style.display = 'block';
icon.textContent = '\u25BC';
folder.classList.remove('collapsed');
} else {
content.style.display = 'none';
icon.textContent = '\u25B6';
folder.classList.add('collapsed');
}
}
// ============================================================================
// Expand/Collapse All
// ============================================================================
function expandAll() {
// Expand all type sections
document.querySelectorAll('.type-section').forEach(section => {
const content = section.querySelector('.type-content');
const icon = section.querySelector('.type-icon');
content.style.display = 'block';
icon.textContent = '\u25BC';
});
// Expand all folders
document.querySelectorAll('.folder-node').forEach(folder => {
const content = folder.querySelector('.folder-content');
const icon = folder.querySelector('.folder-icon');
content.style.display = 'block';
icon.textContent = '\u25BC';
folder.classList.remove('collapsed');
});
}
function collapseAll() {
document.querySelectorAll('.folder-node').forEach(folder => {
const content = folder.querySelector('.folder-content');
const icon = folder.querySelector('.folder-icon');
content.style.display = 'none';
icon.textContent = '\u25B6';
folder.classList.add('collapsed');
});
}
// ============================================================================
// Initialization
// ============================================================================
document.addEventListener('DOMContentLoaded', function() {
restoreUIState();
});

View file

@ -0,0 +1,118 @@
<!doctype html>
<html lang="en" data-theme="light">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tachyon Profiler - Heatmap Report</title>
<!-- INLINE_CSS -->
</head>
<body>
<div class="app-layout">
<!-- Top Bar -->
<header class="top-bar">
<div class="brand">
<span class="brand-text">Tachyon</span>
<span class="brand-divider"></span>
<span class="brand-subtitle">Heatmap Report</span>
</div>
<div class="toolbar">
<button
class="toolbar-btn theme-toggle"
onclick="toggleTheme()"
title="Toggle theme"
id="theme-btn"
>&#9790;</button>
</div>
</header>
<!-- Main Content -->
<div class="main-content">
<!-- Stats Summary -->
<div class="stats-summary">
<div class="stat-card">
<div class="stat-icon">&#128196;</div>
<div class="stat-data">
<span class="stat-value"><!-- NUM_FILES --></span>
<span class="stat-label">Files Profiled</span>
</div>
<div class="stat-sparkline"></div>
</div>
<div class="stat-card">
<div class="stat-icon">&#128202;</div>
<div class="stat-data">
<span class="stat-value"><!-- TOTAL_SAMPLES --></span>
<span class="stat-label">Total Snapshots</span>
</div>
<div class="stat-sparkline"></div>
</div>
<div class="stat-card">
<div class="stat-icon">&#9201;</div>
<div class="stat-data">
<span class="stat-value"><!-- DURATION --></span>
<span class="stat-label">Duration</span>
</div>
<div class="stat-sparkline"></div>
</div>
<div class="stat-card">
<div class="stat-icon">&#9889;</div>
<div class="stat-data">
<span class="stat-value"><!-- SAMPLE_RATE --></span>
<span class="stat-label">Samples/sec</span>
</div>
<div class="stat-sparkline"></div>
</div>
<div class="rate-card">
<div class="rate-header">
<div class="rate-info">
<div class="rate-icon">&#9888;</div>
<span class="rate-label">Error Rate</span>
</div>
<span class="rate-value"><!-- ERROR_RATE --></span>
</div>
<div class="rate-bar">
<div class="rate-fill <!-- ERROR_RATE_CLASS -->" style="width: <!-- ERROR_RATE_WIDTH -->%;"></div>
</div>
</div>
<div class="rate-card">
<div class="rate-header">
<div class="rate-info">
<div class="rate-icon">&#128165;</div>
<span class="rate-label">Missed Samples</span>
</div>
<span class="rate-value"><!-- MISSED_SAMPLES --></span>
</div>
<div class="rate-bar">
<div class="rate-fill <!-- MISSED_SAMPLES_CLASS -->" style="width: <!-- MISSED_SAMPLES_WIDTH -->%;"></div>
</div>
</div>
</div>
<!-- File List Section -->
<div class="section-header">
<h2 class="section-title">Profiled Files</h2>
</div>
<div class="filter-controls">
<button onclick="expandAll()" class="control-btn">Expand All</button>
<button onclick="collapseAll()" class="control-btn">Collapse All</button>
</div>
<div class="module-sections">
<!-- SECTIONS_HTML -->
</div>
</div>
<!-- Status Bar -->
<footer class="status-bar">
<span class="status-item">
<span class="status-value">Tachyon Profiler</span>
</span>
<span class="status-item">
<span class="status-label">Python Sampling Profiler</span>
</span>
</footer>
</div>
<!-- INLINE_JS -->
</body>
</html>

View file

@ -0,0 +1,96 @@
<!doctype html>
<html lang="en" data-theme="light">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><!-- FILENAME --> - Heatmap</title>
<!-- INLINE_CSS -->
</head>
<body class="code-view">
<div class="app-layout">
<!-- Top Bar (Code Header) -->
<header class="top-bar">
<div class="brand">
<span class="brand-text">Tachyon</span>
<span class="brand-divider"></span>
<span class="brand-subtitle" style="font-family: var(--font-mono); font-size: 13px;"><!-- FILENAME --></span>
</div>
<div class="toolbar">
<a href="index.html" class="back-link">Back to Index</a>
<button
class="toolbar-btn theme-toggle"
onclick="toggleTheme()"
title="Toggle theme"
id="theme-btn"
>&#9790;</button>
</div>
</header>
<!-- File Stats Bar -->
<div class="file-stats">
<div class="stats-grid">
<div class="stat-item">
<div class="stat-value"><!-- TOTAL_SELF_SAMPLES --></div>
<div class="stat-label">Self Samples</div>
</div>
<div class="stat-item">
<div class="stat-value"><!-- TOTAL_SAMPLES --></div>
<div class="stat-label">Cumulative</div>
</div>
<div class="stat-item">
<div class="stat-value"><!-- NUM_LINES --></div>
<div class="stat-label">Lines Hit</div>
</div>
<div class="stat-item">
<div class="stat-value"><!-- PERCENTAGE -->%</div>
<div class="stat-label">% of Total</div>
</div>
<div class="stat-item">
<div class="stat-value"><!-- MAX_SELF_SAMPLES --></div>
<div class="stat-label">Max Self</div>
</div>
<div class="stat-item">
<div class="stat-value"><!-- MAX_SAMPLES --></div>
<div class="stat-label">Max Total</div>
</div>
</div>
</div>
<!-- Legend -->
<div class="legend">
<div class="legend-content">
<span class="legend-title">Intensity:</span>
<div class="legend-gradient"></div>
<div class="legend-labels">
<span>Cold</span>
<span></span>
<span>Hot</span>
</div>
<div class="toggle-switch" id="toggle-color-mode" title="Toggle between self time and total time coloring">
<span class="toggle-label active">Self Time</span>
<div class="toggle-track"></div>
<span class="toggle-label">Total Time</span>
</div>
<div class="toggle-switch" id="toggle-cold" title="Toggle visibility of lines with zero samples">
<span class="toggle-label active">Show All</span>
<div class="toggle-track"></div>
<span class="toggle-label">Hot Only</span>
</div>
</div>
</div>
<!-- Code Container -->
<div class="code-container">
<div class="code-header-row">
<div class="header-line-number">Line</div>
<div class="header-samples-self">Self</div>
<div class="header-samples-cumulative">Total</div>
<div class="header-content">Code</div>
</div>
<!-- CODE_LINES -->
</div>
</div>
<!-- INLINE_JS -->
</body>
</html>

View file

@ -0,0 +1,369 @@
/* ==========================================================================
Python Profiler - Shared CSS Foundation
Design system shared between Flamegraph and Heatmap viewers
========================================================================== */
/* --------------------------------------------------------------------------
CSS Variables & Theme System
-------------------------------------------------------------------------- */
:root {
/* Typography */
--font-sans: "Source Sans Pro", "Lucida Grande", "Lucida Sans Unicode",
"Geneva", "Verdana", sans-serif;
--font-mono: 'SF Mono', 'Monaco', 'Consolas', 'Liberation Mono', monospace;
/* Python brand colors (theme-independent) */
--python-blue: #3776ab;
--python-blue-light: #4584bb;
--python-blue-lighter: #5592cc;
--python-gold: #ffd43b;
--python-gold-dark: #ffcd02;
--python-gold-light: #ffdc5c;
/* Heat palette - defined per theme below */
/* Layout */
--sidebar-width: 280px;
--sidebar-collapsed: 44px;
--topbar-height: 56px;
--statusbar-height: 32px;
/* Transitions */
--transition-fast: 0.15s ease;
--transition-normal: 0.25s ease;
}
/* Light theme (default) */
:root, [data-theme="light"] {
--bg-primary: #ffffff;
--bg-secondary: #f8f9fa;
--bg-tertiary: #e9ecef;
--border: #e9ecef;
--border-subtle: #f0f2f5;
--text-primary: #2e3338;
--text-secondary: #5a6c7d;
--text-muted: #8b949e;
--accent: #3776ab;
--accent-hover: #2d5aa0;
--accent-glow: rgba(55, 118, 171, 0.15);
--shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.08);
--shadow-md: 0 4px 12px rgba(0, 0, 0, 0.1);
--shadow-lg: 0 8px 24px rgba(0, 0, 0, 0.15);
--header-gradient: linear-gradient(135deg, #3776ab 0%, #4584bb 100%);
/* Light mode heat palette - blue to yellow to orange to red (cold to hot) */
--heat-1: #d6e9f8;
--heat-2: #a8d0ef;
--heat-3: #7ba3d1;
--heat-4: #ffe6a8;
--heat-5: #ffd43b;
--heat-6: #ffb84d;
--heat-7: #ff9966;
--heat-8: #ff6347;
/* Code view specific */
--code-bg: #ffffff;
--code-bg-line: #f8f9fa;
--code-border: #e9ecef;
--code-text: #2e3338;
--code-text-muted: #8b949e;
--code-accent: #3776ab;
/* Navigation colors */
--nav-caller: #2563eb;
--nav-caller-hover: #1d4ed8;
--nav-callee: #dc2626;
--nav-callee-hover: #b91c1c;
}
/* Dark theme */
[data-theme="dark"] {
--bg-primary: #0d1117;
--bg-secondary: #161b22;
--bg-tertiary: #21262d;
--border: #30363d;
--border-subtle: #21262d;
--text-primary: #e6edf3;
--text-secondary: #8b949e;
--text-muted: #6e7681;
--accent: #58a6ff;
--accent-hover: #79b8ff;
--accent-glow: rgba(88, 166, 255, 0.15);
--shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.3);
--shadow-md: 0 4px 12px rgba(0, 0, 0, 0.4);
--shadow-lg: 0 8px 24px rgba(0, 0, 0, 0.5);
--header-gradient: linear-gradient(135deg, #21262d 0%, #30363d 100%);
/* Dark mode heat palette - dark blue to teal to yellow to orange (cold to hot) */
--heat-1: #1e3a5f;
--heat-2: #2d5580;
--heat-3: #4a7ba7;
--heat-4: #5a9fa8;
--heat-5: #7ec488;
--heat-6: #c4de6a;
--heat-7: #f4d44d;
--heat-8: #ff6b35;
/* Code view specific - dark mode */
--code-bg: #0d1117;
--code-bg-line: #161b22;
--code-border: #30363d;
--code-text: #e6edf3;
--code-text-muted: #6e7681;
--code-accent: #58a6ff;
/* Navigation colors - dark theme friendly */
--nav-caller: #58a6ff;
--nav-caller-hover: #4184e4;
--nav-callee: #f87171;
--nav-callee-hover: #e53e3e;
}
/* --------------------------------------------------------------------------
Base Styles
-------------------------------------------------------------------------- */
*, *::before, *::after {
box-sizing: border-box;
}
html, body {
margin: 0;
padding: 0;
}
body {
font-family: var(--font-sans);
font-size: 14px;
line-height: 1.6;
color: var(--text-primary);
background: var(--bg-primary);
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
transition: background var(--transition-normal), color var(--transition-normal);
}
/* --------------------------------------------------------------------------
Layout Structure
-------------------------------------------------------------------------- */
.app-layout {
display: flex;
flex-direction: column;
}
/* --------------------------------------------------------------------------
Top Bar
-------------------------------------------------------------------------- */
.top-bar {
height: var(--topbar-height);
background: var(--header-gradient);
display: flex;
align-items: center;
padding: 0 16px;
gap: 16px;
flex-shrink: 0;
box-shadow: 0 2px 10px rgba(55, 118, 171, 0.25);
border-bottom: 2px solid var(--python-gold);
}
/* Brand / Logo */
.brand {
display: flex;
align-items: center;
gap: 12px;
color: white;
text-decoration: none;
flex-shrink: 0;
}
.brand-logo {
display: flex;
align-items: center;
justify-content: center;
width: 28px;
height: 28px;
flex-shrink: 0;
}
/* Style the inlined SVG/img inside brand-logo */
.brand-logo svg,
.brand-logo img {
width: 28px;
height: 28px;
display: block;
object-fit: contain;
filter: drop-shadow(0 1px 2px rgba(0, 0, 0, 0.2));
}
.brand-info {
display: flex;
flex-direction: column;
line-height: 1.15;
}
.brand-text {
font-weight: 700;
font-size: 16px;
letter-spacing: -0.3px;
text-shadow: 0 1px 2px rgba(0, 0, 0, 0.15);
}
.brand-subtitle {
font-weight: 500;
font-size: 10px;
opacity: 0.9;
text-transform: uppercase;
letter-spacing: 0.5px;
}
.brand-divider {
width: 1px;
height: 16px;
background: rgba(255, 255, 255, 0.3);
}
/* Toolbar */
.toolbar {
display: flex;
align-items: center;
gap: 6px;
margin-left: auto;
}
.toolbar-btn {
display: flex;
align-items: center;
justify-content: center;
width: 32px;
height: 32px;
padding: 0;
font-size: 15px;
color: white;
background: rgba(255, 255, 255, 0.12);
border: 1px solid rgba(255, 255, 255, 0.18);
border-radius: 6px;
cursor: pointer;
transition: all var(--transition-fast);
}
.toolbar-btn:hover {
background: rgba(255, 255, 255, 0.22);
border-color: rgba(255, 255, 255, 0.35);
}
.toolbar-btn:active {
transform: scale(0.95);
}
/* --------------------------------------------------------------------------
Status Bar
-------------------------------------------------------------------------- */
.status-bar {
height: var(--statusbar-height);
background: var(--bg-secondary);
border-top: 1px solid var(--border);
display: flex;
align-items: center;
padding: 0 16px;
gap: 16px;
font-family: var(--font-mono);
font-size: 11px;
color: var(--text-secondary);
flex-shrink: 0;
}
.status-item {
display: flex;
align-items: center;
gap: 5px;
}
.status-item::before {
content: '';
width: 4px;
height: 4px;
background: var(--python-gold);
border-radius: 50%;
}
.status-item:first-child::before {
display: none;
}
.status-label {
color: var(--text-muted);
}
.status-value {
color: var(--text-primary);
font-weight: 500;
}
.status-value.accent {
color: var(--accent);
font-weight: 600;
}
/* --------------------------------------------------------------------------
Animations
-------------------------------------------------------------------------- */
@keyframes fadeIn {
from { opacity: 0; }
to { opacity: 1; }
}
@keyframes slideUp {
from {
opacity: 0;
transform: translateY(12px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
@keyframes shimmer {
0% { left: -100%; }
100% { left: 100%; }
}
/* --------------------------------------------------------------------------
Focus States (Accessibility)
-------------------------------------------------------------------------- */
button:focus-visible,
select:focus-visible,
input:focus-visible {
outline: 2px solid var(--python-gold);
outline-offset: 2px;
}
/* --------------------------------------------------------------------------
Shared Responsive
-------------------------------------------------------------------------- */
@media (max-width: 900px) {
.brand-subtitle {
display: none;
}
}
@media (max-width: 600px) {
.toolbar-btn:not(.theme-toggle) {
display: none;
}
}

View file

@ -9,6 +9,7 @@
from .sample import sample, sample_live
from .pstats_collector import PstatsCollector
from .stack_collector import CollapsedStackCollector, FlamegraphCollector
from .heatmap_collector import HeatmapCollector
from .gecko_collector import GeckoCollector
from .constants import (
PROFILING_MODE_ALL,
@ -71,6 +72,7 @@ class CustomFormatter(
"collapsed": "txt",
"flamegraph": "html",
"gecko": "json",
"heatmap": "html",
}
COLLECTOR_MAP = {
@ -78,6 +80,7 @@ class CustomFormatter(
"collapsed": CollapsedStackCollector,
"flamegraph": FlamegraphCollector,
"gecko": GeckoCollector,
"heatmap": HeatmapCollector,
}
@ -238,14 +241,21 @@ def _add_format_options(parser):
dest="format",
help="Generate Gecko format for Firefox Profiler",
)
format_group.add_argument(
"--heatmap",
action="store_const",
const="heatmap",
dest="format",
help="Generate interactive HTML heatmap visualization with line-level sample counts",
)
parser.set_defaults(format="pstats")
output_group.add_argument(
"-o",
"--output",
dest="outfile",
help="Save output to a file (default: stdout for pstats, "
"auto-generated filename for other formats)",
help="Output path (default: stdout for pstats, auto-generated for others). "
"For heatmap: directory name (default: heatmap_PID)",
)
@ -327,6 +337,9 @@ def _generate_output_filename(format_type, pid):
Generated filename
"""
extension = FORMAT_EXTENSIONS.get(format_type, "txt")
# For heatmap, use cleaner directory name without extension
if format_type == "heatmap":
return f"heatmap_{pid}"
return f"{format_type}.{pid}.{extension}"

File diff suppressed because it is too large Load diff

View file

@ -10,6 +10,7 @@
from .pstats_collector import PstatsCollector
from .stack_collector import CollapsedStackCollector, FlamegraphCollector
from .heatmap_collector import HeatmapCollector
from .gecko_collector import GeckoCollector
from .constants import (
PROFILING_MODE_WALL,
@ -25,7 +26,6 @@
_FREE_THREADED_BUILD = sysconfig.get_config_var("Py_GIL_DISABLED") is not None
class SampleProfiler:
def __init__(self, pid, sample_interval_usec, all_threads, *, mode=PROFILING_MODE_WALL, native=False, gc=True, skip_non_matching_threads=True):
self.pid = pid

View file

@ -6,6 +6,7 @@
import linecache
import os
from ._css_utils import get_combined_css
from .collector import Collector
from .string_table import StringTable
@ -331,9 +332,9 @@ def _create_flamegraph_html(self, data):
fg_js_path = d3_flame_graph_dir / "d3-flamegraph.min.js"
fg_tooltip_js_path = d3_flame_graph_dir / "d3-flamegraph-tooltip.min.js"
html_template = (template_dir / "flamegraph_template.html").read_text(encoding="utf-8")
css_content = (template_dir / "flamegraph.css").read_text(encoding="utf-8")
js_content = (template_dir / "flamegraph.js").read_text(encoding="utf-8")
html_template = (template_dir / "_flamegraph_assets" / "flamegraph_template.html").read_text(encoding="utf-8")
css_content = get_combined_css("flamegraph")
js_content = (template_dir / "_flamegraph_assets" / "flamegraph.js").read_text(encoding="utf-8")
# Inline first-party CSS/JS
html_template = html_template.replace(

View file

@ -0,0 +1,653 @@
"""Tests for the heatmap collector (profiling.sampling)."""
import os
import shutil
import tempfile
import unittest
from pathlib import Path
from profiling.sampling.heatmap_collector import (
HeatmapCollector,
get_python_path_info,
extract_module_name,
)
from test.support import captured_stdout, captured_stderr
# =============================================================================
# Unit Tests for Public Helper Functions
# =============================================================================
class TestPathInfoFunctions(unittest.TestCase):
"""Test public helper functions for path information."""
def test_get_python_path_info_returns_dict(self):
"""Test that get_python_path_info returns a dictionary with expected keys."""
path_info = get_python_path_info()
self.assertIsInstance(path_info, dict)
self.assertIn('stdlib', path_info)
self.assertIn('site_packages', path_info)
self.assertIn('sys_path', path_info)
def test_get_python_path_info_stdlib_is_path_or_none(self):
"""Test that stdlib is either a Path object or None."""
path_info = get_python_path_info()
if path_info['stdlib'] is not None:
self.assertIsInstance(path_info['stdlib'], Path)
def test_get_python_path_info_site_packages_is_list(self):
"""Test that site_packages is a list."""
path_info = get_python_path_info()
self.assertIsInstance(path_info['site_packages'], list)
for item in path_info['site_packages']:
self.assertIsInstance(item, Path)
def test_get_python_path_info_sys_path_is_list(self):
"""Test that sys_path is a list of Path objects."""
path_info = get_python_path_info()
self.assertIsInstance(path_info['sys_path'], list)
for item in path_info['sys_path']:
self.assertIsInstance(item, Path)
def test_extract_module_name_with_none(self):
"""Test extract_module_name with None filename."""
path_info = get_python_path_info()
module_name, module_type = extract_module_name(None, path_info)
self.assertEqual(module_name, 'unknown')
self.assertEqual(module_type, 'other')
def test_extract_module_name_with_empty_string(self):
"""Test extract_module_name with empty filename."""
path_info = get_python_path_info()
module_name, module_type = extract_module_name('', path_info)
self.assertEqual(module_name, 'unknown')
self.assertEqual(module_type, 'other')
def test_extract_module_name_with_stdlib_file(self):
"""Test extract_module_name with a standard library file."""
path_info = get_python_path_info()
# Use os module as a known stdlib file
if path_info['stdlib']:
stdlib_file = str(path_info['stdlib'] / 'os.py')
module_name, module_type = extract_module_name(stdlib_file, path_info)
self.assertEqual(module_type, 'stdlib')
self.assertIn('os', module_name)
def test_extract_module_name_with_project_file(self):
"""Test extract_module_name with a project file."""
path_info = get_python_path_info()
# Create a mock project file path
if path_info['sys_path']:
# Use current directory as project path
project_file = '/some/project/path/mymodule.py'
module_name, module_type = extract_module_name(project_file, path_info)
# Should classify as 'other' if not in sys.path
self.assertIn(module_type, ['project', 'other'])
def test_extract_module_name_removes_py_extension(self):
"""Test that .py extension is removed from module names."""
path_info = get_python_path_info()
# Test with a simple .py file
module_name, module_type = extract_module_name('/path/to/test.py', path_info)
# Module name should not contain .py
self.assertNotIn('.py', module_name)
def test_extract_module_name_with_special_files(self):
"""Test extract_module_name with special filenames like <string>."""
path_info = get_python_path_info()
special_files = ['<string>', '<stdin>', '[eval]']
for special_file in special_files:
module_name, module_type = extract_module_name(special_file, path_info)
self.assertEqual(module_type, 'other')
# =============================================================================
# Unit Tests for HeatmapCollector Public API
# =============================================================================
class TestHeatmapCollectorInit(unittest.TestCase):
"""Test HeatmapCollector initialization."""
def test_init_creates_empty_data_structures(self):
"""Test that __init__ creates empty data structures."""
collector = HeatmapCollector(sample_interval_usec=100)
# Check that data structures are initialized
self.assertIsInstance(collector.line_samples, dict)
self.assertIsInstance(collector.file_samples, dict)
self.assertIsInstance(collector.line_self_samples, dict)
self.assertIsInstance(collector.file_self_samples, dict)
self.assertIsInstance(collector.call_graph, dict)
self.assertIsInstance(collector.callers_graph, dict)
self.assertIsInstance(collector.function_definitions, dict)
self.assertIsInstance(collector.edge_samples, dict)
# Check that they're empty
self.assertEqual(len(collector.line_samples), 0)
self.assertEqual(len(collector.file_samples), 0)
self.assertEqual(len(collector.line_self_samples), 0)
self.assertEqual(len(collector.file_self_samples), 0)
def test_init_sets_total_samples_to_zero(self):
"""Test that total samples starts at zero."""
collector = HeatmapCollector(sample_interval_usec=100)
self.assertEqual(collector._total_samples, 0)
def test_init_creates_color_cache(self):
"""Test that color cache is initialized."""
collector = HeatmapCollector(sample_interval_usec=100)
self.assertIsInstance(collector._color_cache, dict)
self.assertEqual(len(collector._color_cache), 0)
def test_init_gets_path_info(self):
"""Test that path info is retrieved during init."""
collector = HeatmapCollector(sample_interval_usec=100)
self.assertIsNotNone(collector._path_info)
self.assertIn('stdlib', collector._path_info)
class TestHeatmapCollectorSetStats(unittest.TestCase):
"""Test HeatmapCollector.set_stats() method."""
def test_set_stats_stores_all_parameters(self):
"""Test that set_stats stores all provided parameters."""
collector = HeatmapCollector(sample_interval_usec=100)
collector.set_stats(
sample_interval_usec=500,
duration_sec=10.5,
sample_rate=99.5,
error_rate=0.5
)
self.assertEqual(collector.stats['sample_interval_usec'], 500)
self.assertEqual(collector.stats['duration_sec'], 10.5)
self.assertEqual(collector.stats['sample_rate'], 99.5)
self.assertEqual(collector.stats['error_rate'], 0.5)
def test_set_stats_includes_system_info(self):
"""Test that set_stats includes Python and platform info."""
collector = HeatmapCollector(sample_interval_usec=100)
collector.set_stats(sample_interval_usec=100, duration_sec=1.0, sample_rate=100.0)
self.assertIn('python_version', collector.stats)
self.assertIn('python_implementation', collector.stats)
self.assertIn('platform', collector.stats)
def test_set_stats_accepts_kwargs(self):
"""Test that set_stats accepts additional kwargs."""
collector = HeatmapCollector(sample_interval_usec=100)
collector.set_stats(
sample_interval_usec=100,
duration_sec=1.0,
sample_rate=100.0,
custom_key='custom_value',
another_key=42
)
self.assertEqual(collector.stats['custom_key'], 'custom_value')
self.assertEqual(collector.stats['another_key'], 42)
def test_set_stats_with_none_error_rate(self):
"""Test set_stats with error_rate=None."""
collector = HeatmapCollector(sample_interval_usec=100)
collector.set_stats(sample_interval_usec=100, duration_sec=1.0, sample_rate=100.0)
self.assertIn('error_rate', collector.stats)
self.assertIsNone(collector.stats['error_rate'])
class TestHeatmapCollectorProcessFrames(unittest.TestCase):
"""Test HeatmapCollector.process_frames() method."""
def test_process_frames_increments_total_samples(self):
"""Test that process_frames increments total samples count."""
collector = HeatmapCollector(sample_interval_usec=100)
initial_count = collector._total_samples
frames = [('file.py', 10, 'func')]
collector.process_frames(frames, thread_id=1)
self.assertEqual(collector._total_samples, initial_count + 1)
def test_process_frames_records_line_samples(self):
"""Test that process_frames records line samples."""
collector = HeatmapCollector(sample_interval_usec=100)
frames = [('test.py', 5, 'test_func')]
collector.process_frames(frames, thread_id=1)
# Check that line was recorded
self.assertIn(('test.py', 5), collector.line_samples)
self.assertEqual(collector.line_samples[('test.py', 5)], 1)
def test_process_frames_records_multiple_lines_in_stack(self):
"""Test that process_frames records all lines in a stack."""
collector = HeatmapCollector(sample_interval_usec=100)
frames = [
('file1.py', 10, 'func1'),
('file2.py', 20, 'func2'),
('file3.py', 30, 'func3')
]
collector.process_frames(frames, thread_id=1)
# All frames should be recorded
self.assertIn(('file1.py', 10), collector.line_samples)
self.assertIn(('file2.py', 20), collector.line_samples)
self.assertIn(('file3.py', 30), collector.line_samples)
def test_process_frames_distinguishes_self_samples(self):
"""Test that process_frames distinguishes self (leaf) samples."""
collector = HeatmapCollector(sample_interval_usec=100)
frames = [
('leaf.py', 5, 'leaf_func'), # This is the leaf (top of stack)
('caller.py', 10, 'caller_func')
]
collector.process_frames(frames, thread_id=1)
# Leaf should have self sample
self.assertIn(('leaf.py', 5), collector.line_self_samples)
self.assertEqual(collector.line_self_samples[('leaf.py', 5)], 1)
# Caller should NOT have self sample
self.assertNotIn(('caller.py', 10), collector.line_self_samples)
def test_process_frames_accumulates_samples(self):
"""Test that multiple calls accumulate samples."""
collector = HeatmapCollector(sample_interval_usec=100)
frames = [('file.py', 10, 'func')]
collector.process_frames(frames, thread_id=1)
collector.process_frames(frames, thread_id=1)
collector.process_frames(frames, thread_id=1)
self.assertEqual(collector.line_samples[('file.py', 10)], 3)
self.assertEqual(collector._total_samples, 3)
def test_process_frames_ignores_invalid_frames(self):
"""Test that process_frames ignores invalid frames."""
collector = HeatmapCollector(sample_interval_usec=100)
# These should be ignored
invalid_frames = [
('<string>', 1, 'test'),
('[eval]', 1, 'test'),
('', 1, 'test'),
(None, 1, 'test'),
('__init__', 0, 'test'), # Special invalid frame
]
for frame in invalid_frames:
collector.process_frames([frame], thread_id=1)
# Should not record these invalid frames
for frame in invalid_frames:
if frame[0]:
self.assertNotIn((frame[0], frame[1]), collector.line_samples)
def test_process_frames_builds_call_graph(self):
"""Test that process_frames builds call graph relationships."""
collector = HeatmapCollector(sample_interval_usec=100)
frames = [
('callee.py', 5, 'callee_func'),
('caller.py', 10, 'caller_func')
]
collector.process_frames(frames, thread_id=1)
# Check that call relationship was recorded
caller_key = ('caller.py', 10)
self.assertIn(caller_key, collector.call_graph)
# Check callers graph
callee_key = ('callee.py', 5)
self.assertIn(callee_key, collector.callers_graph)
def test_process_frames_records_function_definitions(self):
"""Test that process_frames records function definition locations."""
collector = HeatmapCollector(sample_interval_usec=100)
frames = [('module.py', 42, 'my_function')]
collector.process_frames(frames, thread_id=1)
self.assertIn(('module.py', 'my_function'), collector.function_definitions)
self.assertEqual(collector.function_definitions[('module.py', 'my_function')], 42)
def test_process_frames_tracks_edge_samples(self):
"""Test that process_frames tracks edge sample counts."""
collector = HeatmapCollector(sample_interval_usec=100)
frames = [
('callee.py', 5, 'callee'),
('caller.py', 10, 'caller')
]
# Process same call stack multiple times
collector.process_frames(frames, thread_id=1)
collector.process_frames(frames, thread_id=1)
# Check that edge count is tracked
self.assertGreater(len(collector.edge_samples), 0)
def test_process_frames_handles_empty_frames(self):
"""Test that process_frames handles empty frame list."""
collector = HeatmapCollector(sample_interval_usec=100)
initial_count = collector._total_samples
collector.process_frames([], thread_id=1)
# Should still increment total samples
self.assertEqual(collector._total_samples, initial_count + 1)
def test_process_frames_with_file_samples_dict(self):
"""Test that file_samples dict is properly populated."""
collector = HeatmapCollector(sample_interval_usec=100)
frames = [('test.py', 10, 'func')]
collector.process_frames(frames, thread_id=1)
self.assertIn('test.py', collector.file_samples)
self.assertIn(10, collector.file_samples['test.py'])
self.assertEqual(collector.file_samples['test.py'][10], 1)
class TestHeatmapCollectorExport(unittest.TestCase):
"""Test HeatmapCollector.export() method."""
def setUp(self):
"""Set up test directory."""
self.test_dir = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, self.test_dir)
def test_export_creates_output_directory(self):
"""Test that export creates the output directory."""
collector = HeatmapCollector(sample_interval_usec=100)
# Add some data
frames = [('test.py', 10, 'func')]
collector.process_frames(frames, thread_id=1)
output_path = os.path.join(self.test_dir, 'heatmap_output')
with captured_stdout(), captured_stderr():
collector.export(output_path)
self.assertTrue(os.path.exists(output_path))
self.assertTrue(os.path.isdir(output_path))
def test_export_creates_index_html(self):
"""Test that export creates index.html."""
collector = HeatmapCollector(sample_interval_usec=100)
frames = [('test.py', 10, 'func')]
collector.process_frames(frames, thread_id=1)
output_path = os.path.join(self.test_dir, 'heatmap_output')
with captured_stdout(), captured_stderr():
collector.export(output_path)
index_path = os.path.join(output_path, 'index.html')
self.assertTrue(os.path.exists(index_path))
def test_export_creates_file_htmls(self):
"""Test that export creates individual file HTMLs."""
collector = HeatmapCollector(sample_interval_usec=100)
frames = [('test.py', 10, 'func')]
collector.process_frames(frames, thread_id=1)
output_path = os.path.join(self.test_dir, 'heatmap_output')
with captured_stdout(), captured_stderr():
collector.export(output_path)
# Check for file_XXXX.html files
html_files = [f for f in os.listdir(output_path)
if f.startswith('file_') and f.endswith('.html')]
self.assertGreater(len(html_files), 0)
def test_export_with_empty_data(self):
"""Test export with no data collected."""
collector = HeatmapCollector(sample_interval_usec=100)
output_path = os.path.join(self.test_dir, 'empty_output')
# Should handle empty data gracefully
with captured_stdout(), captured_stderr():
collector.export(output_path)
def test_export_handles_html_suffix(self):
"""Test that export handles .html suffix in output path."""
collector = HeatmapCollector(sample_interval_usec=100)
frames = [('test.py', 10, 'func')]
collector.process_frames(frames, thread_id=1)
# Path with .html suffix should be stripped
output_path = os.path.join(self.test_dir, 'output.html')
with captured_stdout(), captured_stderr():
collector.export(output_path)
# Should create directory without .html
expected_dir = os.path.join(self.test_dir, 'output')
self.assertTrue(os.path.exists(expected_dir))
def test_export_with_multiple_files(self):
"""Test export with multiple files."""
collector = HeatmapCollector(sample_interval_usec=100)
# Add samples for multiple files
collector.process_frames([('file1.py', 10, 'func1')], thread_id=1)
collector.process_frames([('file2.py', 20, 'func2')], thread_id=1)
collector.process_frames([('file3.py', 30, 'func3')], thread_id=1)
output_path = os.path.join(self.test_dir, 'multi_file')
with captured_stdout(), captured_stderr():
collector.export(output_path)
# Should create HTML for each file
html_files = [f for f in os.listdir(output_path)
if f.startswith('file_') and f.endswith('.html')]
self.assertGreaterEqual(len(html_files), 3)
def test_export_index_contains_file_references(self):
"""Test that index.html contains references to profiled files."""
collector = HeatmapCollector(sample_interval_usec=100)
collector.set_stats(sample_interval_usec=100, duration_sec=1.0, sample_rate=100.0)
frames = [('mytest.py', 10, 'my_func')]
collector.process_frames(frames, thread_id=1)
output_path = os.path.join(self.test_dir, 'test_output')
with captured_stdout(), captured_stderr():
collector.export(output_path)
index_path = os.path.join(output_path, 'index.html')
with open(index_path, 'r', encoding='utf-8') as f:
content = f.read()
# Should contain reference to the file
self.assertIn('mytest', content)
def test_export_file_html_has_line_numbers(self):
"""Test that exported file HTML contains line numbers."""
collector = HeatmapCollector(sample_interval_usec=100)
# Create a temporary Python file
temp_file = os.path.join(self.test_dir, 'temp_source.py')
with open(temp_file, 'w') as f:
f.write('def test():\n pass\n')
frames = [(temp_file, 1, 'test')]
collector.process_frames(frames, thread_id=1)
output_path = os.path.join(self.test_dir, 'line_test')
with captured_stdout(), captured_stderr():
collector.export(output_path)
# Find the generated file HTML
html_files = [f for f in os.listdir(output_path)
if f.startswith('file_') and f.endswith('.html')]
if html_files:
with open(os.path.join(output_path, html_files[0]), 'r', encoding='utf-8') as f:
content = f.read()
# Should have line-related content
self.assertIn('line-', content)
class MockFrameInfo:
"""Mock FrameInfo for testing since the real one isn't accessible."""
def __init__(self, filename, lineno, funcname):
self.filename = filename
self.lineno = lineno
self.funcname = funcname
def __repr__(self):
return f"MockFrameInfo(filename='{self.filename}', lineno={self.lineno}, funcname='{self.funcname}')"
class MockThreadInfo:
"""Mock ThreadInfo for testing since the real one isn't accessible."""
def __init__(self, thread_id, frame_info):
self.thread_id = thread_id
self.frame_info = frame_info
def __repr__(self):
return f"MockThreadInfo(thread_id={self.thread_id}, frame_info={self.frame_info})"
class MockInterpreterInfo:
"""Mock InterpreterInfo for testing since the real one isn't accessible."""
def __init__(self, interpreter_id, threads):
self.interpreter_id = interpreter_id
self.threads = threads
def __repr__(self):
return f"MockInterpreterInfo(interpreter_id={self.interpreter_id}, threads={self.threads})"
class TestHeatmapCollector(unittest.TestCase):
"""Tests for HeatmapCollector functionality."""
def test_heatmap_collector_basic(self):
"""Test basic HeatmapCollector functionality."""
collector = HeatmapCollector(sample_interval_usec=100)
# Test empty state
self.assertEqual(len(collector.file_samples), 0)
self.assertEqual(len(collector.line_samples), 0)
# Test collecting sample data
test_frames = [
MockInterpreterInfo(
0,
[MockThreadInfo(
1,
[("file.py", 10, "func1"), ("file.py", 20, "func2")],
)]
)
]
collector.collect(test_frames)
# Should have recorded samples for the file
self.assertGreater(len(collector.line_samples), 0)
self.assertIn("file.py", collector.file_samples)
# Check that line samples were recorded
file_data = collector.file_samples["file.py"]
self.assertGreater(len(file_data), 0)
def test_heatmap_collector_export(self):
"""Test heatmap HTML export functionality."""
heatmap_dir = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, heatmap_dir)
collector = HeatmapCollector(sample_interval_usec=100)
# Create test data with multiple files
test_frames1 = [
MockInterpreterInfo(
0,
[MockThreadInfo(1, [("file.py", 10, "func1"), ("file.py", 20, "func2")])],
)
]
test_frames2 = [
MockInterpreterInfo(
0,
[MockThreadInfo(1, [("file.py", 10, "func1"), ("file.py", 20, "func2")])],
)
] # Same stack
test_frames3 = [
MockInterpreterInfo(0, [MockThreadInfo(1, [("other.py", 5, "other_func")])])
]
collector.collect(test_frames1)
collector.collect(test_frames2)
collector.collect(test_frames3)
# Export heatmap
with (captured_stdout(), captured_stderr()):
collector.export(heatmap_dir)
# Verify index.html was created
index_path = os.path.join(heatmap_dir, "index.html")
self.assertTrue(os.path.exists(index_path))
self.assertGreater(os.path.getsize(index_path), 0)
# Check index contains HTML content
with open(index_path, "r", encoding="utf-8") as f:
content = f.read()
# Should be valid HTML
self.assertIn("<!doctype html>", content.lower())
self.assertIn("<html", content)
self.assertIn("Tachyon Profiler", content)
# Should contain file references
self.assertIn("file.py", content)
self.assertIn("other.py", content)
# Verify individual file HTMLs were created
file_htmls = [f for f in os.listdir(heatmap_dir) if f.startswith("file_") and f.endswith(".html")]
self.assertGreater(len(file_htmls), 0)
# Check one of the file HTMLs
file_html_path = os.path.join(heatmap_dir, file_htmls[0])
with open(file_html_path, "r", encoding="utf-8") as f:
file_content = f.read()
# Should contain heatmap styling and JavaScript
self.assertIn("line-sample", file_content)
self.assertIn("nav-btn", file_content)
if __name__ == "__main__":
unittest.main()

View file

@ -2578,6 +2578,9 @@ LIBSUBDIRS= asyncio \
profile \
profiling profiling/sampling profiling/tracing \
profiling/sampling/_assets \
profiling/sampling/_heatmap_assets \
profiling/sampling/_flamegraph_assets \
profiling/sampling/_shared_assets \
profiling/sampling/live_collector \
profiling/sampling/_vendor/d3/7.8.5 \
profiling/sampling/_vendor/d3-flame-graph/4.1.3 \

View file

@ -0,0 +1,4 @@
Add heatmap visualization mode to the Tachyon sampling profiler. The new
``--heatmap`` output format provides a line-by-line view showing execution
intensity with color-coded samples, inline statistics, and interactive call
graph navigation between callers and callees.