22
33import statistics
44import time
5+ import timeit
6+ from functools import partial
57from typing import Any , Tuple
68
79import streamlit as st
1113
1214
1315def profile_get_value (
14- panel : "nipanel.StreamlitPanelValueAccessor" , value_id : str , default_value : Any = None
16+ panel : "nipanel.StreamlitPanelValueAccessor" ,
17+ value_id : str ,
18+ default_value : Any = None ,
19+ num_runs : int = 5 ,
1520) -> Tuple [Any , float ]:
1621 """Measure the time it takes to get a value from the panel.
1722
1823 Args:
1924 panel: The panel accessor object
2025 value_id: The ID of the value to get
2126 default_value: Default value if the value is not found
27+ num_runs: Number of runs for timing
2228
2329 Returns:
2430 A tuple of (value, time_ms) where time_ms is the time in milliseconds
2531 """
26- start_time = time .time ()
2732 value = panel .get_value (value_id , default_value )
28- end_time = time . time ( )
29- time_ms = ( end_time - start_time ) * 1000
33+ get_value_func = partial ( panel . get_value , value_id , default_value )
34+ time_ms = timeit . timeit ( get_value_func , number = num_runs ) * 1000 / num_runs
3035 return value , time_ms
3136
3237
3338st .set_page_config (page_title = "Performance Checker Example" , page_icon = "📈" , layout = "wide" )
3439st .title ("Performance Checker Example" )
3540
36- # Initialize refresh history list if it doesn't exist
3741if "refresh_history" not in st .session_state :
3842 st .session_state .refresh_history = [] # List of tuples (timestamp, refresh_time_ms)
3943
@@ -61,24 +65,21 @@ def profile_get_value(
6165else :
6266 refresh_history = []
6367
64- # Calculate statistics for refresh
6568min_refresh_time = min (refresh_history ) if refresh_history else 0
6669max_refresh_time = max (refresh_history ) if refresh_history else 0
6770avg_refresh_time = statistics .mean (refresh_history ) if refresh_history else 0
6871
6972panel = nipanel .get_panel_accessor ()
7073
71- # Measure time to get each value
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" )
74+ num_timing_runs = 5
75+ time_points , time_points_ms = profile_get_value (panel , "time_points" , [0.0 ], num_timing_runs )
76+ sine_values , sine_values_ms = profile_get_value (panel , "sine_values" , [0.0 ], num_timing_runs )
77+ amplitude , amplitude_ms = profile_get_value (panel , "amplitude" , 1.0 , num_timing_runs )
78+ frequency , frequency_ms = profile_get_value (panel , "frequency" , 1.0 , num_timing_runs )
79+ unset_value , unset_value_ms = profile_get_value (panel , "unset_value" , "default" , num_timing_runs )
7780
78- # Prepare data for echarts
7981data = [{"value" : [x , y ]} for x , y in zip (time_points , sine_values )]
8082
81- # Configure the chart options
8283options = {
8384 "animation" : False , # Disable animation for smoother updates
8485 "title" : {"text" : "Sine Wave" },
@@ -103,10 +104,8 @@ def profile_get_value(
103104 ],
104105}
105106
106- # Display the chart
107107st_echarts (options = options , height = "400px" , key = "graph" )
108108
109- # Create columns for metrics
110109col1 , col2 , col3 = st .columns (3 )
111110with col1 :
112111 st .metric ("Amplitude" , f"{ amplitude :.2f} " )
0 commit comments