Explorar o código

tests/bench: delete bench_zk, add zk_arith (uses criterion crate)

zero %!s(int64=2) %!d(string=hai) anos
pai
achega
afa1856236
Modificáronse 3 ficheiros con 180 adicións e 36 borrados
  1. 146 2
      Cargo.lock
  2. 8 0
      Cargo.toml
  3. 26 34
      bench/zk_arith.rs

+ 146 - 2
Cargo.lock

@@ -211,6 +211,12 @@ dependencies = [
  "libc",
 ]
 
+[[package]]
+name = "anes"
+version = "0.1.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4b46cbb362ab8752921c97e041f5e366ee6297bd428a31275b9fcf1e380f7299"
+
 [[package]]
 name = "ansi_term"
 version = "0.12.1"
@@ -1103,6 +1109,12 @@ dependencies = [
  "serde_json",
 ]
 
+[[package]]
+name = "cast"
+version = "0.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5"
+
 [[package]]
 name = "cc"
 version = "1.0.90"
@@ -1185,6 +1197,33 @@ dependencies = [
  "windows-targets 0.52.4",
 ]
 
+[[package]]
+name = "ciborium"
+version = "0.2.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "42e69ffd6f0917f5c029256a24d0161db17cea3997d185db0d35926308770f0e"
+dependencies = [
+ "ciborium-io",
+ "ciborium-ll",
+ "serde",
+]
+
+[[package]]
+name = "ciborium-io"
+version = "0.2.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "05afea1e0a06c9be33d539b876f1ce3692f4afea2cb41f740e7743225ed1c757"
+
+[[package]]
+name = "ciborium-ll"
+version = "0.2.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "57663b653d948a338bfb3eeba9bb2fd5fcfaecb9e199e87e1eda4d9e8b240fd9"
+dependencies = [
+ "ciborium-io",
+ "half",
+]
+
 [[package]]
 name = "cipher"
 version = "0.2.5"
@@ -1226,11 +1265,23 @@ dependencies = [
  "atty",
  "bitflags 1.3.2",
  "strsim 0.8.0",
- "textwrap",
+ "textwrap 0.11.0",
  "unicode-width",
  "vec_map",
 ]
 
+[[package]]
+name = "clap"
+version = "3.2.25"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4ea181bf566f71cb9a5d17a59e1871af638180a18fb0035c92ae62b705207123"
+dependencies = [
+ "bitflags 1.3.2",
+ "clap_lex 0.2.4",
+ "indexmap 1.9.3",
+ "textwrap 0.16.1",
+]
+
 [[package]]
 name = "clap"
 version = "4.5.3"
@@ -1249,7 +1300,7 @@ checksum = "ae129e2e766ae0ec03484e609954119f123cc1fe650337e155d03b022f24f7b4"
 dependencies = [
  "anstream",
  "anstyle",
- "clap_lex",
+ "clap_lex 0.7.0",
  "strsim 0.11.0",
 ]
 
@@ -1265,6 +1316,15 @@ dependencies = [
  "syn 2.0.53",
 ]
 
+[[package]]
+name = "clap_lex"
+version = "0.2.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2850f2f5a82cbf437dd5af4d49848fbdfc27c157c3d010345776f952765261c5"
+dependencies = [
+ "os_str_bytes",
+]
+
 [[package]]
 name = "clap_lex"
 version = "0.7.0"
@@ -1594,6 +1654,42 @@ dependencies = [
  "cfg-if 1.0.0",
 ]
 
+[[package]]
+name = "criterion"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e7c76e09c1aae2bc52b3d2f29e13c6572553b30c4aa1b8a49fd70de6412654cb"
+dependencies = [
+ "anes",
+ "atty",
+ "cast",
+ "ciborium",
+ "clap 3.2.25",
+ "criterion-plot",
+ "itertools 0.10.5",
+ "lazy_static",
+ "num-traits",
+ "oorandom",
+ "plotters",
+ "rayon",
+ "regex",
+ "serde",
+ "serde_derive",
+ "serde_json",
+ "tinytemplate",
+ "walkdir",
+]
+
+[[package]]
+name = "criterion-plot"
+version = "0.5.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6b50826342786a51a89e2da3a28f1c32b06e387201bc2d19791f622c673706b1"
+dependencies = [
+ "cast",
+ "itertools 0.10.5",
+]
+
 [[package]]
 name = "crossbeam-deque"
 version = "0.8.5"
