From 5ac1e27035f4b935ff6ee9856f911a55bb1c67b3 Mon Sep 17 00:00:00 2001 From: Andrew Dirksen Date: Thu, 22 Apr 2021 11:41:32 -0700 Subject: [PATCH] add benches for prove() and infer() --- benches/.gitignore | 5 ++ benches/Cargo.toml | 8 +++ benches/rust-toolchain | 1 + benches/src/lib.rs | 123 +++++++++++++++++++++++++++++++++++++++++ justfile | 6 ++ 5 files changed, 143 insertions(+) create mode 100644 benches/.gitignore create mode 100644 benches/Cargo.toml create mode 100644 benches/rust-toolchain create mode 100644 benches/src/lib.rs diff --git a/benches/.gitignore b/benches/.gitignore new file mode 100644 index 0000000..0363452 --- /dev/null +++ b/benches/.gitignore @@ -0,0 +1,5 @@ +/target +/pkg +/wasm-pack.log +/tmp +/Cargo.lock diff --git a/benches/Cargo.toml b/benches/Cargo.toml new file mode 100644 index 0000000..343742c --- /dev/null +++ b/benches/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "benches" +version = "0.1.0" +authors = ["Andrew Dirksen "] +edition = "2018" + +[dependencies] +rify = { path = ".." } diff --git a/benches/rust-toolchain b/benches/rust-toolchain new file mode 100644 index 0000000..bf867e0 --- /dev/null +++ b/benches/rust-toolchain @@ -0,0 +1 @@ +nightly diff --git a/benches/src/lib.rs b/benches/src/lib.rs new file mode 100644 index 0000000..c5fd2df --- /dev/null +++ b/benches/src/lib.rs @@ -0,0 +1,123 @@ +#![cfg(test)] +#![feature(test)] + +extern crate test; + +use core::fmt::Debug; +use rify::Entity::{Bound as B, Unbound as U}; +use rify::*; +use test::Bencher; + +const DG: &str = "default_graph"; + +mod ancestry { + use super::*; + const PARENT: &str = "parent"; + const ANCESTOR: &str = "ancestor"; + + fn rules() -> Vec> { + decl_rules(&[ + [ + &[[U("a"), B(PARENT), U("b"), B(DG)]], + &[[U("a"), B(ANCESTOR), U("b"), B(DG)]], + ], + [ + &[ + [U("a"), B(ANCESTOR), U("b"), B(DG)], + [U("b"), B(ANCESTOR), U("c"), B(DG)], + ], + &[[U("a"), B(ANCESTOR), U("c"), B(DG)]], + ], + ]) + } + + // Contains intentional leak; don't use outside of tests. + fn facts() -> Vec<[&'static str; 4]> { + let nodes: Vec<&str> = (0..20) + .map(|n| Box::leak(format!("node_{}", n).into_boxed_str()) as &str) + .collect(); + let facts: Vec<[&str; 4]> = nodes + .iter() + .zip(nodes.iter().cycle().skip(1)) + .map(|(a, b)| [a, PARENT, b, DG]) + .collect(); + facts + } + + #[bench] + fn infer_(b: &mut Bencher) { + let facts = facts(); + let rules = rules(); + b.iter(|| infer(&facts, &rules)); + } + + #[bench] + fn prove_(b: &mut Bencher) { + let facts = facts(); + let rules = rules(); + b.iter(|| prove(&facts, &[[PARENT, PARENT, PARENT, PARENT]], &rules)); + } +} + +mod recursion_minimal { + use super::*; + + const RULES: &[[&[[rify::Entity<&str, &str>; 4]]; 2]] = &[ + [ + &[ + [B("andrew"), B("claims"), U("c"), B(DG)], + [U("c"), B("subject"), U("s"), B(DG)], + [U("c"), B("property"), U("p"), B(DG)], + [U("c"), B("object"), U("o"), B(DG)], + ], + &[[U("s"), U("p"), U("o"), B(DG)]], + ], + [ + &[ + [U("person_a"), B("is"), B("awesome"), B(DG)], + [U("person_a"), B("friendswith"), U("person_b"), B(DG)], + ], + &[[U("person_b"), B("is"), B("awesome"), B(DG)]], + ], + [ + &[[U("person_a"), B("friendswith"), U("person_b"), B(DG)]], + &[[U("person_b"), B("friendswith"), U("person_a"), B(DG)]], + ], + ]; + + const FACTS: &[[&str; 4]] = &[ + ["soyoung", "friendswith", "nick", DG], + ["nick", "friendswith", "elina", DG], + ["elina", "friendswith", "sam", DG], + ["sam", "friendswith", "fausto", DG], + ["fausto", "friendswith", "lovesh", DG], + ["andrew", "claims", "_:claim1", DG], + ["_:claim1", "subject", "lovesh", DG], + ["_:claim1", "property", "is", DG], + ["_:claim1", "object", "awesome", DG], + ]; + + #[bench] + fn infer_(b: &mut Bencher) { + let rules = decl_rules(RULES); + b.iter(|| infer(FACTS, &rules)); + } + + #[bench] + fn prove_(b: &mut Bencher) { + let rules = decl_rules(RULES); + let composite_claims: &[[&str; 4]] = &[ + ["soyoung", "is", "awesome", "default_graph"], + ["nick", "is", "awesome", "default_graph"], + ]; + b.iter(|| prove(FACTS, composite_claims, &rules)); + } +} + +pub fn decl_rules( + rs: &[[&[[rify::Entity; 4]]; 2]], +) -> Vec> { + rs.iter() + .map(|[ifa, then]| rify::Rule::create(ifa.to_vec(), then.to_vec()).unwrap()) + .collect() +} diff --git a/justfile b/justfile index 301281a..80bb7e6 100644 --- a/justfile +++ b/justfile @@ -30,6 +30,7 @@ js-test: # remove dist and node_modules from js bindings tests clean: cargo clean + cd benches; cargo clean rm -r bindings/js_wasm/pkg || true just clean-js @@ -37,3 +38,8 @@ clean: clean-js: rm -r bindings/js_wasm/binding_tests/dist || true rm -r bindings/js_wasm/binding_tests/node_modules || true + +bench: + #!/usr/bin/env bash + cd benches + cargo bench