Skip to content

Commit

Permalink
Get benches building
Browse files Browse the repository at this point in the history
  • Loading branch information
Stentonian committed Jan 16, 2024
1 parent d96d5f3 commit 13900f2
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 26 deletions.
49 changes: 33 additions & 16 deletions benches/criterion_benches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
//! long (see large_input_benches.rs).

use std::path::Path;
use std::str::FromStr;

use criterion::measurement::Measurement;
use criterion::{criterion_group, criterion_main};
use criterion::{BenchmarkId, Criterion, SamplingMode};
use statistical::*;

use dapol::accumulators::{NdmSmt, NdmSmtConfigBuilder};
use dapol::{Accumulator, InclusionProof};
use dapol::{DapolConfigBuilder, DapolTree, InclusionProof, Secret};

mod inputs;
use inputs::{max_thread_counts_greater_than, num_entities_in_range, tree_heights_in_range};
Expand Down Expand Up @@ -43,6 +43,8 @@ pub fn bench_build_tree<T: Measurement>(c: &mut Criterion<T>) {
let epoch = jemalloc_ctl::epoch::mib().unwrap();
let allocated = jemalloc_ctl::stats::allocated::mib().unwrap();

let master_secret = Secret::from_str("secret").unwrap();

dapol::initialize_machine_parallelism();
dapol::utils::activate_logging(*LOG_VERBOSITY);

Expand Down Expand Up @@ -103,7 +105,7 @@ pub fn bench_build_tree<T: Measurement>(c: &mut Criterion<T>) {
// Tree build.

let mut memory_readings = vec![];
let mut ndm_smt = Option::<NdmSmt>::None;
let mut dapol_tree = Option::<DapolTree>::None;

group.bench_with_input(
BenchmarkId::new(
Expand All @@ -119,17 +121,20 @@ pub fn bench_build_tree<T: Measurement>(c: &mut Criterion<T>) {
|bench, tup| {
bench.iter(|| {
// this is necessary for the memory readings to work
ndm_smt = None;
dapol_tree = None;

epoch.advance().unwrap();
let before = allocated.read().unwrap();

ndm_smt = Some(
NdmSmtConfigBuilder::default()
dapol_tree = Some(
DapolConfigBuilder::default()
.accumulator_type(dapol::AccumulatorType::NdmSmt)
.height(tup.0)
.max_thread_count(tup.1)
.num_random_entities(tup.2)
.master_secret(master_secret.clone())
.build()
.expect("Unable to build DapolConfig")
.parse()
.expect("Unable to parse NdmSmtConfig"),
);
Expand Down Expand Up @@ -160,8 +165,8 @@ pub fn bench_build_tree<T: Measurement>(c: &mut Criterion<T>) {
let src_dir = env!("CARGO_MANIFEST_DIR");
let target_dir = Path::new(&src_dir).join("target");
let dir = target_dir.join("serialized_trees");
let path = Accumulator::parse_accumulator_serialization_path(dir).unwrap();
let acc = Accumulator::NdmSmt(ndm_smt.expect("Tree should have been built"));
let path = DapolTree::parse_serialization_path(dir).unwrap();
let tree = dapol_tree.expect("Tree should have been built");

group.bench_function(
BenchmarkId::new(
Expand All @@ -174,7 +179,7 @@ pub fn bench_build_tree<T: Measurement>(c: &mut Criterion<T>) {
),
),
|bench| {
bench.iter(|| acc.serialize(path.clone()).unwrap());
bench.iter(|| tree.serialize(path.clone()).unwrap());
},
);

Expand All @@ -196,6 +201,8 @@ pub fn bench_build_tree<T: Measurement>(c: &mut Criterion<T>) {
pub fn bench_generate_proof<T: Measurement>(c: &mut Criterion<T>) {
let mut group = c.benchmark_group("proofs");

let master_secret = Secret::from_str("secret").unwrap();

dapol::initialize_machine_parallelism();
dapol::utils::activate_logging(*LOG_VERBOSITY);

Expand Down Expand Up @@ -241,15 +248,19 @@ pub fn bench_generate_proof<T: Measurement>(c: &mut Criterion<T>) {
continue;
}

let ndm_smt = NdmSmtConfigBuilder::default()
let dapol_tree = DapolConfigBuilder::default()
.accumulator_type(dapol::AccumulatorType::NdmSmt)
.master_secret(master_secret.clone())
.height(h)
.num_random_entities(n)
.build()
.expect("Unable to build DapolConfig")
.parse()
.expect("Unable to parse NdmSmtConfig");

let entity_id = ndm_smt
let entity_id = dapol_tree
.entity_mapping()
.unwrap()
.keys()
.next()
.expect("Tree should have at least 1 entity");
Expand All @@ -264,7 +275,7 @@ pub fn bench_generate_proof<T: Measurement>(c: &mut Criterion<T>) {
|bench| {
bench.iter(|| {
proof = Some(
ndm_smt
dapol_tree
.generate_inclusion_proof(entity_id)
.expect("Proof should have been generated successfully"),
);
Expand Down Expand Up @@ -299,6 +310,8 @@ pub fn bench_generate_proof<T: Measurement>(c: &mut Criterion<T>) {
pub fn bench_verify_proof<T: Measurement>(c: &mut Criterion<T>) {
let mut group = c.benchmark_group("proofs");

let master_secret = Secret::from_str("secret").unwrap();

dapol::initialize_machine_parallelism();
dapol::utils::activate_logging(*LOG_VERBOSITY);

Expand Down Expand Up @@ -344,22 +357,26 @@ pub fn bench_verify_proof<T: Measurement>(c: &mut Criterion<T>) {
continue;
}

let ndm_smt = NdmSmtConfigBuilder::default()
let dapol_tree = DapolConfigBuilder::default()
.accumulator_type(dapol::AccumulatorType::NdmSmt)
.master_secret(master_secret.clone())
.height(h)
.num_random_entities(n)
.build()
.expect("Unable to build DapolConfig")
.parse()
.expect("Unable to parse NdmSmtConfig");

let root_hash = ndm_smt.root_hash();
let root_hash = dapol_tree.root_hash();

let entity_id = ndm_smt
let entity_id = dapol_tree
.entity_mapping()
.unwrap()
.keys()
.next()
.expect("Tree should have at least 1 entity");

let proof = ndm_smt
let proof = dapol_tree
.generate_inclusion_proof(entity_id)
.expect("Proof should have been generated successfully");

Expand Down
27 changes: 17 additions & 10 deletions benches/manual_benches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
//! unfortunately, but this is the trade-off.

use std::path::Path;
use std::str::FromStr;
use std::time::Instant;

use statistical::*;

use dapol::accumulators::{Accumulator, NdmSmt, NdmSmtConfigBuilder};
use dapol::{DapolConfigBuilder, DapolTree, Secret};

mod inputs;
use inputs::{max_thread_counts_greater_than, num_entities_in_range, tree_heights_in_range};
Expand All @@ -36,6 +37,8 @@ fn main() {

let total_mem = system_total_memory_mb();

let master_secret = Secret::from_str("secret").unwrap();

dapol::initialize_machine_parallelism();
dapol::utils::activate_logging(*LOG_VERBOSITY);

Expand Down Expand Up @@ -100,14 +103,14 @@ fn main() {
// ==============================================================
// Tree build.

let mut ndm_smt = Option::<NdmSmt>::None;
let mut dapol_tree = Option::<DapolTree>::None;
let mut memory_readings = vec![];
let mut timings = vec![];

// Do 3 readings (Criterion does 10 minimum).
for _i in 0..3 {
// this is necessary for the memory readings to work
ndm_smt = None;
dapol_tree = None;

println!(
"building tree i {} time {}",
Expand All @@ -119,14 +122,17 @@ fn main() {
let mem_before = allocated.read().unwrap();
let time_start = Instant::now();

ndm_smt = Some(
NdmSmtConfigBuilder::default()
dapol_tree = Some(
DapolConfigBuilder::default()
.accumulator_type(dapol::AccumulatorType::NdmSmt)
.height(h)
.max_thread_count(t)
.master_secret(master_secret.clone())
.num_random_entities(n)
.build()
.expect("Unable to build DapolConfig")
.parse()
.expect("Unable to parse NdmSmtConfig"),
.expect("Unable to parse DapolConfig"),
);

let tree_build_time = time_start.elapsed();
Expand Down Expand Up @@ -154,12 +160,13 @@ fn main() {
let src_dir = env!("CARGO_MANIFEST_DIR");
let target_dir = Path::new(&src_dir).join("target");
let dir = target_dir.join("serialized_trees");
let path = Accumulator::parse_accumulator_serialization_path(dir).unwrap();
let acc =
Accumulator::NdmSmt(ndm_smt.expect("NDM SMT should have been set in loop"));
let path = DapolTree::parse_serialization_path(dir).unwrap();

let time_start = Instant::now();
acc.serialize(path.clone()).unwrap();
dapol_tree
.expect("DapolTree should have been set in loop")
.serialize(path.clone())
.unwrap();
let serialization_time = time_start.elapsed();

let file_size = std::fs::metadata(path)
Expand Down

0 comments on commit 13900f2

Please sign in to comment.