NAC_Blockchain/nac-integration-tests/tests/performance/stability_test.rs

273 lines
7.3 KiB
Rust
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/// 性能测试:稳定性测试
///
/// 测试系统长时间运行的稳定性
use nac_integration_tests::common::*;
#[tokio::test]
#[ignore] // 长时间测试,默认忽略
async fn test_24_hour_stability() {
init_test_env();
log::info!("Starting 24-hour stability test");
let duration_hours = 24;
let target_duration = std::time::Duration::from_secs(duration_hours * 3600);
let start = std::time::Instant::now();
let mut iteration = 0;
let mut total_tx = 0;
while start.elapsed() < target_duration {
iteration += 1;
// 每次迭代处理1000个交易
for i in 0..1000 {
let tx = create_test_transaction(
(i % 50) as u8,
((i + 1) % 50) as u8,
100,
);
assert_transaction_valid(&tx);
total_tx += 1;
}
// 每小时记录一次
if iteration % 3600 == 0 {
let elapsed_hours = start.elapsed().as_secs() / 3600;
log::info!("Hour {}: {} transactions processed", elapsed_hours, total_tx);
}
// 短暂休息
tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
}
let actual_duration = start.elapsed();
log::info!("24-hour stability test completed");
log::info!("Total duration: {:?}", actual_duration);
log::info!("Total transactions: {}", total_tx);
log::info!("Total iterations: {}", iteration);
log::info!("24-hour stability test passed");
}
#[tokio::test]
async fn test_memory_leak_detection() {
init_test_env();
log::info!("Starting memory leak detection test");
// 重复创建和销毁对象,检测内存泄漏
let iterations = 1000;
for i in 0..iterations {
// 创建账户
let accounts = create_test_accounts(100, 1000);
assert_eq!(accounts.len(), 100);
// 创建交易
let mut transactions = Vec::new();
for j in 0..100 {
let tx = create_test_transaction(
(j % 50) as u8,
((j + 1) % 50) as u8,
100,
);
transactions.push(tx);
}
// 对象离开作用域,应该被释放
drop(accounts);
drop(transactions);
if i % 100 == 0 {
log::debug!("Iteration {}/{}", i, iterations);
}
}
log::info!("Memory leak detection test passed");
}
#[tokio::test]
async fn test_error_recovery() {
init_test_env();
log::info!("Starting error recovery test");
let iterations = 1000;
let mut success_count = 0;
let mut error_count = 0;
for i in 0..iterations {
// 模拟正常交易
if i % 10 != 0 {
let tx = create_test_transaction(0, 1, 100);
assert_transaction_valid(&tx);
success_count += 1;
} else {
// 模拟错误10%错误率)
error_count += 1;
}
}
log::info!("Success: {}, Errors: {}", success_count, error_count);
assert_eq!(success_count + error_count, iterations);
// 验证系统从错误中恢复
let recovery_rate = success_count as f64 / iterations as f64;
assert!(recovery_rate >= 0.9);
log::info!("Error recovery test passed");
}
#[tokio::test]
async fn test_graceful_degradation() {
init_test_env();
log::info!("Starting graceful degradation test");
// 正常负载
let normal_tps = 10000;
let normal_tx = create_test_transaction(0, 1, 100);
assert_transaction_valid(&normal_tx);
// 高负载
let high_load_tx_count = 100000;
let mut high_load_valid = 0;
for i in 0..high_load_tx_count {
let tx = create_test_transaction(
(i % 100) as u8,
((i + 1) % 100) as u8,
100,
);
if tx.amount > 0 {
high_load_valid += 1;
}
}
// 验证系统在高负载下仍能处理交易
let success_rate = high_load_valid as f64 / high_load_tx_count as f64;
assert!(success_rate >= 0.95);
log::info!("Graceful degradation test passed");
}
#[tokio::test]
async fn test_continuous_operation() {
init_test_env();
log::info!("Starting continuous operation test");
// 模拟连续运行1小时
let duration_secs = 60; // 实际测试中可以设置为36001小时
let start = std::time::Instant::now();
let mut total_tx = 0;
while start.elapsed().as_secs() < duration_secs {
// 持续处理交易
for i in 0..100 {
let tx = create_test_transaction(
(i % 50) as u8,
((i + 1) % 50) as u8,
100,
);
assert_transaction_valid(&tx);
total_tx += 1;
}
tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
}
let actual_duration = start.elapsed();
log::info!("Continuous operation test completed");
log::info!("Duration: {:?}", actual_duration);
log::info!("Total transactions: {}", total_tx);
log::info!("Continuous operation test passed");
}
#[tokio::test]
async fn test_restart_recovery() {
init_test_env();
log::info!("Starting restart recovery test");
// 模拟系统运行
let blocks_before = create_test_blockchain(100);
assert_eq!(blocks_before.len(), 100);
// 模拟系统重启
log::info!("Simulating system restart");
// 恢复状态
let blocks_after = create_test_blockchain(100);
assert_eq!(blocks_after.len(), 100);
// 验证状态一致性
assert_eq!(blocks_before.len(), blocks_after.len());
log::info!("Restart recovery test passed");
}
#[tokio::test]
async fn test_data_consistency() {
init_test_env();
log::info!("Starting data consistency test");
// 创建初始状态
let accounts = create_test_accounts(100, 1000);
let initial_total: u64 = accounts.iter().map(|a| a.balance).sum();
// 执行大量交易
let tx_count = 1000;
for i in 0..tx_count {
let _tx = create_test_transaction(
(i % 100) as u8,
((i + 1) % 100) as u8,
10,
);
}
// 验证总量守恒(在真实系统中)
let final_total: u64 = accounts.iter().map(|a| a.balance).sum();
assert_eq!(initial_total, final_total);
log::info!("Data consistency test passed");
}
#[tokio::test]
async fn test_concurrent_stability() {
init_test_env();
log::info!("Starting concurrent stability test");
let concurrent_tasks = 50;
let operations_per_task = 1000;
let mut handles = Vec::new();
for task_id in 0..concurrent_tasks {
let handle = tokio::spawn(async move {
for i in 0..operations_per_task {
let tx = create_test_transaction(
(task_id % 50) as u8,
((task_id + 1) % 50) as u8,
100,
);
assert_transaction_valid(&tx);
}
});
handles.push(handle);
}
// 等待所有任务完成
for handle in handles {
handle.await.expect("mainnet: handle error");
}
log::info!("Concurrent stability test passed");
}