@@ -1858,6 +1954,7 @@ dependencies = [
  "bs58",
  "chrono",
  "clap 4.5.3",
+ "criterion",
  "crypto_api_chachapoly",
  "darkfi-derive",
  "darkfi-sdk",
@@ -3446,6 +3543,16 @@ dependencies = [
  "xxhash-rust",
 ]
 
+[[package]]
+name = "half"
+version = "2.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b5eceaaeec696539ddaf7b333340f1af35a5aa87ae3e4f3ead0532f72affab2e"
+dependencies = [
+ "cfg-if 1.0.0",
+ "crunchy",
+]
+
 [[package]]
 name = "halo2_gadgets"
 version = "0.3.0"
@@ -3870,6 +3977,15 @@ dependencies = [
  "waker-fn",
 ]
 
+[[package]]
+name = "itertools"
+version = "0.10.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473"
+dependencies = [
+ "either",
+]
+
 [[package]]
 name = "itertools"
 version = "0.11.0"
@@ -4518,6 +4634,12 @@ version = "1.19.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92"
 
+[[package]]
+name = "oorandom"
+version = "11.1.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0ab1bc2a289d34bd04a330323ac98a1b4bc82c9d9fcb1e66b63caa84da26b575"
+
 [[package]]
 name = "opaque-debug"
 version = "0.3.1"
@@ -4593,6 +4715,12 @@ dependencies = [
  "num-traits",
 ]
 
+[[package]]
+name = "os_str_bytes"
+version = "6.6.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e2355d85b9a3786f481747ced0e0ff2ba35213a1f9bd406ed906554d7af805a1"
+
 [[package]]
 name = "overload"
 version = "0.1.1"
@@ -6876,6 +7004,12 @@ dependencies = [
  "unicode-width",
 ]
 
+[[package]]
+name = "textwrap"
+version = "0.16.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "23d434d3f8967a09480fb04132ebe0a3e088c173e6d0ee7897abbdf4eab0f8b9"
+
 [[package]]
 name = "thiserror"
 version = "1.0.58"
@@ -7024,6 +7158,16 @@ dependencies = [
  "displaydoc",
 ]
 
+[[package]]
+name = "tinytemplate"
+version = "1.2.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "be4d6b5f19ff7664e8c98d03e2139cb510db9b0a60b55f8e8709b689d939b6bc"
+dependencies = [
+ "serde",
+ "serde_json",
+]
+
 [[package]]
 name = "tinyvec"
 version = "1.6.0"

+ 8 - 0
Cargo.toml

@@ -130,6 +130,8 @@ halo2_gadgets = "0.3.0"
 plotters = "0.3.5"
 easy-parallel = "3.3.1"
 prettytable-rs = "0.10.0"
+# Used for benchmarks
+criterion = { version = "0.4", features = ["html_reports"] }
 
 # -----BEGIN LIBRARY FEATURES-----
 [features]
@@ -312,3 +314,9 @@ halo2_proofs = {git="https://github.com/parazyd/halo2", branch="v4"}
 halo2_gadgets = {git="https://github.com/parazyd/halo2", branch="v4"}
 # Temp rust nightly fix
 pathfinder_simd = {git="https://github.com/servo/pathfinder", branch="main"}
+
+[[bench]]
+name = "zk_arith"
+harness = false
+path = "bench/zk_arith.rs"
+

+ 26 - 34
tests/bench_zk.rs → bench/zk_arith.rs

@@ -16,6 +16,7 @@
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
+use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
 use halo2_proofs::{circuit::Value, pasta::Fp};
 use rand::rngs::OsRng;
 
@@ -27,53 +28,44 @@ use darkfi::{
         Proof,
     },
     zkas::ZkBinary,
-    Result,
 };
 
-const SAMPLES: u128 = 10;
-
-#[test]
-#[ignore]
-fn bench_zk() -> Result<()> {
+fn zk_arith_bench(c: &mut Criterion) {
     let bincode = include_bytes!("../proof/arithmetic.zk.bin");
-    let zkbin = ZkBinary::decode(bincode)?;
+    let zkbin = ZkBinary::decode(bincode).unwrap();
 
     let a = Fp::from(4);
     let b = Fp::from(110);
 
-    // Values for the proof
     let prover_witnesses = vec![Witness::Base(Value::known(a)), Witness::Base(Value::known(b))];
-
     let public_inputs = vec![a + b, a * b, a - b];
 
-    // I tried cargo bench, but there's no way to display k=X for each individual bench
-    // TODO: make a benchmark group and use bench_with_input (cargo bench)
-    // see https://github.com/getsentry/relay/blob/master/relay-cardinality/benches/redis_impl.rs#L137-L165
-    for k in 11..20 {
-        println!("Benchmarking k={}", k);
+    let circuit = ZkCircuit::new(prover_witnesses.clone(), &zkbin);
 
-        let circuit = ZkCircuit::new(prover_witnesses.clone(), &zkbin);
-        let proving_key = ProvingKey::build(k, &circuit);
-        let mut total = 0;
-        for _ in 0..SAMPLES {
-            let now = std::time::Instant::now();
-            let _ = Proof::create(&proving_key, &[circuit.clone()], &public_inputs, &mut OsRng)?;
-            total += now.elapsed().as_millis();
-        }
-        println!("Avg proving time: {} ms", total / SAMPLES);
-        let proof = Proof::create(&proving_key, &[circuit], &public_inputs, &mut OsRng)?;
+    let mut prove_group = c.benchmark_group("prove");
+    for k in 11..20 {
+        let proving_key = ProvingKey::build(k, &circuit.clone());
+        prove_group.bench_with_input(BenchmarkId::from_parameter(k), &k, |b, &_k| {
+            b.iter(|| Proof::create(&proving_key, &[circuit.clone()], &public_inputs, &mut OsRng))
+        });
+    }
+    prove_group.finish();
 
-        let verifier_witnesses = empty_witnesses(&zkbin)?;
+    let mut verif_group = c.benchmark_group("verify");
+    for k in 11..20 {
+        let proving_key = ProvingKey::build(k, &circuit.clone());
+        let proof =
+            Proof::create(&proving_key, &[circuit.clone()], &public_inputs, &mut OsRng).unwrap();
+        let verifier_witnesses = empty_witnesses(&zkbin).unwrap();
         let circuit = ZkCircuit::new(verifier_witnesses, &zkbin);
         let verifying_key = VerifyingKey::build(k, &circuit);
-        let mut total = 0;
-        for _ in 0..SAMPLES {
-            let now = std::time::Instant::now();
-            proof.verify(&verifying_key, &public_inputs)?;
-            total += now.elapsed().as_millis();
-        }
-        println!("Avg verification time: {} ms", total / SAMPLES);
-    }
 
-    Ok(())
+        verif_group.bench_with_input(BenchmarkId::from_parameter(k), &k, |b, &_k| {
+            b.iter(|| proof.verify(&verifying_key, &public_inputs))
+        });
+    }
+    verif_group.finish();
 }
+
+criterion_group!(benches, zk_arith_bench);
+criterion_main!(benches);