/
teterkin
/
vm_load_testing
Обзор
Документация
Войти
/
teterkin
/
vm_load_testing
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/cpu.rs
84 строки
2 KB
alexefan136
upload files
12 ноя 2025, 18:00
12 ноя 2025, 18:00
eb766a0
Код
Авторство
О чём код?
// src/cpu.rs use std::time::Instant; use sha2::{Digest}; #[derive(Debug, Clone)] pub struct CpuResult { pub logical_cores: usize, pub physical_cores: usize, pub base_frequency_mhz: Option<u64>, pub ops_per_sec: f64, pub has_avx2: bool, pub has_aes: bool, } /// Выполняет CPU-bound нагрузку и собирает информацию о процессоре. pub fn bench_cpu() -> CpuResult { let logical_cores = num_cpus::get(); let physical_cores = num_cpus::get_physical(); let base_freq = read_cpu_base_frequency(); let has_avx2 = detect_avx2(); let has_aes = detect_aes(); const OPS: u64 = 300_000; let start = Instant::now(); for _ in 0..OPS { let data = (0..1024).map(|_| rand::random::<u8>()).collect::<Vec<u8>>(); let _ = sha2::Sha256::digest(&data); } let elapsed = start.elapsed().as_secs_f64(); let ops_per_sec = OPS as f64 / elapsed; CpuResult { logical_cores, physical_cores, base_frequency_mhz: base_freq, ops_per_sec, has_avx2, has_aes, } } /// Читает базовую частоту CPU из /proc/cpuinfo (только Linux). fn read_cpu_base_frequency() -> Option<u64> { #[cfg(target_os = "linux")] { if let Ok(content) = std::fs::read_to_string("/proc/cpuinfo") { for line in content.lines() { if line.starts_with("cpu MHz") { if let Some(freq_str) = line.split(':').nth(1) { if let Ok(freq_f64) = freq_str.trim().parse::<f64>() { return Some(freq_f64 as u64); } } } } } } None } /// Проверяет поддержку AVX2 (только x86_64). #[cfg(target_arch = "x86_64")] fn detect_avx2() -> bool { std::is_x86_feature_detected!("avx2") } #[cfg(not(target_arch = "x86_64"))] fn detect_avx2() -> bool { false } /// Проверяет поддержку AES-NI (только x86_64). #[cfg(target_arch = "x86_64")] fn detect_aes() -> bool { std::is_x86_feature_detected!("aes") } #[cfg(not(target_arch = "x86_64"))] fn detect_aes() -> bool { false }