Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions profiler/profiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,8 @@ def output_html(
)

if style in ("flamegraph", "icicle"):
if sort_by is not None or limit is not None:
raise ValueError("sort_by and limit only apply to style='table'")
tree = self._extract_call_tree()
doc = self._build_flame_html(tree, title, inverted=(style == "icicle"))
else:
Expand Down Expand Up @@ -357,6 +359,7 @@ def _build_node(
for callee_key, edge in callees.items():
if callee_key in visited:
continue
# edge is (cc,nc,tt,ct) or (nc,ct) depending on CPython version
child_ct = edge[3] if len(edge) == 4 else edge[1]
children.append(
_build_node(callee_key, visited | {callee_key}, child_ct)
Expand Down Expand Up @@ -391,7 +394,8 @@ def _build_flame_html(
"""Build a self-contained flamegraph or icicle chart HTML document."""
import json as _json

total_time = self.total_time or 1e-9
# Use CPU total_tt (same as _extract_call_tree) for consistency
total_time = cast(Any, self._stats).total_tt or 1e-9
style_name = "Icicle Chart" if inverted else "Flamegraph"

return (
Expand Down Expand Up @@ -689,7 +693,6 @@ def _build_table_html(self, rows: list[dict[str, Any]], title: str) -> str:
return COLORS[Math.abs(h)%COLORS.length];
}

var zoomStack=[];
var inverted=container.classList.contains('inverted');

function flatten(nodes,depth,parentLeft,parentWidth,totalTime,arr){
Expand Down Expand Up @@ -747,17 +750,16 @@ def _build_table_html(self, rows: list[dict[str, Any]], title: str) -> str:
}
}

function onFrameClick(e){
function onFrameClick(){
var node=this._node;
zoomStack.push({data:FLAME_DATA,total:TOTAL_TIME});
render([node],node.cumtime);
}

function showTooltip(e){
var el=this;
tooltip.innerHTML='<b>'+escH(el.getAttribute('data-name'))+'</b><br>'
+escH(el.getAttribute('data-file'))+'<br>'
+'Cumulative: '+el.getAttribute('data-cumtime')+'s ('
+'Cumulative (path): '+el.getAttribute('data-cumtime')+'s ('
+el.getAttribute('data-pct')+'%)<br>'
+'Calls: '+el.getAttribute('data-calls');
tooltip.style.display='block';
Expand All @@ -775,7 +777,6 @@ def _build_table_html(self, rows: list[dict[str, Any]], title: str) -> str:
}

document.getElementById('reset-zoom').addEventListener('click',function(){
zoomStack=[];
render(FLAME_DATA,TOTAL_TIME);
});

Expand Down
19 changes: 19 additions & 0 deletions profiler/test_profiler_correctness.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,25 @@ def test_flamegraph_subtitle(self):
assert "Flamegraph" in html
assert "Total time:" in html

def test_flamegraph_empty_profile(self):
with Profiler() as p:
pass
html = p.output_html(style="flamegraph")
assert "<!DOCTYPE html>" in html
assert "FLAME_DATA" in html

def test_flamegraph_sort_by_raises(self):
with Profiler() as p:
_busy_work()
with pytest.raises(ValueError, match="sort_by and limit"):
p.output_html(style="flamegraph", sort_by="tottime")

def test_flamegraph_limit_raises(self):
with Profiler() as p:
_busy_work()
with pytest.raises(ValueError, match="sort_by and limit"):
p.output_html(style="flamegraph", limit=10)


# -- TestIcicleOutput --------------------------------------------------------

Expand Down
Loading