namerena-rs/miner/src/cacluate.rs

144 lines
4.6 KiB
Rust
Raw Normal View History

2024-03-23 00:32:19 +08:00
use crate::{
evaluate::NamerEvaluater,
name::{Namer, TeamNamer},
};
2024-03-03 17:12:29 +08:00
use std::{io::Write, path::PathBuf};
use base16384::Base16384Utf8;
use colored::Colorize;
2024-03-03 20:05:33 +08:00
use tracing::{debug, info, warn};
2024-03-03 17:12:29 +08:00
/// 根据 u64 生成对应的 name
/// 转换成 base 16384
/// 禁用:
/// U00 ~ U1F ,换行,制表符 等
/// ? , 问号
/// U2000 - U202F , unicode特殊空格 等
/// 不可以空格开头
#[inline(always)]
pub fn gen_name(id: u64) -> String {
let id_bytes = id.to_be_bytes();
Base16384Utf8::encode(id_bytes.as_slice())
}
pub struct CacluateConfig {
/// 开始的 id
pub start: u64,
/// 结束的 id
pub end: u64,
/// 线程数
pub thread_count: u32,
/// 八围预期值
pub prop_expect: u32,
2024-03-31 00:36:36 +08:00
/// qp 预期值
pub qp_expect: u32,
2024-03-03 17:12:29 +08:00
/// 队伍名称
pub team: String,
/// 预期状态输出时间间隔 (秒)
pub report_interval: u64,
2024-04-27 20:27:02 +08:00
/// 可能的设置指定核心亲和性
pub core_affinity: Option<usize>,
2024-03-03 17:12:29 +08:00
}
#[inline(always)]
pub fn cacl(config: CacluateConfig, id: u64, outfile: &PathBuf) {
// 初始猜测的时间间隔
2024-04-26 22:20:13 +08:00
let mut report_interval = 100000; // 第一次猜测测 10w 次, 获取初始数据
2024-03-03 17:12:29 +08:00
let mut run_speed = 0.0;
let mut start_time = std::time::Instant::now();
let mut k: u64 = 0;
let mut get_count: u32 = 0;
2024-04-27 21:20:45 +08:00
// 设置线程亲和性
if let Some(core_affinity) = config.core_affinity {
crate::set_thread2core(core_affinity)
}
2024-04-27 20:27:02 +08:00
2024-03-03 17:12:29 +08:00
// 提前准备好 team_namer
let team_namer = TeamNamer::new(&config.team).unwrap();
for i in (config.start + id..config.end).step_by(config.thread_count as usize) {
let name = gen_name(i as u64);
2024-03-23 00:32:19 +08:00
let mut namer = Namer::new_from_team_namer_unchecked(&team_namer, name.as_str());
2024-03-03 17:12:29 +08:00
let prop = namer.get_property();
2024-03-23 00:37:35 +08:00
k += 1;
if k >= report_interval as u64 {
let now = std::time::Instant::now();
let d_t: std::time::Duration = now.duration_since(start_time);
let new_run_speed = k as f64 / d_t.as_secs_f64();
// 预估剩余时间
let wait_time = (config.end - i) / config.thread_count as u64 / new_run_speed as u64;
let wait_time = chrono::Duration::seconds(wait_time as i64);
// 转换成 时:分:秒
// 根据实际运行速率来调整 report_interval
report_interval = config.report_interval * new_run_speed as u64;
info!(
"|{:>2}|Id:{:>15}|{:6.2}/s {:>3.3}E/d {:>5.2}{}|{:<3}|预计:{}:{}:{}|",
id,
i,
new_run_speed,
new_run_speed * 8.64 / 1_0000.0,
d_t.as_secs_f64(),
// 根据对比上一段运行速度 输出 emoji
// ⬆️ ➡️ ⬇️
if new_run_speed > run_speed {
"⬆️".green()
} else if new_run_speed < run_speed {
// 橙色
"⬇️".red()
} else {
"➡️".blue()
},
get_count,
wait_time.num_hours(),
wait_time.num_minutes() % 60,
wait_time.num_seconds() % 60
);
run_speed = new_run_speed;
start_time = std::time::Instant::now();
k = 0;
}
2024-03-23 09:57:13 +08:00
2024-03-31 00:36:36 +08:00
if prop > config.prop_expect as f32 {
2024-03-03 17:12:29 +08:00
let name = gen_name(i as u64);
let full_name = format!("{}@{}", name, config.team);
2024-03-03 20:05:33 +08:00
// 虚评
2024-03-31 00:23:27 +08:00
namer.update_skill();
2024-03-03 20:05:33 +08:00
2024-03-31 00:23:27 +08:00
let xu = crate::evaluate::xuping::XuPing2_0_1015::evaluate(&namer);
2024-03-23 00:32:19 +08:00
2024-03-31 00:36:36 +08:00
if xu < config.qp_expect as f64 {
2024-03-03 20:05:33 +08:00
continue;
}
2024-04-26 22:20:13 +08:00
let xu_qd = crate::evaluate::xuping::XuPing2_0_1015_QD::evaluate(&namer);
2024-03-03 20:05:33 +08:00
get_count += 1;
2024-03-31 00:23:27 +08:00
info!("Id:{:>15}|{}|{}|{}", i, full_name, xu, namer.get_info());
2024-04-26 22:20:13 +08:00
let write_in = format!(
// <full_name>,<xu>,<xuqd>,<namer.get_info()>
"{},{},{},{}\n",
full_name,
xu,
xu_qd,
2024-04-27 10:37:18 +08:00
namer.get_info_csv()
2024-04-26 22:20:13 +08:00
);
2024-03-03 17:12:29 +08:00
// 写入 (写到最后一行)
match std::fs::OpenOptions::new()
.append(true)
.create(true)
.open(outfile)
2024-04-26 22:20:13 +08:00
.and_then(|mut file| file.write(write_in.as_bytes()))
2024-03-03 17:12:29 +08:00
{
Ok(_) => {}
Err(e) => {
warn!("写入文件<{:?}>失败: {}", outfile, e);
}
}
}
}
2024-03-03 17:37:45 +08:00
}