use nanoid::nanoid;
pub struct IdGenerator;
impl IdGenerator {
pub fn get_id() -> String {
const ALPHABET: [char; 32] = [
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c',
'd', 'e', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r',
's', 't', 'u', 'v', 'w', 'x',
];
nanoid!(12, &ALPHABET)
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::time::Instant;
#[test]
fn test_id_generation_performance() {
const ITERATIONS: usize = 1_000_000;
let start = Instant::now();
for _ in 0..ITERATIONS {
let _id = IdGenerator::get_id();
}
let new_duration = start.elapsed();
println!("生成 {} 个新ID耗时: {:?}", ITERATIONS, new_duration);
println!("平均每个ID生成时间: {:?}", new_duration / ITERATIONS as u32);
}
#[test]
fn test_id_uniqueness() {
const ITERATIONS: usize = 1_000_000;
let mut ids = std::collections::HashSet::with_capacity(ITERATIONS);
for _ in 0..ITERATIONS {
let id = IdGenerator::get_id();
assert!(ids.insert(id), "发现重复ID!");
}
println!("成功生成 {} 个唯一ID", ITERATIONS);
let total_possible = 32u64.pow(12);
let collision_probability =
1.0 - (1.0 - 1.0 / (total_possible as f64)).powi(ITERATIONS as i32);
println!("理论碰撞概率: {:.10}%", collision_probability * 100.0);
}
}