#![ allow( clippy ::std_instead_of_core ) ]
#![ allow( clippy ::float_cmp ) ]
#[ cfg( feature = "integration" ) ]
#[ cfg( feature = "markdown_reports" ) ]
mod tests
{
use benchkit ::prelude :: *;
use std ::collections ::HashMap;
use std ::time :: { Duration, SystemTime };
fn create_sample_results() -> HashMap< String, BenchmarkResult >
{
let mut results = HashMap ::new();
let fast_times = vec![
Duration ::from_micros( 100 ), Duration ::from_micros( 102 ), Duration ::from_micros( 98 ),
Duration ::from_micros( 101 ), Duration ::from_micros( 99 ), Duration ::from_micros( 100 ),
Duration ::from_micros( 103 ), Duration ::from_micros( 97 ), Duration ::from_micros( 101 ),
Duration ::from_micros( 100 ), Duration ::from_micros( 102 ), Duration ::from_micros( 99 )
];
results.insert( "fast_operation".to_string(), BenchmarkResult ::new( "fast_operation", fast_times ) );
let slow_times = vec![
Duration ::from_millis( 10 ), Duration ::from_millis( 15 ), Duration ::from_millis( 8 ),
Duration ::from_millis( 12 ), Duration ::from_millis( 20 ), Duration ::from_millis( 9 )
];
results.insert( "slow_operation".to_string(), BenchmarkResult ::new( "slow_operation", slow_times ) );
results
}
#[ test ]
fn test_performance_report_basic()
{
let results = create_sample_results();
let template = PerformanceReport ::new()
.title( "Test Performance Analysis" )
.add_context( "Comparing fast vs slow operations" );
let report = template.generate( &results ).unwrap();
assert!( report.contains( "# Test Performance Analysis" ) );
assert!( report.contains( "Comparing fast vs slow operations" ) );
assert!( report.contains( "## Executive Summary" ) );
assert!( report.contains( "## Performance Results" ) );
assert!( report.contains( "## Statistical Analysis" ) );
assert!( report.contains( "## Methodology" ) );
assert!( report.contains( "fast_operation" ) );
assert!( report.contains( "slow_operation" ) );
assert!( report.contains( "**Total operations benchmarked** : 2" ) );
}
#[ test ]
fn test_performance_report_with_options()
{
let results = create_sample_results();
let template = PerformanceReport ::new()
.title( "Custom Report" )
.include_statistical_analysis( false )
.include_regression_analysis( true )
.add_custom_section( CustomSection ::new( "Custom Analysis", "This is custom content." ) );
let report = template.generate( &results ).unwrap();
assert!( !report.contains( "## Statistical Analysis" ) );
assert!( report.contains( "## Regression Analysis" ) );
assert!( report.contains( "## Custom Analysis" ) );
assert!( report.contains( "This is custom content." ) );
}
#[ test ]
fn test_comparison_report_basic()
{
let results = create_sample_results();
let template = ComparisonReport ::new()
.title( "Fast vs Slow Comparison" )
.baseline( "slow_operation" )
.candidate( "fast_operation" )
.significance_threshold( 0.05 )
.practical_significance_threshold( 0.10 );
let report = template.generate( &results ).unwrap();
assert!( report.contains( "# Fast vs Slow Comparison" ) );
assert!( report.contains( "## Comparison Summary" ) );
assert!( report.contains( "## Detailed Comparison" ) );
assert!( report.contains( "## Statistical Analysis" ) );
assert!( report.contains( "## Reliability Assessment" ) );
assert!( report.contains( "## Methodology" ) );
assert!( report.contains( "faster" ) );
assert!( report.contains( "fast_operation" ) );
assert!( report.contains( "slow_operation" ) );
}
#[ test ]
fn test_comparison_report_missing_baseline()
{
let results = create_sample_results();
let template = ComparisonReport ::new()
.baseline( "nonexistent_operation" )
.candidate( "fast_operation" );
let result = template.generate( &results );
assert!( result.is_err() );
assert!( result.unwrap_err().to_string().contains( "nonexistent_operation" ) );
}
#[ test ]
fn test_comparison_report_missing_candidate()
{
let results = create_sample_results();
let template = ComparisonReport ::new()
.baseline( "fast_operation" )
.candidate( "nonexistent_operation" );
let result = template.generate( &results );
assert!( result.is_err() );
assert!( result.unwrap_err().to_string().contains( "nonexistent_operation" ) );
}
#[ test ]
fn test_performance_report_empty_results()
{
let results = HashMap ::new();
let template = PerformanceReport ::new();
let report = template.generate( &results ).unwrap();
assert!( report.contains( "No benchmark results available." ) );
assert!( report.contains( "# Performance Analysis" ) );
}
#[ test ]
fn test_custom_section()
{
let section = CustomSection ::new( "Test Section", "Test content with *markdown*." );
assert_eq!( section.title, "Test Section" );
assert_eq!( section.content, "Test content with *markdown*." );
}
#[ test ]
fn test_performance_report_reliability_analysis()
{
let results = create_sample_results();
let template = PerformanceReport ::new()
.include_statistical_analysis( true );
let report = template.generate( &results ).unwrap();
assert!( report.contains( "Reliable Results" ) || report.contains( "Measurements Needing Attention" ) );
assert!( report.contains( "✅" ) || report.contains( "⚠️" ) );
}
#[ test ]
fn test_comparison_report_confidence_intervals()
{
let results = create_sample_results();
let template = ComparisonReport ::new()
.baseline( "slow_operation" )
.candidate( "fast_operation" );
let report = template.generate( &results ).unwrap();
assert!( report.contains( "95% CI" ) );
assert!( report.contains( "Confidence intervals" ) || report.contains( "confidence interval" ) );
assert!( report.contains( "Performance ratio" ) );
assert!( report.contains( "Improvement" ) );
}
#[ test ]
fn test_performance_report_default_values()
{
let template = PerformanceReport ::default();
let results = create_sample_results();
let report = template.generate( &results ).unwrap();
assert!( report.contains( "# Performance Analysis" ) );
assert!( report.contains( "## Statistical Analysis" ) );
assert!( !report.contains( "## Regression Analysis" ) );
}
#[ test ]
fn test_comparison_report_default_values()
{
let template = ComparisonReport ::default();
assert_eq!( template.baseline_name(), "Baseline" );
assert_eq!( template.candidate_name(), "Candidate" );
assert_eq!( template.significance_threshold_value(), 0.05 );
assert_eq!( template.practical_significance_threshold_value(), 0.10 );
}
#[ test ]
fn test_performance_report_with_regression_analysis()
{
let results = create_sample_results();
let mut baseline_data = HashMap ::new();
let baseline_times = vec![
Duration ::from_micros( 120 ), Duration ::from_micros( 118 ), Duration ::from_micros( 122 ),
Duration ::from_micros( 119 ), Duration ::from_micros( 121 ), Duration ::from_micros( 120 ),
Duration ::from_micros( 123 ), Duration ::from_micros( 117 ), Duration ::from_micros( 121 ),
Duration ::from_micros( 120 ), Duration ::from_micros( 122 ), Duration ::from_micros( 119 )
];
baseline_data.insert( "fast_operation".to_string(), BenchmarkResult ::new( "fast_operation", baseline_times ) );
let historical = HistoricalResults ::new()
.with_baseline( baseline_data );
let template = PerformanceReport ::new()
.title( "Performance Report with Regression Analysis" )
.include_regression_analysis( true )
.with_historical_data( historical );
let report = template.generate( &results ).unwrap();
assert!( report.contains( "## Regression Analysis" ) );
assert!( report.contains( "Performance improvement detected" ) || report.contains( "faster than baseline" ) );
assert!( !report.contains( "Not yet implemented" ) );
}
#[ test ]
fn test_regression_analyzer_fixed_baseline_strategy()
{
let results = create_sample_results();
let mut baseline_data = HashMap ::new();
let baseline_times = vec![
Duration ::from_micros( 150 ), Duration ::from_micros( 148 ), Duration ::from_micros( 152 ),
Duration ::from_micros( 149 ), Duration ::from_micros( 151 ), Duration ::from_micros( 150 )
];
baseline_data.insert( "fast_operation".to_string(), BenchmarkResult ::new( "fast_operation", baseline_times ) );
let historical = HistoricalResults ::new()
.with_baseline( baseline_data );
let analyzer = RegressionAnalyzer ::new()
.with_baseline_strategy( BaselineStrategy ::FixedBaseline )
.with_significance_threshold( 0.05 );
let regression_report = analyzer.analyze( &results, &historical );
assert!( regression_report.has_significant_changes() );
assert!( regression_report.get_trend_for( "fast_operation" ) == Some( PerformanceTrend ::Improving ) );
assert!( regression_report.is_statistically_significant( "fast_operation" ) );
}
#[ test ]
fn test_regression_analyzer_rolling_average_strategy()
{
let results = create_sample_results();
let mut historical_runs = Vec ::new();
let mut run1_results = HashMap ::new();
let run1_times = vec![ Duration ::from_micros( 140 ), Duration ::from_micros( 142 ), Duration ::from_micros( 138 ) ];
run1_results.insert( "fast_operation".to_string(), BenchmarkResult ::new( "fast_operation", run1_times ) );
historical_runs.push( TimestampedResults ::new(
SystemTime ::now() - Duration ::from_secs( 604_800 ), run1_results
) );
let mut run2_results = HashMap ::new();
let run2_times = vec![ Duration ::from_micros( 120 ), Duration ::from_micros( 122 ), Duration ::from_micros( 118 ) ];
run2_results.insert( "fast_operation".to_string(), BenchmarkResult ::new( "fast_operation", run2_times ) );
historical_runs.push( TimestampedResults ::new(
SystemTime ::now() - Duration ::from_secs( 86400 ), run2_results
) );
let historical = HistoricalResults ::new()
.with_historical_runs( historical_runs );
let analyzer = RegressionAnalyzer ::new()
.with_baseline_strategy( BaselineStrategy ::RollingAverage )
.with_trend_window( 3 );
let regression_report = analyzer.analyze( &results, &historical );
assert!( regression_report.get_trend_for( "fast_operation" ) == Some( PerformanceTrend ::Improving ) );
assert!( regression_report.has_historical_data( "fast_operation" ) );
}
#[ test ]
fn test_regression_analyzer_previous_run_strategy()
{
let results = create_sample_results();
let mut previous_results = HashMap ::new();
let previous_times = vec![ Duration ::from_micros( 130 ), Duration ::from_micros( 132 ), Duration ::from_micros( 128 ) ];
previous_results.insert( "fast_operation".to_string(), BenchmarkResult ::new( "fast_operation", previous_times ) );
let historical = HistoricalResults ::new()
.with_previous_run( TimestampedResults ::new(
SystemTime ::now() - Duration ::from_secs( 3600 ), previous_results
) );
let analyzer = RegressionAnalyzer ::new()
.with_baseline_strategy( BaselineStrategy ::PreviousRun );
let regression_report = analyzer.analyze( &results, &historical );
assert!( regression_report.get_trend_for( "fast_operation" ) == Some( PerformanceTrend ::Improving ) );
assert!( regression_report.has_previous_run_data() );
}
#[ test ]
fn test_regression_analyzer_statistical_significance()
{
let results = create_sample_results();
let mut baseline_data = HashMap ::new();
let baseline_times = vec![
Duration ::from_micros( 101 ), Duration ::from_micros( 99 ), Duration ::from_micros( 102 ),
Duration ::from_micros( 100 ), Duration ::from_micros( 98 ), Duration ::from_micros( 101 )
];
baseline_data.insert( "fast_operation".to_string(), BenchmarkResult ::new( "fast_operation", baseline_times ) );
let historical = HistoricalResults ::new()
.with_baseline( baseline_data );
let analyzer = RegressionAnalyzer ::new()
.with_significance_threshold( 0.01 );
let regression_report = analyzer.analyze( &results, &historical );
assert!( !regression_report.is_statistically_significant( "fast_operation" ) );
assert!( regression_report.get_trend_for( "fast_operation" ) == Some( PerformanceTrend ::Stable ) );
}
#[ test ]
fn test_regression_report_markdown_output()
{
let results = create_sample_results();
let mut baseline_data = HashMap ::new();
let baseline_times = vec![ Duration ::from_micros( 150 ), Duration ::from_micros( 152 ), Duration ::from_micros( 148 ) ];
baseline_data.insert( "fast_operation".to_string(), BenchmarkResult ::new( "fast_operation", baseline_times ) );
let historical = HistoricalResults ::new()
.with_baseline( baseline_data );
let analyzer = RegressionAnalyzer ::new();
let regression_report = analyzer.analyze( &results, &historical );
let markdown = regression_report.format_markdown();
assert!( markdown.contains( "### Performance Comparison Against Baseline" ) );
assert!( markdown.contains( "### Analysis Summary & Recommendations" ) );
assert!( markdown.contains( "Performance improvement detected" ) );
assert!( markdown.contains( "faster than baseline" ) );
}
}