#![deny(unsafe_code)]
use zenith_capability::detection::{CapabilitySnapshot, EnvironmentDetector, KernelVersion};
use zenith_capability::profile::{Profile, ProfileFeatures, ProfileSelector};
use zenith_capability::PermissionSet;
fn main() {
println!("Zenith 自动降级与跨平台适配验证");
println!("============================================================");
let mut pass = 0u32;
let mut fail = 0u32;
println!("\n[1] EnvironmentDetector 环境探测");
let detector = EnvironmentDetector::new();
let snapshot = detector.detect();
println!(" 内核版本: {}.{}.{}", snapshot.kernel_version.major, snapshot.kernel_version.minor, snapshot.kernel_version.patch);
println!(" CPU 核数: {}", snapshot.cpu_cores);
println!(" NUMA 节点: {}", snapshot.numa_nodes);
println!(" BPF: {}", snapshot.bpf_supported);
println!(" XDP Native: {}", snapshot.xdp_native_supported);
println!(" XDP Generic: {}", snapshot.xdp_generic_supported);
println!(" AF_XDP: {}", snapshot.af_xdp_supported);
println!(" io_uring: {}", snapshot.io_uring_supported);
println!(" io_uring splice: {}", snapshot.io_uring_splice_sendfile_capable);
println!(" BTF/CO-RE: {}", snapshot.btf_co_re_supported);
println!(" 零拷贝: {}", snapshot.zero_copy_supported);
println!(" 大页: {}", snapshot.hugepage_supported);
println!(" 锁页内存: {} KB", snapshot.locked_memory_kb);
println!(" NIC 驱动: {} ({} 队列)", snapshot.nic_driver, snapshot.nic_queues);
println!(" 命名空间: {}", snapshot.namespace);
println!(" 权限: {} 项", snapshot.capabilities.len());
if snapshot.cpu_cores > 0 {
println!(" [PASS] CPU 核数探测 > 0 ({})", snapshot.cpu_cores);
pass += 1;
} else {
println!(" [FAIL] CPU 核数应为 > 0");
fail += 1;
}
if snapshot.numa_nodes >= 1 {
println!(" [PASS] NUMA 节点 >= 1 ({})", snapshot.numa_nodes);
pass += 1;
} else {
println!(" [FAIL] NUMA 节点应 >= 1");
fail += 1;
}
println!("\n[2] ProfileSelector 自动选择");
let selector = ProfileSelector::new();
let selected = selector.select(&snapshot);
println!(" 自动选择 Profile: {} ({})", selected.as_str(), selected.tier());
let ranked = selector.rank_candidates(&snapshot);
for r in &ranked {
println!(" 候选 {} (tier={}): eligible={} score={} perf={} compat={} resource={}",
r.profile.as_str(), r.profile.tier(), r.eligible, r.score,
r.performance_score, r.compatibility_score, r.resource_score);
}
let selected_ranked = ranked.iter().find(|r| r.profile == selected);
if selected_ranked.map(|r| r.eligible).unwrap_or(false) {
println!(" [PASS] 选中的 Profile {} 是 eligible", selected.as_str());
pass += 1;
} else {
println!(" [FAIL] 选中的 Profile 应 eligible");
fail += 1;
}
let min_ranked = ranked.iter().find(|r| r.profile == Profile::Minimal);
if min_ranked.map(|r| r.eligible).unwrap_or(false) {
println!(" [PASS] Minimal Profile 永远 eligible(跨平台兜底)");
pass += 1;
} else {
println!(" [FAIL] Minimal 应永远 eligible");
fail += 1;
}
println!("\n[3] ProfileFeatures 特性映射");
let features = ProfileFeatures::from_profile(selected, &snapshot);
println!(" xdp_native={}, xdp_generic={}, af_xdp={}", features.xdp_native, features.xdp_generic, features.af_xdp);
println!(" btf_co_re={}, zero_copy={}, bpf={}", features.btf_co_re, features.zero_copy, features.bpf);
println!(" multi_queue={}, huge_pages={}, locked_memory={}", features.multi_queue, features.huge_pages, features.locked_memory);
println!(" basic_socket={}", features.basic_socket);
let min_features = ProfileFeatures::from_profile(Profile::Minimal, &snapshot);
if !min_features.xdp_native && !min_features.af_xdp && !min_features.zero_copy && min_features.basic_socket {
println!(" [PASS] Minimal 模式: 无 XDP/AF_XDP/零拷贝,仅 basic_socket(跨平台兼容)");
pass += 1;
} else {
println!(" [FAIL] Minimal 模式不应依赖 Linux 特性");
fail += 1;
}
let perf_features = ProfileFeatures::from_profile(Profile::Performance, &snapshot);
if perf_features.bpf == snapshot.bpf_supported {
println!(" [PASS] Performance features.bpf 跟随 snapshot(不虚报)");
pass += 1;
} else {
println!(" [FAIL] Performance features.bpf 应跟随 snapshot");
fail += 1;
}
println!("\n[4] Profile 三级降级链");
let chain = [
Profile::Performance.degrade(),
Profile::Performance.degrade().degrade(),
Profile::Performance.degrade().degrade().degrade(),
];
let expected = [Profile::Balanced, Profile::Minimal, Profile::Minimal];
for (i, (actual, exp)) in chain.iter().zip(expected.iter()).enumerate() {
if actual == exp {
println!(" [PASS] 降级链第 {} 步: {:?} → {:?}", i + 1, if i == 0 { "Performance" } else { "down" }, exp.as_str());
pass += 1;
} else {
println!(" [FAIL] 降级链第 {} 步: 期望 {:?},实际 {:?}", i + 1, exp, actual);
fail += 1;
}
}
let degraded = selector.degrade(selected, &snapshot);
println!(" degrade({}) → {}", selected.as_str(), degraded.as_str());
if degraded == Profile::Minimal || degraded == Profile::Balanced {
println!(" [PASS] 降级后的 Profile {} 仍可运行", degraded.as_str());
pass += 1;
} else {
println!(" [FAIL] 降级后的 Profile 应可运行");
fail += 1;
}
println!("\n[5] 低能力环境模拟降级(模拟无 Linux 特性的平台)");
let low_snap = CapabilitySnapshot {
kernel_version: KernelVersion { major: 0, minor: 0, patch: 0, raw: "non-linux".to_string() },
bpf_supported: false,
xdp_native_supported: false,
xdp_generic_supported: false,
af_xdp_supported: false,
io_uring_supported: false,
io_uring_splice_sendfile_capable: false,
btf_co_re_supported: false,
nic_driver: String::new(),
nic_queues: 0,
zero_copy_supported: false,
numa_nodes: 1,
cpu_cores: 2,
locked_memory_kb: 0,
hugepage_supported: false,
namespace: "host".to_string(),
capabilities: PermissionSet::new(),
};
let low_profile = selector.select(&low_snap);
println!(" 低能力环境自动选择: {}", low_profile.as_str());
if low_profile == Profile::Minimal {
println!(" [PASS] 低能力环境自动降级到 Minimal(跨平台兜底)");
pass += 1;
} else {
println!(" [FAIL] 低能力环境应降级到 Minimal,实际: {}", low_profile.as_str());
fail += 1;
}
let low_features = ProfileFeatures::from_profile(low_profile, &low_snap);
if !low_features.xdp_native && !low_features.af_xdp && !low_features.zero_copy && !low_features.bpf && low_features.basic_socket {
println!(" [PASS] 低能力环境 Minimal: 仅 basic_socket,无任何 Linux 特性");
pass += 1;
} else {
println!(" [FAIL] 低能力环境不应有任何 Linux 特性");
fail += 1;
}
println!("\n[6] RuntimeConfig::auto() 自动推导");
let rt_config = zenith_runtime::RuntimeConfig::auto();
println!(" worker_threads: {}", rt_config.worker_threads);
println!(" stack_size: {} bytes", rt_config.stack_size);
println!(" enable_io: {}", rt_config.enable_io);
println!(" enable_time: {}", rt_config.enable_time);
if rt_config.worker_threads >= 1 {
println!(" [PASS] worker_threads >= 1 ({})", rt_config.worker_threads);
pass += 1;
} else {
println!(" [FAIL] worker_threads 应 >= 1");
fail += 1;
}
if rt_config.stack_size >= 4096 {
println!(" [PASS] stack_size >= 4096 ({})", rt_config.stack_size);
pass += 1;
} else {
println!(" [FAIL] stack_size 应 >= 4096");
fail += 1;
}
println!("\n[7] 全局运行时初始化(Fail-Closed 三路径兜底)");
let global = zenith_runtime::init_global(rt_config);
println!(" 全局运行时已初始化: worker_threads={}", global.config().worker_threads);
let result = zenith_runtime::block_on(async {
let task = zenith_runtime::spawn(async { 42 });
task.await.unwrap()
});
if result == Ok(42) {
println!(" [PASS] spawn + block_on 闭环: async {{ 42 }} → Ok(42)");
pass += 1;
} else {
println!(" [FAIL] spawn + block_on 应返回 Ok(42)");
fail += 1;
}
println!("\n[8] 高性能环境模拟(不降级,选择 Performance)");
let high_snap = CapabilitySnapshot {
kernel_version: KernelVersion { major: 6, minor: 1, patch: 0, raw: "6.1.0".to_string() },
bpf_supported: true,
xdp_native_supported: true,
xdp_generic_supported: true,
af_xdp_supported: true,
io_uring_supported: true,
io_uring_splice_sendfile_capable: true,
btf_co_re_supported: true,
nic_driver: "mlx5".to_string(),
nic_queues: 4,
zero_copy_supported: true,
numa_nodes: 2,
cpu_cores: 16,
locked_memory_kb: 262144,
hugepage_supported: true,
namespace: "host".to_string(),
capabilities: PermissionSet::new(),
};
let high_ranked = selector.rank_candidates(&high_snap);
for r in &high_ranked {
println!(" 候选 {} (tier={}): eligible={} score={}",
r.profile.as_str(), r.profile.tier(), r.eligible, r.score);
}
let high_selected = selector.select(&high_snap);
println!(" 高性能环境自动选择: {} (tier={})", high_selected.as_str(), high_selected.tier());
let high_features = ProfileFeatures::from_profile(high_selected, &high_snap);
if high_features.is_performance_ready() {
println!(" [PASS] 高性能环境: is_performance_ready() = true (XDP Native + AF_XDP + 零拷贝)");
pass += 1;
} else {
println!(" [FAIL] 高性能环境应 is_performance_ready()");
fail += 1;
}
println!("\n[9] 部分 Linux 特性环境(Balanced 级别)");
let mid_snap = CapabilitySnapshot {
kernel_version: KernelVersion { major: 5, minor: 4, patch: 0, raw: "5.4.0".to_string() },
bpf_supported: true,
xdp_native_supported: false, xdp_generic_supported: true, af_xdp_supported: false,
io_uring_supported: true,
io_uring_splice_sendfile_capable: false,
btf_co_re_supported: false,
nic_driver: "e1000".to_string(),
nic_queues: 2,
zero_copy_supported: false,
numa_nodes: 1,
cpu_cores: 4,
locked_memory_kb: 8192,
hugepage_supported: false,
namespace: "docker".to_string(),
capabilities: PermissionSet::new(),
};
let mid_selected = selector.select(&mid_snap);
println!(" 中等环境自动选择: {} (tier={})", mid_selected.as_str(), mid_selected.tier());
let mid_ranked = selector.rank_candidates(&mid_snap);
let perf_ranked = mid_ranked.iter().find(|r| r.profile == Profile::Performance);
if perf_ranked.map(|r| !r.eligible).unwrap_or(true) {
println!(" [PASS] 中等环境: Performance 不 eligible(缺少 XDP Native/AF_XDP/零拷贝)");
pass += 1;
} else {
println!(" [FAIL] 中等环境: Performance 应不 eligible");
fail += 1;
}
if mid_selected == Profile::Balanced || mid_selected == Profile::Minimal {
println!(" [PASS] 中等环境: 选择 {} (非 Performance)", mid_selected.as_str());
pass += 1;
} else {
println!(" [FAIL] 中等环境应选 Balanced/Minimal");
fail += 1;
}
println!("\n============================================================");
println!(" 总计: {} 通过, {} 失败", pass, fail);
println!("============================================================");
if fail > 0 {
std::process::exit(1);
}
}