Skip to content

Commit 8a8d645

Browse files
Mike ProsserMike Prosser
authored andcommitted
feedback
1 parent 6660907 commit 8a8d645

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

examples/performance_checker/performance_checker_panel.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import nipanel
1111

1212

13-
def measure_get_value_time(
13+
def profile_get_value(
1414
panel: "nipanel.StreamlitPanelValueAccessor", value_id: str, default_value: Any = None
1515
) -> Tuple[Any, float]:
1616
"""Measure the time it takes to get a value from the panel.
@@ -35,7 +35,7 @@ def measure_get_value_time(
3535

3636
# Initialize refresh history list if it doesn't exist
3737
if "refresh_history" not in st.session_state:
38-
st.session_state.refresh_history = []
38+
st.session_state.refresh_history = [] # List of tuples (timestamp, refresh_time_ms)
3939

4040
# Store current timestamp and calculate time since last refresh
4141
current_time = time.time()
@@ -46,13 +46,18 @@ def measure_get_value_time(
4646
time_since_last_refresh = (current_time - st.session_state.last_refresh_time) * 1000
4747
st.session_state.last_refresh_time = current_time
4848

49-
# Store the last 10 refresh times
50-
st.session_state.refresh_history.append(time_since_last_refresh)
51-
if len(st.session_state.refresh_history) > 10:
52-
st.session_state.refresh_history.pop(0)
49+
# Store refresh times with timestamps, keeping only the last 1 second of data
50+
st.session_state.refresh_history.append((current_time, time_since_last_refresh))
5351

52+
# Remove entries older than 1 second
53+
cutoff_time = current_time - 1.0 # 1 second ago
54+
st.session_state.refresh_history = [
55+
item for item in st.session_state.refresh_history if item[0] >= cutoff_time
56+
]
57+
58+
# Extract just the refresh times for calculations
5459
if st.session_state.refresh_history:
55-
refresh_history = st.session_state.refresh_history
60+
refresh_history = [item[1] for item in st.session_state.refresh_history]
5661
else:
5762
refresh_history = []
5863

@@ -64,11 +69,11 @@ def measure_get_value_time(
6469
panel = nipanel.get_panel_accessor()
6570

6671
# Measure time to get each value
67-
time_points, time_points_ms = measure_get_value_time(panel, "time_points", [0.0])
68-
sine_values, sine_values_ms = measure_get_value_time(panel, "sine_values", [0.0])
69-
amplitude, amplitude_ms = measure_get_value_time(panel, "amplitude", 1.0)
70-
frequency, frequency_ms = measure_get_value_time(panel, "frequency", 1.0)
71-
unset_value, unset_value_ms = measure_get_value_time(panel, "unset_value", "default")
72+
time_points, time_points_ms = profile_get_value(panel, "time_points", [0.0])
73+
sine_values, sine_values_ms = profile_get_value(panel, "sine_values", [0.0])
74+
amplitude, amplitude_ms = profile_get_value(panel, "amplitude", 1.0)
75+
frequency, frequency_ms = profile_get_value(panel, "frequency", 1.0)
76+
unset_value, unset_value_ms = profile_get_value(panel, "unset_value", "default")
7277

7378
# Prepare data for echarts
7479
data = [{"value": [x, y]} for x, y in zip(time_points, sine_values)]
@@ -111,11 +116,11 @@ def measure_get_value_time(
111116
st.metric("Min Refresh Time", f"{min_refresh_time:.1f} ms")
112117
st.metric("Max Refresh Time", f"{max_refresh_time:.1f} ms")
113118
st.metric("Avg Refresh Time", f"{avg_refresh_time:.1f} ms")
119+
st.metric("FPS", f"{len(refresh_history)}")
114120

115121
with col3:
116122
st.metric("get time_points", f"{time_points_ms:.1f} ms")
117123
st.metric("get sine_values", f"{sine_values_ms:.1f} ms")
118124
st.metric("get amplitude", f"{amplitude_ms:.1f} ms")
119125
st.metric("get frequency", f"{frequency_ms:.1f} ms")
120-
with col4:
121126
st.metric("get unset_value", f"{unset_value_ms:.1f} ms")

0 commit comments

Comments
 (0)