Skip to content

Commit 5035156

Browse files
committed
Fix HZ update
1 parent 00dc8e7 commit 5035156

3 files changed

Lines changed: 23 additions & 42 deletions

File tree

Lib/profiling/sampling/live_collector/collector.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ def __init__(
139139
self._max_sample_rate = 0 # Track maximum sample rate seen
140140
self._successful_samples = 0 # Track samples that captured frames
141141
self._failed_samples = 0 # Track samples that failed to capture frames
142+
self._display_update_interval = DISPLAY_UPDATE_INTERVAL # Instance variable for display refresh rate
142143

143144
# Thread status statistics (bit flags)
144145
self._thread_status_counts = {
@@ -372,7 +373,7 @@ def collect(self, stack_frames):
372373
if (
373374
self._last_display_update is None
374375
or (current_time - self._last_display_update)
375-
>= DISPLAY_UPDATE_INTERVAL
376+
>= self._display_update_interval
376377
):
377378
self._update_display()
378379
self._last_display_update = current_time
@@ -822,17 +823,15 @@ def _handle_input(self):
822823

823824
elif ch == ord("+") or ch == ord("="):
824825
# Decrease update interval (faster refresh)
825-
new_interval = max(
826-
0.05, constants.DISPLAY_UPDATE_INTERVAL - 0.05
826+
self._display_update_interval = max(
827+
0.05, self._display_update_interval - 0.05
827828
) # Min 20Hz
828-
constants.DISPLAY_UPDATE_INTERVAL = new_interval
829829

830830
elif ch == ord("-") or ch == ord("_"):
831831
# Increase update interval (slower refresh)
832-
new_interval = min(
833-
1.0, constants.DISPLAY_UPDATE_INTERVAL + 0.05
832+
self._display_update_interval = min(
833+
1.0, self._display_update_interval + 0.05
834834
) # Max 1Hz
835-
constants.DISPLAY_UPDATE_INTERVAL = new_interval
836835

837836
elif ch == ord("c") or ch == ord("C"):
838837
if self.filter_pattern:

Lib/profiling/sampling/live_collector/widgets.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ def draw_header_info(self, line, width, elapsed):
180180

181181
# Calculate display refresh rate
182182
refresh_hz = (
183-
1.0 / DISPLAY_UPDATE_INTERVAL if DISPLAY_UPDATE_INTERVAL > 0 else 0
183+
1.0 / self.collector._display_update_interval if self.collector._display_update_interval > 0 else 0
184184
)
185185

186186
# Get current view mode and thread display

Lib/test/test_profiling/test_live_collector_interaction.py

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,17 @@ class TestLiveCollectorInteractiveControls(unittest.TestCase):
2929

3030
def setUp(self):
3131
"""Set up collector with mock display."""
32-
from profiling.sampling.live_collector import constants
33-
34-
# Save and reset the display update interval
35-
self._saved_interval = constants.DISPLAY_UPDATE_INTERVAL
36-
constants.DISPLAY_UPDATE_INTERVAL = 0.1
37-
3832
self.display = MockDisplay(height=40, width=160)
3933
self.collector = LiveStatsCollector(
4034
1000, pid=12345, display=self.display
4135
)
4236
self.collector.start_time = time.perf_counter()
37+
# Set a consistent display update interval for tests
38+
self.collector._display_update_interval = 0.1
4339

4440
def tearDown(self):
45-
"""Restore the display update interval."""
46-
from profiling.sampling.live_collector import constants
47-
48-
constants.DISPLAY_UPDATE_INTERVAL = self._saved_interval
41+
"""Clean up after test."""
42+
pass
4943

5044
def test_pause_functionality(self):
5145
"""Test pause/resume functionality."""
@@ -116,53 +110,45 @@ def test_reset_stats(self):
116110

117111
def test_increase_refresh_rate(self):
118112
"""Test increasing refresh rate (faster updates)."""
119-
from profiling.sampling.live_collector import constants
120-
121-
initial_interval = constants.DISPLAY_UPDATE_INTERVAL
113+
initial_interval = self.collector._display_update_interval
122114

123115
# Simulate '+' key press (faster = smaller interval)
124116
self.display.simulate_input(ord("+"))
125117
self.collector._handle_input()
126118

127-
self.assertLess(constants.DISPLAY_UPDATE_INTERVAL, initial_interval)
119+
self.assertLess(self.collector._display_update_interval, initial_interval)
128120

129121
def test_decrease_refresh_rate(self):
130122
"""Test decreasing refresh rate (slower updates)."""
131-
from profiling.sampling.live_collector import constants
132-
133-
initial_interval = constants.DISPLAY_UPDATE_INTERVAL
123+
initial_interval = self.collector._display_update_interval
134124

135125
# Simulate '-' key press (slower = larger interval)
136126
self.display.simulate_input(ord("-"))
137127
self.collector._handle_input()
138128

139-
self.assertGreater(constants.DISPLAY_UPDATE_INTERVAL, initial_interval)
129+
self.assertGreater(self.collector._display_update_interval, initial_interval)
140130

141131
def test_refresh_rate_minimum(self):
142132
"""Test that refresh rate has a minimum (max speed)."""
143-
from profiling.sampling.live_collector import constants
144-
145-
constants.DISPLAY_UPDATE_INTERVAL = 0.05 # Set to minimum
133+
self.collector._display_update_interval = 0.05 # Set to minimum
146134

147135
# Try to go faster
148136
self.display.simulate_input(ord("+"))
149137
self.collector._handle_input()
150138

151139
# Should stay at minimum
152-
self.assertEqual(constants.DISPLAY_UPDATE_INTERVAL, 0.05)
140+
self.assertEqual(self.collector._display_update_interval, 0.05)
153141

154142
def test_refresh_rate_maximum(self):
155143
"""Test that refresh rate has a maximum (min speed)."""
156-
from profiling.sampling.live_collector import constants
157-
158-
constants.DISPLAY_UPDATE_INTERVAL = 1.0 # Set to maximum
144+
self.collector._display_update_interval = 1.0 # Set to maximum
159145

160146
# Try to go slower
161147
self.display.simulate_input(ord("-"))
162148
self.collector._handle_input()
163149

164150
# Should stay at maximum
165-
self.assertEqual(constants.DISPLAY_UPDATE_INTERVAL, 1.0)
151+
self.assertEqual(self.collector._display_update_interval, 1.0)
166152

167153
def test_help_toggle(self):
168154
"""Test help screen toggle."""
@@ -290,27 +276,23 @@ def test_filter_clear_uppercase(self):
290276

291277
def test_increase_refresh_rate_with_equals(self):
292278
"""Test increasing refresh rate with '=' key."""
293-
from profiling.sampling.live_collector import constants
294-
295-
initial_interval = constants.DISPLAY_UPDATE_INTERVAL
279+
initial_interval = self.collector._display_update_interval
296280

297281
# Simulate '=' key press (alternative to '+')
298282
self.display.simulate_input(ord("="))
299283
self.collector._handle_input()
300284

301-
self.assertLess(constants.DISPLAY_UPDATE_INTERVAL, initial_interval)
285+
self.assertLess(self.collector._display_update_interval, initial_interval)
302286

303287
def test_decrease_refresh_rate_with_underscore(self):
304288
"""Test decreasing refresh rate with '_' key."""
305-
from profiling.sampling.live_collector import constants
306-
307-
initial_interval = constants.DISPLAY_UPDATE_INTERVAL
289+
initial_interval = self.collector._display_update_interval
308290

309291
# Simulate '_' key press (alternative to '-')
310292
self.display.simulate_input(ord("_"))
311293
self.collector._handle_input()
312294

313-
self.assertGreater(constants.DISPLAY_UPDATE_INTERVAL, initial_interval)
295+
self.assertGreater(self.collector._display_update_interval, initial_interval)
314296

315297
def test_finished_state_displays_banner(self):
316298
"""Test that finished state shows prominent banner."""

0 commit comments

Comments
 (0)