|  | 
|  | 1 | +"""A Streamlit visualization panel for the perf_check.py example script.""" | 
|  | 2 | + | 
|  | 3 | +import statistics | 
|  | 4 | +import time | 
|  | 5 | + | 
|  | 6 | +import streamlit as st | 
|  | 7 | +from streamlit_echarts import st_echarts | 
|  | 8 | + | 
|  | 9 | +import nipanel | 
|  | 10 | + | 
|  | 11 | + | 
|  | 12 | +def measure_get_value_time(panel, value_id, default_value=None): | 
|  | 13 | +    """Measure the time it takes to get a value from the panel. | 
|  | 14 | +
 | 
|  | 15 | +    Args: | 
|  | 16 | +        panel: The panel accessor object | 
|  | 17 | +        value_id: The ID of the value to get | 
|  | 18 | +        default_value: Default value if the value is not found | 
|  | 19 | +
 | 
|  | 20 | +    Returns: | 
|  | 21 | +        A tuple of (value, time_ms) where time_ms is the time in milliseconds | 
|  | 22 | +    """ | 
|  | 23 | +    start_time = time.time() | 
|  | 24 | +    value = panel.get_value(value_id, default_value) | 
|  | 25 | +    end_time = time.time() | 
|  | 26 | +    time_ms = (end_time - start_time) * 1000 | 
|  | 27 | +    return value, time_ms | 
|  | 28 | + | 
|  | 29 | + | 
|  | 30 | +st.set_page_config(page_title="Performance Checker Example", page_icon="📈", layout="wide") | 
|  | 31 | +st.title("Performance Checker Example") | 
|  | 32 | + | 
|  | 33 | +# Initialize refresh history list if it doesn't exist | 
|  | 34 | +if "refresh_history" not in st.session_state: | 
|  | 35 | +    st.session_state.refresh_history = [] | 
|  | 36 | + | 
|  | 37 | +# Store current timestamp and calculate time since last refresh | 
|  | 38 | +current_time = time.time() | 
|  | 39 | +if "last_refresh_time" not in st.session_state: | 
|  | 40 | +    st.session_state.last_refresh_time = current_time | 
|  | 41 | +    time_since_last_refresh = 0.0 | 
|  | 42 | +else: | 
|  | 43 | +    time_since_last_refresh = (current_time - st.session_state.last_refresh_time) * 1000 | 
|  | 44 | +    st.session_state.last_refresh_time = current_time | 
|  | 45 | + | 
|  | 46 | +    # Store the last 10 refresh times | 
|  | 47 | +    st.session_state.refresh_history.append(time_since_last_refresh) | 
|  | 48 | +    if len(st.session_state.refresh_history) > 10: | 
|  | 49 | +        st.session_state.refresh_history.pop(0) | 
|  | 50 | + | 
|  | 51 | +panel = nipanel.get_panel_accessor() | 
|  | 52 | + | 
|  | 53 | +# Measure time to get each value | 
|  | 54 | +time_points, time_points_ms = measure_get_value_time(panel, "time_points", [0.0]) | 
|  | 55 | +sine_values, sine_values_ms = measure_get_value_time(panel, "sine_values", [0.0]) | 
|  | 56 | +amplitude, amplitude_ms = measure_get_value_time(panel, "amplitude", 1.0) | 
|  | 57 | +frequency, frequency_ms = measure_get_value_time(panel, "frequency", 1.0) | 
|  | 58 | +unset_value, unset_value_ms = measure_get_value_time(panel, "unset_value", "default") | 
|  | 59 | + | 
|  | 60 | +if st.session_state.refresh_history: | 
|  | 61 | +    history = st.session_state.refresh_history | 
|  | 62 | +else: | 
|  | 63 | +    history = [] | 
|  | 64 | + | 
|  | 65 | +# Calculate statistics | 
|  | 66 | +min_time = min(history) if history else 0 | 
|  | 67 | +max_time = max(history) if history else 0 | 
|  | 68 | +avg_time = statistics.mean(history) if history else 0 | 
|  | 69 | + | 
|  | 70 | +# Prepare data for echarts | 
|  | 71 | +data = [{"value": [x, y]} for x, y in zip(time_points, sine_values)] | 
|  | 72 | + | 
|  | 73 | +# Configure the chart options | 
|  | 74 | +options = { | 
|  | 75 | +    "animation": False,  # Disable animation for smoother updates | 
|  | 76 | +    "title": {"text": "Sine Wave"}, | 
|  | 77 | +    "tooltip": {"trigger": "axis"}, | 
|  | 78 | +    "xAxis": {"type": "value", "name": "Time (s)", "nameLocation": "middle", "nameGap": 30}, | 
|  | 79 | +    "yAxis": { | 
|  | 80 | +        "type": "value", | 
|  | 81 | +        "name": "Amplitude", | 
|  | 82 | +        "nameLocation": "middle", | 
|  | 83 | +        "nameGap": 30, | 
|  | 84 | +    }, | 
|  | 85 | +    "series": [ | 
|  | 86 | +        { | 
|  | 87 | +            "data": data, | 
|  | 88 | +            "type": "line", | 
|  | 89 | +            "showSymbol": True, | 
|  | 90 | +            "smooth": True, | 
|  | 91 | +            "lineStyle": {"width": 2, "color": "#1f77b4"}, | 
|  | 92 | +            "areaStyle": {"color": "#1f77b4", "opacity": 0.3}, | 
|  | 93 | +            "name": "Sine Wave", | 
|  | 94 | +        } | 
|  | 95 | +    ], | 
|  | 96 | +} | 
|  | 97 | + | 
|  | 98 | +# Display the chart | 
|  | 99 | +st_echarts(options=options, height="400px", key="graph") | 
|  | 100 | + | 
|  | 101 | +# Create columns for metrics | 
|  | 102 | +col1, col2, col3, col4 = st.columns(4) | 
|  | 103 | +with col1: | 
|  | 104 | +    st.metric("Amplitude", f"{amplitude:.2f}") | 
|  | 105 | +    st.metric("Frequency", f"{frequency:.2f} Hz") | 
|  | 106 | +with col2: | 
|  | 107 | +    st.metric("Refresh Time", f"{time_since_last_refresh:.1f} ms") | 
|  | 108 | +    st.metric("Min Refresh Time", f"{min_time:.1f} ms") | 
|  | 109 | +    st.metric("Max Refresh Time", f"{max_time:.1f} ms") | 
|  | 110 | +    st.metric("Avg Refresh Time", f"{avg_time:.1f} ms") | 
|  | 111 | + | 
|  | 112 | +with col3: | 
|  | 113 | +    st.metric("get time_points", f"{time_points_ms:.1f} ms") | 
|  | 114 | +    st.metric("get sine_values", f"{sine_values_ms:.1f} ms") | 
|  | 115 | +    st.metric("get amplitude", f"{amplitude_ms:.1f} ms") | 
|  | 116 | +    st.metric("get frequency", f"{frequency_ms:.1f} ms") | 
|  | 117 | +with col4: | 
|  | 118 | +    st.metric("get unset_value", f"{unset_value_ms:.1f} ms") | 
0 commit comments