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
11 changes: 8 additions & 3 deletions austin/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ class HierarchicalStats:
leaf is given by those instances with an empty ``children`` attribute.

The ``label`` attribute is used for indexing reasons and therefore should
be of a hashable type.
be of a hashable type. The ``samples`` attribute counts the samples that
contributed to the ``total`` of the node.

This class overrides the default ``add`` operator so that one can perform
operations like ``stats1 + stats2``. Note, however, that instances of this
Expand All @@ -79,6 +80,7 @@ class are not assumed to be immutable and indeed this operation will modify
own: int
total: int
children: Dict[Any, "HierarchicalStats"] = field(default_factory=dict)
samples: int = 0

def __lshift__(self, other: "HierarchicalStats") -> "HierarchicalStats":
"""Merge the RHS into the LHS."""
Expand All @@ -87,6 +89,7 @@ def __lshift__(self, other: "HierarchicalStats") -> "HierarchicalStats":

self.own += other.own
self.total += other.total
self.samples += other.samples

for frame, child in other.children.items():
try:
Expand Down Expand Up @@ -292,13 +295,15 @@ def update(self, sample: AustinSample) -> None:

pid = sample.pid
thread_info = ThreadInfo(sample.thread, sample.iid or 0)
thread_stats = ThreadStats(thread_info, own=0, total=metric)
thread_stats = ThreadStats(thread_info, own=0, total=metric, samples=1)

# Convert the list of frames into a nested FrameStats instance
stats: HierarchicalStats = thread_stats
container = thread_stats.children
for height, frame in enumerate(sample.frames or []):
stats = FrameStats(label=frame, height=height, own=0, total=metric)
stats = FrameStats(
label=frame, height=height, own=0, total=metric, samples=1
)
container[frame] = stats
container = stats.children
stats.own = stats.total
Expand Down
17 changes: 17 additions & 0 deletions test/stats/test_austin_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def test_austin_stats_single_process():
label=ThreadInfo("0x7f45645646", 0),
own=0,
total=152,
samples=1,
children={
AustinFrame(
function="foo", filename="foo_module.py", line=10
Expand All @@ -66,6 +67,7 @@ def test_austin_stats_single_process():
),
own=152,
total=152,
samples=1,
)
},
)
Expand All @@ -84,6 +86,7 @@ def test_austin_stats_single_process():
ThreadInfo("0x7f45645646", 0): ThreadStats(
label=ThreadInfo("0x7f45645646", 0),
total=300,
samples=2,
own=148,
children={
AustinFrame(
Expand All @@ -94,6 +97,7 @@ def test_austin_stats_single_process():
),
own=152,
total=152,
samples=1,
)
},
)
Expand All @@ -112,6 +116,7 @@ def test_austin_stats_single_process():
ThreadInfo("0x7f45645646", 0): ThreadStats(
label=ThreadInfo("0x7f45645646", 0),
total=400,
samples=3,
own=148,
children={
AustinFrame(
Expand All @@ -122,6 +127,7 @@ def test_austin_stats_single_process():
),
own=252,
total=252,
samples=2,
)
},
)
Expand All @@ -140,6 +146,7 @@ def test_austin_stats_single_process():
ThreadInfo("0x7f45645646", 0): ThreadStats(
label=ThreadInfo("0x7f45645646", 0),
total=800,
samples=4,
own=148,
children={
AustinFrame(
Expand All @@ -150,6 +157,7 @@ def test_austin_stats_single_process():
),
own=252,
total=252,
samples=2,
),
AustinFrame(
function="bar", filename="foo_module.py", line=35
Expand All @@ -159,6 +167,7 @@ def test_austin_stats_single_process():
),
own=400,
total=400,
samples=1,
),
},
)
Expand All @@ -178,6 +187,7 @@ def test_austin_stats_single_process():
label=ThreadInfo("0x7f45645664", 0),
own=0,
total=152,
samples=1,
children={
AustinFrame(
function="foo", filename="foo_module.py", line=10
Expand All @@ -187,12 +197,14 @@ def test_austin_stats_single_process():
),
own=152,
total=152,
samples=1,
)
},
),
ThreadInfo("0x7f45645646", 0): ThreadStats(
label=ThreadInfo("0x7f45645646", 0),
total=800,
samples=4,
own=148,
children={
AustinFrame(
Expand All @@ -203,6 +215,7 @@ def test_austin_stats_single_process():
),
own=252,
total=252,
samples=2,
),
AustinFrame(
function="bar", filename="foo_module.py", line=35
Expand All @@ -212,6 +225,7 @@ def test_austin_stats_single_process():
),
own=400,
total=400,
samples=1,
),
},
),
Expand Down Expand Up @@ -305,6 +319,7 @@ def test_load():
label=ThreadInfo("0x7f45645646", 0),
own=0,
total=1300,
samples=2,
children={
AustinFrame(
function="foo", filename="foo_module.py", line=10
Expand All @@ -314,6 +329,7 @@ def test_load():
),
own=300,
total=1300,
samples=2,
children={
AustinFrame(
function="bar",
Expand All @@ -327,6 +343,7 @@ def test_load():
),
own=1000,
total=1000,
samples=1,
children={},
height=1,
)
Expand Down
Loading