use dat::dat_certificate::DatCertificate;
use dat::dat_crypto_algorithm::DatCryptoAlgorithm::{AES128GCMN, AES256GCMN};
use dat::dat_manager::DatManager;
use dat::dat_payload::DatPayload;
use dat::dat_signature_algorithm::DatSignatureAlgorithm::{P256, P384, P521};
use dat::util::now_unix_timestamp;
use rand::RngExt;
use std::sync::Arc;
use std::time::Instant;
use tokio::task::JoinSet;
fn rand_string() -> String {
let mut rng = rand::rng();
(0..100).map(|_| { rng.sample(rand::distr::Alphanumeric) as char }).collect()
}
#[tokio::test(flavor = "multi_thread")]
async fn test() {
if cfg!(debug_assertions) {
println!("performance test is disabled in debug mode.");
return;
}
let loop_size = 10000;
let plain = Arc::new(rand_string());
let secure = Arc::new(rand_string());
println!("performance test (plain, secure)");
println!("plain: {}", plain);
println!("secure: {}", secure);
for signature_algorithm in [P256, P384, P521] {
for crypto_algorithm in [AES128GCMN, AES256GCMN] {
let key = Arc::new(DatCertificate::generate(0, signature_algorithm, crypto_algorithm, now_unix_timestamp() - 10, now_unix_timestamp() + 600, 60).unwrap());
let mut futures: JoinSet<String> = JoinSet::new();
let start = Instant::now();
for _ in 0..loop_size {
let key = Arc::clone(&key);
let plain = Arc::clone(&plain);
let secure = Arc::clone(&secure);
futures.spawn(async move {
DatManager::_issue(&key, &*plain, &*secure).unwrap()
});
}
let mut dat = String::new();
while let Some(res) = futures.join_next().await {
dat = res.unwrap();
}
let duration = start.elapsed(); println!("{}/{} issue * {loop_size} : {}ms", signature_algorithm, crypto_algorithm, duration.as_millis());
let dat = Arc::new(dat);
let mut futures: JoinSet<DatPayload> = JoinSet::new();
let start = Instant::now();
for _ in 0..loop_size {
let key = Arc::clone(&key);
let dat = Arc::clone(&dat);
futures.spawn(async move {
DatManager::_parse(&key, (*dat).parse().unwrap()).unwrap()
});
}
while let Some(res) = futures.join_next().await {
res.unwrap();
}
let duration = start.elapsed(); println!("{}/{} parse * {loop_size} : {}ms", signature_algorithm, crypto_algorithm, duration.as_millis());
}
}
}