Skip to content

Commit

Permalink
Merge pull request #10 from laixintao/detail-height
Browse files Browse the repository at this point in the history
heap's default sample index set to inuse_space.
  • Loading branch information
laixintao authored Sep 25, 2023
2 parents c050b18 + 33e45cd commit 7ef3be8
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
23 changes: 18 additions & 5 deletions flameshow/render/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ class FlameGraphApp(App):
"""

focused_stack_id = reactive(0)
sample_index = reactive(0, init=False)
sample_index = reactive(None, init=False)

def __init__(
self,
profile,
max_level,
_debug_exit_after_rednder=False,
default_sample_index=0,
default_sample_index=None,
*args,
**kwargs,
):
Expand All @@ -102,7 +102,17 @@ def __init__(

self.filename = self.profile.filename

self.sample_index = default_sample_index
if default_sample_index is not None:
self.sample_index = default_sample_index

self.sample_index = self._choose_default_index(self.profile.sample_types)

def _choose_default_index(self, sample_types):
for index, sample_type in enumerate(sample_types):
if sample_type.sample_type == "inuse_space":
return index

return 0

def on_mount(self):
logger.info("mounted")
Expand All @@ -121,7 +131,6 @@ def compose(self) -> ComposeResult:
RadioButton(f"{s.sample_type}, {s.sample_unit}")
for s in self.profile.sample_types
]
# TODO for heap default to inuse bytes
options[self.sample_index].value = True
radioset = RadioSet(*options, id="sample-type-radio")
detail_row = Horizontal(
Expand Down Expand Up @@ -221,7 +230,11 @@ async def watch_sample_index(self, sample_index):
logger.info("sample index changed to %d", sample_index)

center_text = self._center_header_text(self.sample_index)
header = self.query_one("FlameshowHeader")
try:
header = self.query_one("FlameshowHeader")
except NoMatches:
logger.warning("FlameshowHeader not found, might be not composed yet.")
return
header.center_text = center_text

stack = self.profile.id_store[self.focused_stack_id]
Expand Down
32 changes: 31 additions & 1 deletion tests/test_render.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pytest
from flameshow.parser import ProfileParser
from flameshow.parser import Profile, ProfileParser, SampleType
from flameshow.render import FlameGraphApp
from flameshow.pprof_parser import parse_golang_profile

Expand Down Expand Up @@ -27,3 +27,33 @@ async def test_render_goroutine_child_not_100percent_of_parent(data_dir):
for i in range(369, 379):
child = app.query_one(f"#fg-{i}")
assert child.styles.width.value == 66.67


def test_default_sample_types_heap():
p = Profile()
app = FlameGraphApp(
Profile(),
15,
False,
)
p.sample_types = [
SampleType("alloc_objects", "count"),
SampleType("alloc_space", "bytes"),
SampleType("inuse_objects", "count"),
SampleType("inuse_space", "bytes"),
]
assert app._choose_default_index(p.sample_types) == 3


def test_default_sample_types_profile():
p = Profile()
app = FlameGraphApp(
Profile(),
15,
False,
)
p.sample_types = [
SampleType("samples", "count"),
SampleType("cpu", "nanoseconds"),
]
assert app._choose_default_index(p.sample_types) == 0

0 comments on commit 7ef3be8

Please sign in to comment.