diff --git a/Cargo.lock b/Cargo.lock index 17426b0c..b0981f4b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2021,10 +2021,12 @@ dependencies = [ "ark-std", "bincode", "console_error_panic_hook", + "criterion 0.4.0", "getrandom 0.2.7", "group-threshold-cryptography", "js-sys", "rand 0.8.5", + "rand_core 0.6.3", "serde", "serde_with", "wasm-bindgen", diff --git a/tpke-wasm/.gitignore b/tpke-wasm/.gitignore index 4e301317..bb9a2539 100644 --- a/tpke-wasm/.gitignore +++ b/tpke-wasm/.gitignore @@ -2,5 +2,5 @@ **/*.rs.bk Cargo.lock bin/ -pkg/ +pkg wasm-pack.log diff --git a/tpke-wasm/BENCHMARK.md b/tpke-wasm/BENCHMARK.md new file mode 100644 index 00000000..572462ab --- /dev/null +++ b/tpke-wasm/BENCHMARK.md @@ -0,0 +1,56 @@ +# Benchmarks + +## Benchmarking WASM + +This time we may not use [`centurion.rs`](https://github.com/bheisler/criterion.rs/blob/version-0.4/book/src/user_guide/wasi.md#webasseblywasi-benchmarking) because `wasm32-wasi` is incompatible with `wasm_bindgen` ([1](https://github.com/rustwasm/wasm-bindgen/issues/2554), [2](https://github.com/bevyengine/bevy/discussions/5908?sort=new)). Instead, we're going to measure performance directly in the browser. + +### Setup + +```bash +wasm-pack build --release --target web + +cd js-benches +ln -s ../pkg . +``` + +### Running + +```bash +npx http-server +# Visit localhost:8080/index.html +``` + +## Benchmarking Rust + +```bash +cargo bench +``` + +## Results + +### WASM Results + +``` + +``` + +### Rust Results + +``` +TPKE-WASM/tpke-wasm::encrypt - num_shares=8, num_entities=8, threshold=8 + time: [4.8427 ms 4.9178 ms 5.0113 ms] +Found 2 outliers among 10 measurements (20.00%) + 2 (20.00%) high mild +TPKE-WASM/tpke-wasm::encrypt - num_shares=16, num_entities=16, threshold=16 + time: [4.8967 ms 4.9732 ms 5.1114 ms] +Found 1 outliers among 10 measurements (10.00%) + 1 (10.00%) high mild +TPKE-WASM/tpke-wasm::encrypt - num_shares=32, num_entities=32, threshold=32 + time: [4.8219 ms 5.0377 ms 5.3367 ms] +Found 1 outliers among 10 measurements (10.00%) + 1 (10.00%) high severe +TPKE-WASM/tpke-wasm::encrypt - num_shares=64, num_entities=64, threshold=64 + time: [4.8865 ms 4.9192 ms 4.9529 ms] +TPKE-WASM/tpke-wasm::encrypt - num_shares=128, num_entities=128, threshold=128 + time: [4.8900 ms 4.9389 ms 4.9834 ms] +``` diff --git a/tpke-wasm/Cargo.toml b/tpke-wasm/Cargo.toml index aff4b5b8..9eab8584 100644 --- a/tpke-wasm/Cargo.toml +++ b/tpke-wasm/Cargo.toml @@ -33,3 +33,9 @@ ark-std = "0.3.0" [dev-dependencies] wasm-bindgen-test = "0.3.13" console_error_panic_hook = "0.1.7" +criterion = { version = "0.4", default-features = false } +rand_core = "0.6" + +[[bench]] +name = "benchmarks" +harness = false \ No newline at end of file diff --git a/tpke-wasm/benches/benchmarks.rs b/tpke-wasm/benches/benchmarks.rs new file mode 100644 index 00000000..a882a01c --- /dev/null +++ b/tpke-wasm/benches/benchmarks.rs @@ -0,0 +1,30 @@ +use criterion::{black_box, criterion_group, criterion_main, Criterion}; + +pub fn bench_encrypt_combine(c: &mut Criterion) { + fn encrypt_bench( + num_shares: usize, + num_entities: usize, + threshold: usize, + ) -> impl Fn() { + let message = "my-secret-message".as_bytes().to_vec(); + let setup = tpke_wasm::Setup::new(threshold, num_shares, num_entities); + move || { + let message = message.clone(); + black_box(tpke_wasm::encrypt(message, setup.public_key)); + } + } + + let mut group = c.benchmark_group("TPKE-WASM"); + group.sample_size(10); + + for num_shares in [8, 16, 32, 64, 128].iter() { + let a = encrypt_bench(*num_shares, *num_shares, *num_shares); + group.measurement_time(core::time::Duration::new(30, 0)); + group.bench_function(format!("tpke-wasm::encrypt - num_shares={}, num_entities={}, threshold={}", num_shares, num_shares, num_shares), |b| { + b.iter(|| a()) + }); + } +} + +criterion_group!(benches, bench_encrypt_combine); +criterion_main!(benches); diff --git a/tpke-wasm/js-benches/index.html b/tpke-wasm/js-benches/index.html new file mode 100644 index 00000000..70068db4 --- /dev/null +++ b/tpke-wasm/js-benches/index.html @@ -0,0 +1,9 @@ + + +
+ +