Skip to content

Commit 37d4be6

Browse files
authored
Merge pull request #18 from aws/issue-15-fix
[Bug fix] Do not sample right after flushing profile
2 parents 6ed00bd + 065208a commit 37d4be6

File tree

4 files changed

+18
-4
lines changed

4 files changed

+18
-4
lines changed

codeguru_profiler_agent/model/profile.py

Lines changed: 2 additions & 2 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 greater than or equal to {}, got {}".format(self.start, value))
48+
"Profile end value must be greater than {}, 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

codeguru_profiler_agent/profiler_runner.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ def _run_profiler(self):
8888
if self.is_profiling_in_progress:
8989
if self.collector.flush():
9090
self.is_profiling_in_progress = False
91+
return True
9192
sample = self.sampler.sample()
9293
self.collector.add(sample)
9394
return True

test/acceptance/test_live_profiling.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,17 @@ def test_live_profiling(self):
6666
start_status = profiler.start()
6767
assert start_status
6868
assert profiler.is_running()
69-
time.sleep(3)
69+
time.sleep(4)
7070
finally:
7171
profiler.stop()
7272

73-
assert wrapped_add.call_count >= 3
73+
# We should see at least 2 samples in 4 seconds as the sequence should happen in the order of
74+
# initial delay (1 second)
75+
# After 1 second, no flush -> sample
76+
# After 2 seconds, it attempt to flush (possibly succeed) -> sample/ no sample
77+
# After 3 seconds, it attempt to flush (must succeed if it did not flush before) -> no sample/ sample
78+
# After 4 seconds, no flush -> sample (if profiler has not stopped yet)
79+
assert wrapped_add.call_count >= 2
7480
assert wrapped_post_agent_profile.call_count >= 1
7581
assert wrapped_configure_agent.call_count >= 1
7682
assert AgentConfiguration.get().sampling_interval == timedelta(seconds=1)

test/unit/test_profiler_runner.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ def test_when_it_is_time_to_report_it_refreshes_configuration_again(self):
7373
self.profiler_runner._profiling_command()
7474
self.mock_collector.refresh_configuration.assert_called_once()
7575

76+
def test_when_it_reports_it_does_not_sample(self):
77+
self.is_time_to_report = True
78+
79+
assert self.profiler_runner._profiling_command()
80+
self.mock_collector.flush.assert_called_once()
81+
self.mock_collector.add.assert_not_called()
82+
7683
def test_when_disabler_say_to_stop(self):
7784
self.mock_disabler.should_stop_profiling.return_value = True
7885
self.profiler_runner._profiling_command()

0 commit comments

Comments
 (0)