feat: benchmark pow times

This commit is contained in:
Aravinth Manivannan 2023-07-09 16:12:04 +05:30
commit 4bc6450d22
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88
4 changed files with 1136 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
/target
db

1012
Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

18
Cargo.toml Normal file
View file

@ -0,0 +1,18 @@
[package]
name = "powd"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
bincode = "1.3.3"
clap = { version = "4.3.11", features = ["derive"] }
log = "0.4.19"
pow_sha256 = { git = "https://github.com/mCaptcha/pow_sha256"}
pretty_env_logger = "0.5.0"
rand = "0.8.5"
rayon = "1.7.0"
serde = { version = "1.0.169", features = ["derive"] }
serde_json = "1.0.100"
sled = "0.34.7"

104
src/main.rs Normal file
View file

@ -0,0 +1,104 @@
use clap::Parser;
use pow_sha256::ConfigBuilder;
use serde::{Deserialize, Serialize};
use std::time::Instant;
#[derive(Parser)] // requires `derive` feature
#[command(name = "cargo")]
#[command(bin_name = "cargo")]
enum PoWdCli {
Generate(GenerateDeriveArgs),
PrintDB(PrintRes),
}
#[derive(clap::Args, Debug, Clone)]
#[command(author, version, about, long_about = None)]
struct GenerateDeriveArgs {
#[arg(long)]
start: u32,
#[arg(long)]
max: u32,
#[arg(long)]
db: std::path::PathBuf,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
struct Log {
string: String,
salt: String,
time: u128,
}
impl GenerateDeriveArgs {
fn run(&self) {
let db = sled::open(&self.db).unwrap();
let salt = get_random(32);
let string = get_random(32);
let pow_config = ConfigBuilder::default().salt(salt.clone()).build().unwrap();
for difficulty in self.start..self.max {
let start = Instant::now();
pow_config.prove_work(&string, difficulty).unwrap();
let finish = Instant::now();
let time_elapsed = finish.duration_since(start);
let time= time_elapsed.as_micros();
log::info!("Difficulty factor {difficulty} generated in {time}");
let log = Log {
salt: salt.clone(),
time,
string: string.clone(),
};
db.insert(
bincode::serialize(&difficulty).unwrap(),
bincode::serialize(&log).unwrap(),
)
.unwrap();
}
}
}
#[derive(clap::Args, Debug, Clone)]
#[command(author, version, about, long_about = None)]
struct PrintRes {
#[arg(long)]
db: std::path::PathBuf,
}
fn main() {
std::env::set_var("RUST_LOG", "INFO");
pretty_env_logger::init();
match PoWdCli::parse() {
PoWdCli::Generate(args) => {
println!("{:?}", args);
args.run();
}
PoWdCli::PrintDB(args) => {
println!("{:?}", args);
let db = sled::open(args.db).unwrap();
for entry in db.iter() {
let (difficulty, entry) = entry.unwrap();
let log: Log = bincode::deserialize::<Log>(&entry[..]).unwrap();
let difficulty: u32 = bincode::deserialize::<u32>(&difficulty).unwrap();
println!("{difficulty}: {}", log.time);
}
}
}
}
pub fn get_random(len: usize) -> String {
use std::iter;
use rand::{distributions::Alphanumeric, rngs::ThreadRng, thread_rng, Rng};
let mut rng: ThreadRng = thread_rng();
iter::repeat(())
.map(|()| rng.sample(Alphanumeric))
.map(char::from)
.take(len)
.collect::<String>()
}