Skip to content

Commit 071b59c

Browse files
author
Colman Yau
committed
[Bug fix] Do not sample right after flushing profile
There was a bug introduced from Issue #15 where we attempt to sample right after flushing profiles even is_profiling_in_progress has been reset to False already
1 parent c24d1ea commit 071b59c

File tree

4 files changed

+13
-3
lines changed

4 files changed

+13
-3
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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ def test_live_profiling(self):
7070
finally:
7171
profiler.stop()
7272

73-
assert wrapped_add.call_count >= 3
73+
# We should only see 2 sample in 3 seconds as the sequence should happen in the order of
74+
# no flush -> sample -> no flush -> sample -> flush (without sample)
75+
assert wrapped_add.call_count >= 2
7476
assert wrapped_post_agent_profile.call_count >= 1
7577
assert wrapped_configure_agent.call_count >= 1
7678
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_does_not_sample_when_it_reports(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)