QuantLib_Pro / pages /15_Testing_original.py
gdukens's picture
Initial HF Space deploy bundle
72fc0ee
Raw History Blame Contribute Delete
17.5 kB
"""
Testing & Validation Dashboard
Comprehensive testing interface:
- Load testing and performance benchmarks
- Model validation results
- Chaos engineering experiments
- Integration test results
"""
import streamlit as st
import pandas as pd
import numpy as np
import plotly.graph_objects as go
import plotly.express as px
from plotly.subplots import make_subplots
import time
# Page config
st.set_page_config(
page_title="Testing & Validation",
page_icon="",
layout="wide",
)
st.title("Testing & Validation Dashboard")
st.markdown("Performance testing, model validation, and chaos engineering")
# Tabs for different testing types
tab1, tab2, tab3, tab4 = st.tabs([
" Load Testing",
" Model Validation",
" Chaos Engineering",
" Integration Tests"
])
# ============================================================================
# Tab 1: Load Testing
# ============================================================================
with tab1:
st.header("Load Testing & Performance Benchmarks")
# Load test configuration
col1, col2, col3 = st.columns(3)
with col1:
n_users = st.slider("Concurrent Users", 10, 200, 50, 10)
with col2:
duration = st.slider("Duration (s)", 10, 300, 60, 10)
with col3:
pattern = st.selectbox("Load Pattern", ["Ramp Up", "Constant", "Spike", "Wave"])
# Scenarios
st.subheader("Test Scenarios")
scenarios_config = {
'Portfolio Optimization': st.checkbox("Portfolio Optimization", value=True),
'VaR Calculation': st.checkbox("VaR Calculation", value=True),
'Options Pricing': st.checkbox("Options Pricing", value=True),
'Regime Detection': st.checkbox("Regime Detection", value=False),
}
active_scenarios = [name for name, enabled in scenarios_config.items() if enabled]
if run_load_test := st.button("â–¶ Run Load Test", type="primary"):
with st.spinner("Running load test..."):
# Simulate load test results
progress = st.progress(0)
for i in range(100):
time.sleep(duration / 100 * 0.01) # Simulate progress
progress.progress(i + 1)
# Generate simulated results
results_data = []
for scenario in active_scenarios:
# Simulate performance metrics
base_latency = {'Portfolio Optimization': 300, 'VaR Calculation': 80,
'Options Pricing': 40, 'Regime Detection': 150}.get(scenario, 100)
total_requests = n_users * (duration // 5) # ~1 request per 5s per user
results_data.append({
'Scenario': scenario,
'Total Requests': total_requests,
'Success Rate (%)': np.random.uniform(98, 100),
'RPS': total_requests / duration,
'Mean (ms)': base_latency * np.random.uniform(0.9, 1.1),
'Median (ms)': base_latency * np.random.uniform(0.85, 0.95),
'P95 (ms)': base_latency * np.random.uniform(1.5, 2.0),
'P99 (ms)': base_latency * np.random.uniform(2.5, 3.5),
})
results_df = pd.DataFrame(results_data)
# Display results
st.success(f" Load test completed in {duration}s")
# Metrics summary
col1, col2, col3, col4 = st.columns(4)
with col1:
total_requests = results_df['Total Requests'].sum()
st.metric("Total Requests", f"{int(total_requests):,}")
with col2:
avg_success = results_df['Success Rate (%)'].mean()
st.metric("Avg Success Rate", f"{avg_success:.1f}%")
with col3:
total_rps = results_df['RPS'].sum()
st.metric("Total RPS", f"{total_rps:.1f}")
with col4:
avg_p95 = results_df['P95 (ms)'].mean()
st.metric("Avg P95 Latency", f"{avg_p95:.0f}ms")
# Results table
st.subheader("Performance Results")
st.dataframe(results_df, use_container_width=True, hide_index=True)
# Latency distribution chart
st.subheader("Latency Distribution")
fig = go.Figure()
for _, row in results_df.iterrows():
# Simulate latency distribution
latencies = np.random.gamma(2, row['Mean (ms)'] / 2, 1000)
fig.add_trace(go.Box(
y=latencies,
name=row['Scenario'],
boxmean='sd',
))
fig.update_layout(
title="Response Time Distribution by Scenario",
yaxis_title="Latency (ms)",
template='plotly_dark',
height=400,
)
st.plotly_chart(fig, use_container_width=True)
# Benchmark comparison
st.subheader("Benchmark Comparison")
benchmarks = {
'Portfolio Optimization': {'P95': 500, 'P99': 1000},
'VaR Calculation': {'P95': 100, 'P99': 200},
'Options Pricing': {'P95': 50, 'P99': 100},
'Regime Detection': {'P95': 200, 'P99': 500},
}
benchmark_results = []
for _, row in results_df.iterrows():
if row['Scenario'] in benchmarks:
bench = benchmarks[row['Scenario']]
benchmark_results.append({
'Scenario': row['Scenario'],
'Metric': 'P95',
'Actual': row['P95 (ms)'],
'Target': bench['P95'],
'Status': '' if row['P95 (ms)'] <= bench['P95'] else '',
})
benchmark_results.append({
'Scenario': row['Scenario'],
'Metric': 'P99',
'Actual': row['P99 (ms)'],
'Target': bench['P99'],
'Status': '' if row['P99 (ms)'] <= bench['P99'] else '',
})
benchmark_df = pd.DataFrame(benchmark_results)
st.dataframe(benchmark_df, use_container_width=True, hide_index=True)
# ============================================================================
# Tab 2: Model Validation
# ============================================================================
with tab2:
st.header("Model Validation Results")
if st.button(" Run Model Validation", type="primary"):
with st.spinner("Validating models..."):
# Simulate validation
time.sleep(2)
# Sample validation results
validation_data = [
{'Model': 'Black-Scholes', 'Test': 'ATM Call Price', 'Expected': 10.45, 'Actual': 10.46, 'Error %': 0.10, 'Passed': True},
{'Model': 'Black-Scholes', 'Test': 'ATM Put Price', 'Expected': 5.57, 'Actual': 5.58, 'Error %': 0.18, 'Passed': True},
{'Model': 'Black-Scholes', 'Test': 'Put-Call Parity', 'Expected': 4.88, 'Actual': 4.88, 'Error %': 0.00, 'Passed': True},
{'Model': 'Monte Carlo', 'Test': 'MC vs BS Call Price', 'Expected': 10.46, 'Actual': 10.52, 'Error %': 0.57, 'Passed': True},
{'Model': 'Black-Scholes', 'Test': 'Call Delta', 'Expected': 0.6368, 'Actual': 0.6371, 'Error %': 0.05, 'Passed': True},
{'Model': 'Black-Scholes', 'Test': 'Gamma', 'Expected': 0.0199, 'Actual': 0.0198, 'Error %': 0.50, 'Passed': True},
{'Model': 'VaR', 'Test': 'Parametric VaR', 'Expected': -0.01645, 'Actual': -0.01652, 'Error %': 0.43, 'Passed': True},
{'Model': 'VaR', 'Test': 'CVaR >= VaR', 'Expected': -0.01645, 'Actual': -0.02103, 'Error %': 27.84, 'Passed': True},
{'Model': 'Portfolio Optimization', 'Test': 'Weights Sum to 1', 'Expected': 1.0, 'Actual': 1.0, 'Error %': 0.00, 'Passed': True},
{'Model': 'Portfolio Optimization', 'Test': 'Positive Sharpe Ratio', 'Expected': 0.5, 'Actual': 1.23, 'Error %': 146.0, 'Passed': True},
]
val_df = pd.DataFrame(validation_data)
# Summary metrics
total_tests = len(val_df)
passed_tests = val_df['Passed'].sum()
pass_rate = passed_tests / total_tests * 100
col1, col2, col3, col4 = st.columns(4)
with col1:
st.metric("Total Tests", total_tests)
with col2:
st.metric("Passed", passed_tests, delta=f"{pass_rate:.1f}%")
with col3:
st.metric("Failed", total_tests - passed_tests)
with col4:
avg_error = val_df['Error %'].mean()
st.metric("Avg Error", f"{avg_error:.2f}%")
# Results by model
st.subheader("Validation Results")
# Style the dataframe
def highlight_passed(row):
if row['Passed']:
return ['background-color: rgba(0, 255, 0, 0.1)'] * len(row)
else:
return ['background-color: rgba(255, 0, 0, 0.1)'] * len(row)
styled_df = val_df.style.apply(highlight_passed, axis=1)
st.dataframe(styled_df, use_container_width=True, hide_index=True)
# Error distribution
st.subheader("Error Distribution")
fig = go.Figure()
fig.add_trace(go.Histogram(
x=val_df['Error %'],
nbinsx=20,
name="Error %",
marker=dict(color='#1f77b4'),
))
fig.update_layout(
title="Validation Error Distribution",
xaxis_title="Error (%)",
yaxis_title="Count",
template='plotly_dark',
height=300,
)
st.plotly_chart(fig, use_container_width=True)
# Pass rate by model
st.subheader("Pass Rate by Model")
model_stats = val_df.groupby('Model').agg({
'Passed': ['sum', 'count']
}).reset_index()
model_stats.columns = ['Model', 'Passed', 'Total']
model_stats['Pass Rate %'] = (model_stats['Passed'] / model_stats['Total'] * 100)
fig2 = go.Figure()
fig2.add_trace(go.Bar(
x=model_stats['Model'],
y=model_stats['Pass Rate %'],
text=model_stats['Pass Rate %'].apply(lambda x: f"{x:.1f}%"),
textposition='auto',
marker=dict(color='#2ecc71'),
))
fig2.update_layout(
title="Pass Rate by Model",
xaxis_title="Model",
yaxis_title="Pass Rate (%)",
template='plotly_dark',
height=300,
)
st.plotly_chart(fig2, use_container_width=True)
# Overall status
if pass_rate >= 90:
st.success(f" VALIDATION PASSED ({pass_rate:.1f}% success rate)")
else:
st.error(f" VALIDATION FAILED ({pass_rate:.1f}% success rate)")
# ============================================================================
# Tab 3: Chaos Engineering
# ============================================================================
with tab3:
st.header("Chaos Engineering Experiments")
st.markdown("""
Test system resilience by injecting controlled faults.
""")
# Fault selection
col1, col2 = st.columns(2)
with col1:
fault_type = st.selectbox(
"Fault Type",
["Network Latency", "Network Timeout", "Data Corruption",
"Service Failure", "Intermittent Failure"]
)
with col2:
intensity = st.slider("Fault Intensity", 0.0, 1.0, 0.5, 0.1)
# Resilience patterns to test
st.subheader("Resilience Patterns")
patterns = {
'Circuit Breaker': st.checkbox("Circuit Breaker", value=True),
'Retry with Backoff': st.checkbox("Retry with Backoff", value=True),
'Fallback': st.checkbox("Fallback", value=True),
'Timeout': st.checkbox("Timeout", value=False),
}
if st.button(" Run Chaos Experiment", type="primary"):
with st.spinner("Running chaos experiment..."):
time.sleep(3)
# Simulate results
results = []
for pattern, enabled in patterns.items():
if enabled:
# Simulate test
passed = np.random.random() > 0.2 # 80% pass rate
results.append({
'Pattern': pattern,
'Fault': fault_type,
'Intensity': intensity,
'Passed': passed,
'Recovery Time (s)': np.random.uniform(0.5, 3.0) if passed else np.nan,
})
results_df = pd.DataFrame(results)
# Display results
st.subheader("Experiment Results")
passed_count = results_df['Passed'].sum()
total_count = len(results_df)
col1, col2, col3 = st.columns(3)
with col1:
st.metric("Patterns Tested", total_count)
with col2:
st.metric("Passed", passed_count)
with col3:
avg_recovery = results_df['Recovery Time (s)'].mean()
st.metric("Avg Recovery Time", f"{avg_recovery:.2f}s" if not np.isnan(avg_recovery) else "N/A")
# Results table
st.dataframe(results_df, use_container_width=True, hide_index=True)
# Overall status
if passed_count == total_count:
st.success(" All resilience patterns passed!")
else:
st.warning(f" {total_count - passed_count} pattern(s) failed")
# ============================================================================
# Tab 4: Integration Tests
# ============================================================================
with tab4:
st.header("Integration Test Results")
if st.button(" Run Integration Tests", type="primary"):
with st.spinner("Running integration tests..."):
time.sleep(2)
# Simulate test results
test_data = [
{'Workflow': 'Portfolio → Risk', 'Tests': 5, 'Passed': 5, 'Duration (s)': 2.3},
{'Workflow': 'Options → Risk', 'Tests': 6, 'Passed': 6, 'Duration (s)': 1.8},
{'Workflow': 'Regime → Rebalancing', 'Tests': 4, 'Passed': 4, 'Duration (s)': 3.1},
{'Workflow': 'Data → Audit', 'Tests': 7, 'Passed': 7, 'Duration (s)': 1.2},
{'Workflow': 'Backtesting → Compliance', 'Tests': 8, 'Passed': 8, 'Duration (s)': 4.5},
]
test_df = pd.DataFrame(test_data)
test_df['Pass Rate %'] = (test_df['Passed'] / test_df['Tests'] * 100)
# Summary
total_tests = test_df['Tests'].sum()
total_passed = test_df['Passed'].sum()
total_duration = test_df['Duration (s)'].sum()
col1, col2, col3, col4 = st.columns(4)
with col1:
st.metric("Total Tests", total_tests)
with col2:
st.metric("Passed", total_passed)
with col3:
st.metric("Pass Rate", f"{total_passed/total_tests*100:.1f}%")
with col4:
st.metric("Total Duration", f"{total_duration:.1f}s")
# Results
st.dataframe(test_df, use_container_width=True, hide_index=True)
# Test duration chart
fig = go.Figure()
fig.add_trace(go.Bar(
x=test_df['Workflow'],
y=test_df['Duration (s)'],
text=test_df['Duration (s)'].apply(lambda x: f"{x:.1f}s"),
textposition='auto',
marker=dict(color='#3498db'),
))
fig.update_layout(
title="Test Duration by Workflow",
xaxis_title="Workflow",
yaxis_title="Duration (seconds)",
template='plotly_dark',
height=300,
)
st.plotly_chart(fig, use_container_width=True)
st.success(" All integration tests passed!")