- 
                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 all 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 measures the performance of a stremlit panel with a graph. | ||
|  | ||
| ## Features | ||
|  | ||
| - Generates sine wave data with varying frequency | ||
| - Displays the data in a graph | ||
| - 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,78 @@ | ||
| """Check the performance of get_value and set_value methods in nipanel.""" | ||
|  | ||
| import math | ||
| import time | ||
| import timeit | ||
| 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 | ||
| time_points = np.linspace(0, num_points, num_points) | ||
| sine_values = amplitude * np.sin(frequency * time_points) | ||
|  | ||
|  | ||
| def _set_time_points() -> None: | ||
| panel.set_value("time_points", time_points.tolist()) | ||
|  | ||
|  | ||
| def _set_amplitude() -> None: | ||
| panel.set_value("amplitude", amplitude) | ||
|  | ||
|  | ||
| def _get_time_points() -> None: | ||
| panel.get_value("time_points", [0.0]) | ||
|  | ||
|  | ||
| def _get_amplitude() -> None: | ||
| panel.get_value("amplitude", 1.0) | ||
|  | ||
|  | ||
| def _get_unset_value() -> None: | ||
| panel.get_value("unset_value", 1.0) | ||
|  | ||
|  | ||
| iterations = 100 | ||
|  | ||
| set_time_points_time = timeit.timeit(_set_time_points, number=iterations) * 1000 / iterations | ||
| print(f"Average time to set 'time_points': {set_time_points_time:.2f} ms") | ||
|  | ||
| set_amplitude_time = timeit.timeit(_set_amplitude, number=iterations) * 1000 / iterations | ||
| print(f"Average time to set 'amplitude': {set_amplitude_time:.2f} ms") | ||
|  | ||
| get_time_points_time = timeit.timeit(_get_time_points, number=iterations) * 1000 / iterations | ||
| print(f"Average time to get 'time_points': {get_time_points_time:.2f} ms") | ||
|  | ||
| get_amplitude_time = timeit.timeit(_get_amplitude, number=iterations) * 1000 / iterations | ||
| print(f"Average time to get 'amplitude': {get_amplitude_time:.2f} ms") | ||
|  | ||
| get_unset_value_time = timeit.timeit(_get_unset_value, number=iterations) * 1000 / iterations | ||
| print(f"Average time to get 'unset_value': {get_unset_value_time:.2f} ms") | ||
|  | ||
| 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...") | 
        
          
  
    
      
          
            125 changes: 125 additions & 0 deletions
          
          125 
        
  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,125 @@ | ||
| """A Streamlit visualization panel for the performance_checker.py example script.""" | ||
|  | ||
| import statistics | ||
| import time | ||
| import timeit | ||
| from functools import partial | ||
| from typing import Any, Tuple | ||
|  | ||
| import streamlit as st | ||
| from streamlit_echarts import st_echarts | ||
|  | ||
| import nipanel | ||
|  | ||
|  | ||
| def profile_get_value( | ||
| panel: "nipanel.StreamlitPanelValueAccessor", | ||
| value_id: str, | ||
| default_value: Any = None, | ||
| num_runs: int = 5, | ||
| ) -> 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 | ||
| num_runs: Number of runs for timing | ||
|  | ||
| Returns: | ||
| A tuple of (value, time_ms) where time_ms is the time in milliseconds | ||
| """ | ||
| value = panel.get_value(value_id, default_value) | ||
| get_value_func = partial(panel.get_value, value_id, default_value) | ||
| time_ms = timeit.timeit(get_value_func, number=num_runs) * 1000 / num_runs | ||
| return value, time_ms | ||
|  | ||
|  | ||
| st.set_page_config(page_title="Performance Checker Example", page_icon="📈", layout="wide") | ||
| st.title("Performance Checker Example") | ||
|  | ||
| if "refresh_history" not in st.session_state: | ||
| st.session_state.refresh_history = [] # List of tuples (timestamp, refresh_time_ms) | ||
|  | ||
| # 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 refresh times with timestamps, keeping only the last 1 second of data | ||
| st.session_state.refresh_history.append((current_time, time_since_last_refresh)) | ||
|  | ||
| # Remove entries older than 1 second | ||
| cutoff_time = current_time - 1.0 # 1 second ago | ||
| st.session_state.refresh_history = [ | ||
| item for item in st.session_state.refresh_history if item[0] >= cutoff_time | ||
| ] | ||
|  | ||
| # Extract just the refresh times for calculations | ||
| if st.session_state.refresh_history: | ||
| refresh_history = [item[1] for item in st.session_state.refresh_history] | ||
| else: | ||
| refresh_history = [] | ||
|  | ||
| min_refresh_time = min(refresh_history) if refresh_history else 0 | ||
| max_refresh_time = max(refresh_history) if refresh_history else 0 | ||
| avg_refresh_time = statistics.mean(refresh_history) if refresh_history else 0 | ||
|  | ||
| panel = nipanel.get_panel_accessor() | ||
|  | ||
| num_timing_runs = 5 | ||
| time_points, time_points_ms = profile_get_value(panel, "time_points", [0.0], num_timing_runs) | ||
| sine_values, sine_values_ms = profile_get_value(panel, "sine_values", [0.0], num_timing_runs) | ||
| amplitude, amplitude_ms = profile_get_value(panel, "amplitude", 1.0, num_timing_runs) | ||
| frequency, frequency_ms = profile_get_value(panel, "frequency", 1.0, num_timing_runs) | ||
| unset_value, unset_value_ms = profile_get_value(panel, "unset_value", "default", num_timing_runs) | ||
|  | ||
| data = [{"value": [x, y]} for x, y in zip(time_points, sine_values)] | ||
|  | ||
| 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", | ||
| } | ||
| ], | ||
| } | ||
|  | ||
| st_echarts(options=options, height="400px", key="graph") | ||
|  | ||
| col1, col2, col3 = st.columns(3) | ||
| 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_refresh_time:.1f} ms") | ||
| st.metric("Max Refresh Time", f"{max_refresh_time:.1f} ms") | ||
| st.metric("Avg Refresh Time", f"{avg_refresh_time:.1f} ms") | ||
| st.metric("FPS", f"{len(refresh_history)}") | ||
|  | ||
| 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") | ||
| st.metric("get unset_value", f"{unset_value_ms:.1f} ms") | ||
  
    
      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
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
  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.