From 9c0ff14793af5edbe81a784f8e43bafae75591f1 Mon Sep 17 00:00:00 2001 From: Dustin Ray <40841027+drcapybara@users.noreply.github.com> Date: Fri, 6 Sep 2024 05:27:56 -0700 Subject: [PATCH] bench: append rows to tables and update commitments (#115) --- crates/proof-of-sql/Cargo.toml | 7 +- .../proof-of-sql/benches/bench_append_rows.rs | 170 ++++++++++++++++++ ...{criterion_benches.rs => posql_benches.rs} | 0 3 files changed, 176 insertions(+), 1 deletion(-) create mode 100644 crates/proof-of-sql/benches/bench_append_rows.rs rename crates/proof-of-sql/benches/{criterion_benches.rs => posql_benches.rs} (100%) diff --git a/crates/proof-of-sql/Cargo.toml b/crates/proof-of-sql/Cargo.toml index 0bca3b9e7..cfcf2473f 100644 --- a/crates/proof-of-sql/Cargo.toml +++ b/crates/proof-of-sql/Cargo.toml @@ -88,10 +88,15 @@ name = "posql_db" required-features = [ "arrow", "blitzar" ] [[bench]] -name = "criterion_benches" +name = "posql_benches" harness = false required-features = [ "blitzar" ] +[[bench]] +name = "bench_append_rows" +harness = false +required-features = [ "test" ] + [[bench]] name = "jaeger_benches" harness = false diff --git a/crates/proof-of-sql/benches/bench_append_rows.rs b/crates/proof-of-sql/benches/bench_append_rows.rs new file mode 100644 index 000000000..62c25c226 --- /dev/null +++ b/crates/proof-of-sql/benches/bench_append_rows.rs @@ -0,0 +1,170 @@ +//! # Running the Benchmark +//! +//! To run the benchmark with the necessary feature flags enabled, use the following command: +//! +//! ```bash +//! cargo bench --features "test" --bench bench_append_rows +//! ``` +#![allow(missing_docs)] +use criterion::{black_box, criterion_group, criterion_main, Criterion}; +use proof_of_sql::{ + base::{ + commitment::TableCommitment, + database::{ + owned_table_utility::{ + bigint, boolean, decimal75, int, int128, owned_table, scalar, smallint, + timestamptz, varchar, + }, + OwnedTable, + }, + scalar::Scalar, + }, + proof_primitive::dory::{ + test_rng, DoryCommitment, DoryProverPublicSetup, DoryScalar, ProverSetup, PublicParameters, + }, +}; +use proof_of_sql_parser::posql_time::{PoSQLTimeUnit, PoSQLTimeZone}; +use rand::Rng; +use std::ops::Deref; + +/// Bench dory performance when appending rows to a table. This includes the computation of +/// commitments. Chose the number of columns to randomly generate across supported PoSQL +/// data types, and choose the number of rows to append at a time. +/// +/// ```text +/// Most recent benches on 13th Gen Intel® Core™ i9-13900H × 20 with 32gb of RAM: +/// append 10 rows to 10 cols in 1 table = 11.382 ms +/// append 10 rows to 10 cols in 100 tables = 1.1382 seconds +/// append 1000 rows to 10 cols in 1 table = 652ms +/// ``` +fn bench_append_rows(c: &mut Criterion, cols: usize, rows: usize) { + let public_parameters = PublicParameters::rand(10, &mut test_rng()); + let prover_setup = ProverSetup::from(&public_parameters); + let dory_prover_setup = DoryProverPublicSetup::new(&prover_setup, 3); + c.bench_function("append_rows_to_table_commitment", |b| { + let initial_columns: OwnedTable = generate_random_owned_table(cols, rows); + + let table_commitment = TableCommitment::::try_from_columns_with_offset( + initial_columns.inner_table(), + 0, + &dory_prover_setup, + ) + .unwrap(); + + let append_columns: OwnedTable = initial_columns; + + b.iter(|| { + let mut local_commitment = table_commitment.clone(); + local_commitment + .try_append_rows( + black_box(append_columns.inner_table()), + &black_box(dory_prover_setup), + ) + .unwrap(); + }); + }); +} + +/// Generates a random OwnedTable with a specified number of columns +pub fn generate_random_owned_table( + num_columns: usize, + num_rows: usize, +) -> OwnedTable { + let mut rng = rand::thread_rng(); + let column_types = [ + "bigint", + "boolean", + "int128", + "scalar", + "varchar", + "decimal75", + "smallint", + "int", + "timestamptz", + ]; + + let mut columns = Vec::new(); + + for _ in 0..num_columns { + let column_type = column_types[rng.gen_range(0..column_types.len())]; + let identifier = format!("column_{}", rng.gen::()); + + match column_type { + "bigint" => columns.push(bigint(identifier.deref(), vec![rng.gen::(); num_rows])), + "boolean" => columns.push(boolean( + identifier.deref(), + generate_random_boolean_vector(num_rows), + )), + "int128" => columns.push(int128( + identifier.deref(), + vec![rng.gen::(); num_rows], + )), + "scalar" => columns.push(scalar( + identifier.deref(), + vec![generate_random_u64_array(); num_rows], + )), + "varchar" => columns.push(varchar(identifier.deref(), gen_rnd_str(num_rows))), + "decimal75" => columns.push(decimal75( + identifier.deref(), + 12, + 2, + vec![generate_random_u64_array(); num_rows], + )), + "smallint" => columns.push(smallint( + identifier.deref(), + vec![rng.gen::(); num_rows], + )), + "int" => columns.push(int(identifier.deref(), vec![rng.gen::(); num_rows])), + "timestamptz" => columns.push(timestamptz( + identifier.deref(), + PoSQLTimeUnit::Second, + PoSQLTimeZone::Utc, + vec![rng.gen::(); num_rows], + )), + _ => unreachable!(), + } + } + + owned_table(columns) +} + +/// Generates a random vec of varchar +fn gen_rnd_str(array_size: usize) -> Vec { + let mut rng = rand::thread_rng(); + + // Create a vector to hold the owned Strings + let mut string_vec: Vec = Vec::with_capacity(array_size); + + for _ in 0..array_size { + // Generate a random string of a fixed length (e.g., 10 characters) + let random_string: String = (0..10) + .map(|_| rng.sample(rand::distributions::Alphanumeric) as char) + .collect(); + + string_vec.push(random_string); + } + + string_vec +} + +/// Generates a random [u64; 4] +fn generate_random_u64_array() -> [u64; 4] { + let mut rng = rand::thread_rng(); + [rng.gen(), rng.gen(), rng.gen(), rng.gen()] +} + +/// Generates a random vec of true/false +fn generate_random_boolean_vector(size: usize) -> Vec { + let mut rng = rand::thread_rng(); + (0..size).map(|_| rng.gen()).collect() +} + +fn bench_append_rows_10x10(c: &mut Criterion) { + bench_append_rows(c, 10, 10); +} + +fn bench_append_rows_10x1000(c: &mut Criterion) { + bench_append_rows(c, 10, 1000); +} +criterion_group!(benches, bench_append_rows_10x10, bench_append_rows_10x1000); +criterion_main!(benches); diff --git a/crates/proof-of-sql/benches/criterion_benches.rs b/crates/proof-of-sql/benches/posql_benches.rs similarity index 100% rename from crates/proof-of-sql/benches/criterion_benches.rs rename to crates/proof-of-sql/benches/posql_benches.rs