Skip to content

Commit

Permalink
add benches for prove() and infer()
Browse files Browse the repository at this point in the history
  • Loading branch information
bddap committed Apr 22, 2021
1 parent ce92942 commit 5ac1e27
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 0 deletions.
5 changes: 5 additions & 0 deletions benches/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/target
/pkg
/wasm-pack.log
/tmp
/Cargo.lock
8 changes: 8 additions & 0 deletions benches/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "benches"
version = "0.1.0"
authors = ["Andrew Dirksen <[email protected]>"]
edition = "2018"

[dependencies]
rify = { path = ".." }
1 change: 1 addition & 0 deletions benches/rust-toolchain
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nightly
123 changes: 123 additions & 0 deletions benches/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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<rify::Rule<&'static str, &'static str>> {
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<Unbound: Ord + Debug + Clone, Bound: Ord + Clone>(
rs: &[[&[[rify::Entity<Unbound, Bound>; 4]]; 2]],
) -> Vec<rify::Rule<Unbound, Bound>> {
rs.iter()
.map(|[ifa, then]| rify::Rule::create(ifa.to_vec(), then.to_vec()).unwrap())
.collect()
}
6 changes: 6 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,16 @@ 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

# remove artifacts from js bindings tests
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

0 comments on commit 5ac1e27

Please sign in to comment.