Skip to content

Commit

Permalink
panicks at 'capacity overflow' during js-benches
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-roslaniec committed Nov 16, 2022
1 parent 5573895 commit 2958e93
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 9 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tpke-wasm/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
**/*.rs.bk
Cargo.lock
bin/
pkg/
pkg
wasm-pack.log
56 changes: 56 additions & 0 deletions tpke-wasm/BENCHMARK.md
Original file line number Diff line number Diff line change
@@ -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]
```
6 changes: 6 additions & 0 deletions tpke-wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
30 changes: 30 additions & 0 deletions tpke-wasm/benches/benchmarks.rs
Original file line number Diff line number Diff line change
@@ -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);
9 changes: 9 additions & 0 deletions tpke-wasm/js-benches/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>TPKE benchmark</title>
<script type="module" src="./index.js"></script>
</head>
<body></body>
</html>
16 changes: 16 additions & 0 deletions tpke-wasm/js-benches/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import init from "./pkg/tpke_wasm.js";
import { Setup } from "./pkg/tpke_wasm.js";

const runBenchmarks = async () => {
console.log("Initializing ...");

const setup = new Setup();

console.log("Setup: ", setup);

console.log("Done!");
};

init()
.then(runBenchmarks)
.catch((err) => console.error(err));
2 changes: 0 additions & 2 deletions tpke-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ use serde_with::serde_as;
use utils::set_panic_hook;
use wasm_bindgen::prelude::*;

extern crate alloc;
// Use `wee_alloc` as the global allocator.
extern crate wee_alloc;

pub type E = ark_bls12_381::Bls12_381;
Expand Down
37 changes: 34 additions & 3 deletions ferveo/BENCHMARK.md → tpke/BENCHMARK.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,34 @@
# WASM results
# Benchmarks

## Benchmarking WASM

Based on `centurion.rs` (docs)[https://github.com/bheisler/criterion.rs/blob/version-0.4/book/src/user_guide/wasi.md#webasseblywasi-benchmarking]

### Setup

```bash
cargo install cargo-wasi
npm install -g @wasmer/cli

cargo wasi build --bench=benchmarks --release
cp `ls -t ../target/wasm32-wasi/release/deps/*.wasm | head -n 1` benchmarks.wasm
```

### Running

```bash
wasmer-js run --dir=. benchmarks.wasm -- --bench
```

## Benchmarking Rust

```bash
cargo bench
```

## Results

### WASM Results

```
TPKE/share_combine: 100 validators threshold 1024*2/3 - #msg 1 - msg-size = 100 bytes
Expand Down Expand Up @@ -27,7 +57,8 @@ TPKE/share_combine: 200 validators threshold 8192*2/3 - #msg 1 - msg-size = 100
time: [413.68 ms 426.59 ms 434.22 ms]
```

# Rust results
### Rust Results

```
TPKE/share_combine: 100 validators threshold 1024*2/3 - #msg 1 - msg-size = 100 bytes
time: [39.402 ms 39.933 ms 40.442 ms]
Expand Down Expand Up @@ -76,4 +107,4 @@ TPKE/share_combine: 200 validators threshold 8192*2/3 - #msg 1 - msg-size = 100
time: [76.676 ms 77.459 ms 78.123 ms]
change: [-80.315% -79.559% -78.876%] (p = 0.00 < 0.05)
Performance has improved.
```
```
4 changes: 1 addition & 3 deletions tpke/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,4 @@ cp `ls -t ../target/wasm32-wasi/release/deps/*.wasm | head -n 1` benchmarks.wasm

```bash
wasmer-js run --dir=. benchmarks.wasm -- --bench
```

### Results
```

0 comments on commit 2958e93

Please sign in to comment.