2022-01-05 21:46:57 +00:00
|
|
|
//! read many sources
|
|
|
|
#[macro_use]
|
|
|
|
extern crate criterion;
|
|
|
|
|
|
|
|
use criterion::Criterion;
|
|
|
|
use ethers_core::rand;
|
2022-11-08 23:08:41 +00:00
|
|
|
use ethers_solc::{artifacts::Source, project_util::TempProject};
|
2022-01-05 21:46:57 +00:00
|
|
|
use rand::{distributions::Alphanumeric, Rng};
|
|
|
|
use std::{
|
|
|
|
fs::File,
|
|
|
|
io::{BufWriter, Write},
|
|
|
|
path::{Path, PathBuf},
|
|
|
|
};
|
|
|
|
|
|
|
|
fn read_all_benchmark(c: &mut Criterion) {
|
2022-01-10 19:43:34 +00:00
|
|
|
let root = tempfile::tempdir().unwrap();
|
2022-11-08 23:08:41 +00:00
|
|
|
let inputs = prepare_contracts(root.path(), 35);
|
2022-01-05 21:46:57 +00:00
|
|
|
|
|
|
|
let mut group = c.benchmark_group("read many");
|
2022-11-08 23:08:41 +00:00
|
|
|
group.bench_function("sequential", |b| {
|
|
|
|
b.iter(|| {
|
|
|
|
Source::read_all(&inputs).unwrap();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
group.bench_function("parallel", |b| {
|
|
|
|
b.iter(|| {
|
|
|
|
Source::par_read_all(&inputs).unwrap();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read_solmate(c: &mut Criterion) {
|
|
|
|
let prj = TempProject::checkout("transmissions11/solmate").unwrap();
|
|
|
|
let inputs = ethers_solc::utils::source_files(prj.sources_path());
|
|
|
|
|
|
|
|
let mut group = c.benchmark_group("read solmate");
|
2022-01-05 21:46:57 +00:00
|
|
|
group.bench_function("sequential", |b| {
|
|
|
|
b.iter(|| {
|
|
|
|
Source::read_all(&inputs).unwrap();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
group.bench_function("parallel", |b| {
|
|
|
|
b.iter(|| {
|
|
|
|
Source::par_read_all(&inputs).unwrap();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn prepare_contracts(root: &Path, num: usize) -> Vec<PathBuf> {
|
|
|
|
let mut files = Vec::with_capacity(num);
|
|
|
|
for _ in 0..num {
|
2022-11-07 23:43:11 +00:00
|
|
|
let path = root.join(format!("file{num}.sol"));
|
2022-01-05 21:46:57 +00:00
|
|
|
let f = File::create(&path).unwrap();
|
|
|
|
let mut writer = BufWriter::new(f);
|
|
|
|
|
|
|
|
let mut rng = rand::thread_rng();
|
|
|
|
|
|
|
|
// let's assume a solidity file is between 2kb and 16kb
|
2022-11-08 23:08:41 +00:00
|
|
|
let n: usize = rng.gen_range(4..17);
|
2022-01-05 21:46:57 +00:00
|
|
|
let s: String = rng.sample_iter(&Alphanumeric).take(n * 1024).map(char::from).collect();
|
|
|
|
writer.write_all(s.as_bytes()).unwrap();
|
|
|
|
writer.flush().unwrap();
|
|
|
|
files.push(path)
|
|
|
|
}
|
|
|
|
files
|
|
|
|
}
|
|
|
|
|
2022-11-08 23:08:41 +00:00
|
|
|
criterion_group!(benches, read_all_benchmark, read_solmate);
|
2022-01-05 21:46:57 +00:00
|
|
|
criterion_main!(benches);
|