Skip to content

Commit c24d1ea

Browse files
authored
Merge pull request #16 from aws/issue-15
Report profile before sample to avoid incorrect profile end time #15
2 parents ad718c5 + 7e63b3c commit c24d1ea

File tree

4 files changed

+16
-7
lines changed

4 files changed

+16
-7
lines changed

codeguru_profiler_agent/local_aggregator.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ def refresh_configuration(self):
102102
def _report_profile(self, now):
103103
self.last_report_attempted = now
104104
self._add_overhead_metric_to_profile()
105-
self.profile.end = now
106105
logger.info("Attempting to report profile data: " + str(self.profile))
107106
if self.profile.is_empty():
108107
logger.info("Report was cancelled because it was empty")

codeguru_profiler_agent/model/profile.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ def end(self):
4343
@end.setter
4444
def end(self, value):
4545
self._validate_positive_number(value)
46-
if value <= self.start:
46+
if value < self.start:
4747
raise ValueError(
48-
"Profile end value must be bigger than {}, got {}".format(self.start, value))
48+
"Profile end value must be greater than or equal to {}, got {}".format(self.start, value))
4949
self._end = value
5050
# this is the total cpu time spent in this application since start, not just the overhead
5151
self.cpu_time_seconds = time.process_time() - self._start_process_time
@@ -63,7 +63,7 @@ def get_active_millis_since_start(self):
6363

6464
def add(self, sample):
6565
"""
66-
Merge Sample into the call graph.
66+
Merge Sample into the call graph and update profile end time pointing to the last sample time.
6767
"""
6868
self.total_attempted_sample_threads_count += \
6969
sample.attempted_sample_threads_count
@@ -74,6 +74,8 @@ def add(self, sample):
7474
for stack in sample.stacks:
7575
self._insert_stack(stack)
7676

77+
self.end = current_milli_time(clock=self._clock)
78+
7779
def set_overhead_ms(self, duration_timedelta):
7880
"""
7981
The overhead is the total cpu time spent profiling since start. It is measured by a Timer object and only passed
@@ -138,6 +140,6 @@ def average_thread_weight(self):
138140
def __str__(self):
139141
return "Profile(profiling_group_name=" + self.profiling_group_name \
140142
+ ", start=" + to_iso(self.start) \
141-
+ ', end=' + to_iso(self.end) \
143+
+ ', end=' + "none" if self.end is None else to_iso(self.end) \
142144
+ ', duration_ms=' + str(self.get_active_millis_since_start()) \
143145
+ ')'

codeguru_profiler_agent/profiler_runner.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,10 @@ def _run_profiler(self):
8686

8787
# after the refresh we may be working on a profile
8888
if self.is_profiling_in_progress:
89-
sample = self.sampler.sample()
90-
self.collector.add(sample)
9189
if self.collector.flush():
9290
self.is_profiling_in_progress = False
91+
sample = self.sampler.sample()
92+
self.collector.add(sample)
9393
return True
9494

9595
def is_running(self):

test/unit/model/test_profile.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class TestAdd(TestProfile):
3232
@before
3333
def before(self):
3434
super().before()
35+
self.turn_clock(1)
3536

3637
@pytest.mark.parametrize("stacks, expected", [
3738
([[Frame("method_one"), Frame("method_two"), Frame("method_three")]], {
@@ -254,6 +255,12 @@ def test_add_stack(self, stacks, expected):
254255

255256
assert (_convert_profile_into_dict(self.subject) == expected)
256257

258+
def test_add_stack_set_profile_end(self):
259+
self.subject.add(Sample(stacks=[[Frame("frame1")]], attempted_sample_threads_count=12))
260+
test_end_time = self.subject.start + 1000
261+
assert self.subject.end == test_end_time
262+
263+
257264
def test_it_keeps_the_total_sum_of_the_attempted_sample_threads_count_values(
258265
self):
259266
sample1 = Sample(stacks=[[Frame("frame1")]], attempted_sample_threads_count=12)
@@ -454,6 +461,7 @@ class TestGetAverageThreadWeight(TestProfile):
454461
@before
455462
def before(self):
456463
super().before()
464+
self.turn_clock(1)
457465

458466
def test_it_returns_the_average_thread_weight_for_the_samples_in_the_profile(
459467
self):

0 commit comments

Comments
 (0)