Skip to content

Commit a276b42

Browse files
committed
Fix some edge cases
1 parent dca0f8a commit a276b42

2 files changed

Lines changed: 16 additions & 14 deletions

File tree

Lib/profiling/sampling/flamegraph.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ function updateStatusBar(nodeData, rootValue) {
167167
const filename = resolveString(nodeData.filename) || "";
168168
const lineno = nodeData.lineno;
169169
const timeMs = (nodeData.value / 1000).toFixed(2);
170-
const percent = ((nodeData.value / rootValue) * 100).toFixed(1);
170+
const percent = rootValue > 0 ? ((nodeData.value / rootValue) * 100).toFixed(1) : "0.0";
171171

172172
const locationEl = document.getElementById('status-location');
173173
const funcItem = document.getElementById('status-func-item');
@@ -890,7 +890,7 @@ function filterByThread() {
890890
if (selectedThread === 'all') {
891891
filteredData = originalData;
892892
} else {
893-
selectedThreadId = parseInt(selectedThread);
893+
selectedThreadId = parseInt(selectedThread, 10);
894894
filteredData = filterDataByThread(originalData, selectedThreadId);
895895

896896
if (filteredData.strings) {
@@ -956,17 +956,19 @@ function resetZoom() {
956956

957957
function exportSVG() {
958958
const svgElement = document.querySelector("#chart svg");
959-
if (svgElement) {
960-
const serializer = new XMLSerializer();
961-
const svgString = serializer.serializeToString(svgElement);
962-
const blob = new Blob([svgString], { type: "image/svg+xml" });
963-
const url = URL.createObjectURL(blob);
964-
const a = document.createElement("a");
965-
a.href = url;
966-
a.download = "python-performance-flamegraph.svg";
967-
a.click();
968-
URL.revokeObjectURL(url);
959+
if (!svgElement) {
960+
console.warn("Cannot export: No flamegraph SVG found");
961+
return;
969962
}
963+
const serializer = new XMLSerializer();
964+
const svgString = serializer.serializeToString(svgElement);
965+
const blob = new Blob([svgString], { type: "image/svg+xml" });
966+
const url = URL.createObjectURL(blob);
967+
const a = document.createElement("a");
968+
a.href = url;
969+
a.download = "python-performance-flamegraph.svg";
970+
a.click();
971+
URL.revokeObjectURL(url);
970972
}
971973

972974
// ============================================================================

Lib/profiling/sampling/sample.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,10 @@ def sample(self, collector, duration_sec=10):
112112
if self.realtime_stats and len(self.sample_intervals) > 0:
113113
print() # Add newline after real-time stats
114114

115-
sample_rate = num_samples / running_time
115+
sample_rate = num_samples / running_time if running_time > 0 else 0
116116
error_rate = (errors / num_samples) * 100 if num_samples > 0 else 0
117117
expected_samples = int(duration_sec / sample_interval_sec)
118-
missed_samples = (expected_samples - num_samples) / expected_samples * 100
118+
missed_samples = (expected_samples - num_samples) / expected_samples * 100 if expected_samples > 0 else 0
119119

120120
# Don't print stats for live mode (curses is handling display)
121121
is_live_mode = LiveStatsCollector is not None and isinstance(collector, LiveStatsCollector)

0 commit comments

Comments
 (0)