-
Notifications
You must be signed in to change notification settings - Fork 0
Improve performance for unset values and add performance checker example #110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c04a7c6
improve performance for unset values and add performance checker example
6b8cadc
refactor: add type hints to measure_get_value_time function
0a65ebc
feat: add performance measurement for panel value setting and getting
6660907
cleanup
8a8d645
feedback
ae872ac
cleanup
db10e5d
use timeit for performance measurements
2f936dc
refactor: add return type annotations to performance checker functions
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| # Performance checker Example | ||
|
|
||
| This example demonstrates using nipanel with Streamlit to display a dynamic sine wave using the `streamlit-echarts` library. | ||
|
|
||
| ## Features | ||
|
|
||
| - Generates sine wave data with varying frequency | ||
| - Displays the data in an chart | ||
| - Updates rapidly | ||
| - Shows timing information | ||
|
|
||
| ### Required Software | ||
|
|
||
| - Python 3.9 or later | ||
|
|
||
| ### Usage | ||
|
|
||
| Run `poetry run python examples/performance_checker/performance_checker.py` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| """Example of using nipanel to display a sine wave graph using st_echarts.""" | ||
|
|
||
| import math | ||
| import time | ||
| from pathlib import Path | ||
|
|
||
| import numpy as np | ||
|
|
||
| import nipanel | ||
|
|
||
|
|
||
| panel_script_path = Path(__file__).with_name("performance_checker_panel.py") | ||
| panel = nipanel.create_panel(panel_script_path) | ||
|
|
||
| amplitude = 1.0 | ||
| frequency = 1.0 | ||
| num_points = 1000 | ||
| try: | ||
| print(f"Panel URL: {panel.panel_url}") | ||
| print("Press Ctrl+C to exit") | ||
|
|
||
| # Generate and update the sine wave data as fast as possible | ||
| while True: | ||
| time_points = np.linspace(0, num_points, num_points) | ||
| sine_values = amplitude * np.sin(frequency * time_points) | ||
|
|
||
| panel.set_value("time_points", time_points.tolist()) | ||
| panel.set_value("sine_values", sine_values.tolist()) | ||
| panel.set_value("amplitude", amplitude) | ||
| panel.set_value("frequency", frequency) | ||
|
|
||
| # Slowly vary the frequency for a more dynamic visualization | ||
| frequency = 1.0 + 0.5 * math.sin(time.time() / 5.0) | ||
|
|
||
| except KeyboardInterrupt: | ||
| print("Exiting...") |
121 changes: 121 additions & 0 deletions
121
examples/performance_checker/performance_checker_panel.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| """A Streamlit visualization panel for the perf_check.py example script.""" | ||
|
|
||
| import statistics | ||
| import time | ||
| from typing import Any, Tuple | ||
|
|
||
| import streamlit as st | ||
| from streamlit_echarts import st_echarts | ||
|
|
||
| import nipanel | ||
|
|
||
|
|
||
| def measure_get_value_time( | ||
| panel: "nipanel.StreamlitPanelValueAccessor", value_id: str, default_value: Any = None | ||
| ) -> Tuple[Any, float]: | ||
| """Measure the time it takes to get a value from the panel. | ||
|
|
||
| Args: | ||
| panel: The panel accessor object | ||
| value_id: The ID of the value to get | ||
| default_value: Default value if the value is not found | ||
|
|
||
| Returns: | ||
| A tuple of (value, time_ms) where time_ms is the time in milliseconds | ||
| """ | ||
| start_time = time.time() | ||
| value = panel.get_value(value_id, default_value) | ||
| end_time = time.time() | ||
| time_ms = (end_time - start_time) * 1000 | ||
| return value, time_ms | ||
|
|
||
|
|
||
| st.set_page_config(page_title="Performance Checker Example", page_icon="📈", layout="wide") | ||
| st.title("Performance Checker Example") | ||
|
|
||
| # Initialize refresh history list if it doesn't exist | ||
| if "refresh_history" not in st.session_state: | ||
| st.session_state.refresh_history = [] | ||
|
|
||
| # Store current timestamp and calculate time since last refresh | ||
| current_time = time.time() | ||
| if "last_refresh_time" not in st.session_state: | ||
| st.session_state.last_refresh_time = current_time | ||
| time_since_last_refresh = 0.0 | ||
| else: | ||
| time_since_last_refresh = (current_time - st.session_state.last_refresh_time) * 1000 | ||
| st.session_state.last_refresh_time = current_time | ||
|
|
||
| # Store the last 10 refresh times | ||
| st.session_state.refresh_history.append(time_since_last_refresh) | ||
| if len(st.session_state.refresh_history) > 10: | ||
| st.session_state.refresh_history.pop(0) | ||
mikeprosserni marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| panel = nipanel.get_panel_accessor() | ||
|
|
||
| # Measure time to get each value | ||
| time_points, time_points_ms = measure_get_value_time(panel, "time_points", [0.0]) | ||
| sine_values, sine_values_ms = measure_get_value_time(panel, "sine_values", [0.0]) | ||
| amplitude, amplitude_ms = measure_get_value_time(panel, "amplitude", 1.0) | ||
| frequency, frequency_ms = measure_get_value_time(panel, "frequency", 1.0) | ||
| unset_value, unset_value_ms = measure_get_value_time(panel, "unset_value", "default") | ||
|
|
||
| if st.session_state.refresh_history: | ||
| history = st.session_state.refresh_history | ||
| else: | ||
| history = [] | ||
|
|
||
| # Calculate statistics | ||
| min_time = min(history) if history else 0 | ||
| max_time = max(history) if history else 0 | ||
| avg_time = statistics.mean(history) if history else 0 | ||
|
|
||
| # Prepare data for echarts | ||
| data = [{"value": [x, y]} for x, y in zip(time_points, sine_values)] | ||
|
|
||
| # Configure the chart options | ||
| options = { | ||
| "animation": False, # Disable animation for smoother updates | ||
| "title": {"text": "Sine Wave"}, | ||
| "tooltip": {"trigger": "axis"}, | ||
| "xAxis": {"type": "value", "name": "Time (s)", "nameLocation": "middle", "nameGap": 30}, | ||
| "yAxis": { | ||
| "type": "value", | ||
| "name": "Amplitude", | ||
| "nameLocation": "middle", | ||
| "nameGap": 30, | ||
| }, | ||
| "series": [ | ||
| { | ||
| "data": data, | ||
| "type": "line", | ||
| "showSymbol": True, | ||
| "smooth": True, | ||
| "lineStyle": {"width": 2, "color": "#1f77b4"}, | ||
| "areaStyle": {"color": "#1f77b4", "opacity": 0.3}, | ||
| "name": "Sine Wave", | ||
| } | ||
| ], | ||
| } | ||
|
|
||
| # Display the chart | ||
| st_echarts(options=options, height="400px", key="graph") | ||
|
|
||
| # Create columns for metrics | ||
| col1, col2, col3, col4 = st.columns(4) | ||
| with col1: | ||
| st.metric("Amplitude", f"{amplitude:.2f}") | ||
| st.metric("Frequency", f"{frequency:.2f} Hz") | ||
| with col2: | ||
| st.metric("Refresh Time", f"{time_since_last_refresh:.1f} ms") | ||
| st.metric("Min Refresh Time", f"{min_time:.1f} ms") | ||
| st.metric("Max Refresh Time", f"{max_time:.1f} ms") | ||
| st.metric("Avg Refresh Time", f"{avg_time:.1f} ms") | ||
|
|
||
| with col3: | ||
| st.metric("get time_points", f"{time_points_ms:.1f} ms") | ||
| st.metric("get sine_values", f"{sine_values_ms:.1f} ms") | ||
| st.metric("get amplitude", f"{amplitude_ms:.1f} ms") | ||
| st.metric("get frequency", f"{frequency_ms:.1f} ms") | ||
| with col4: | ||
| st.metric("get unset_value", f"{unset_value_ms:.1f} ms") | ||
mikeprosserni marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.