Skip to content

Commit

Permalink
Migrate benches
Browse files Browse the repository at this point in the history
  • Loading branch information
bakaq committed Oct 13, 2024
1 parent 495ed1a commit 463d44a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 19 deletions.
4 changes: 2 additions & 2 deletions benches/run_iai.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ mod setup;
mod iai {
use iai_callgrind::{library_benchmark, library_benchmark_group, main};

use scryer_prolog::QueryResolution;
use scryer_prolog::LeafAnswer;

use super::setup;

#[library_benchmark]
#[bench::count_edges(setup::prolog_benches()["count_edges"].setup())]
#[bench::numlist(setup::prolog_benches()["numlist"].setup())]
#[bench::csv_codename(setup::prolog_benches()["csv_codename"].setup())]
fn bench(mut run: impl FnMut() -> QueryResolution) -> QueryResolution {
fn bench(mut run: impl FnMut() -> Vec<LeafAnswer>) -> Vec<LeafAnswer> {
run()
}

Expand Down
31 changes: 20 additions & 11 deletions benches/setup.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{collections::BTreeMap, fs, path::Path};

use maplit::btreemap;
use scryer_prolog::{Machine, QueryResolution, Value};
use scryer_prolog::{LeafAnswer, Machine, MachineBuilder, Term};

pub fn prolog_benches() -> BTreeMap<&'static str, PrologBenchmark> {
[
Expand All @@ -10,21 +10,21 @@ pub fn prolog_benches() -> BTreeMap<&'static str, PrologBenchmark> {
"benches/edges.pl", // name of the prolog module file to load. use the same file in multiple benchmarks
"independent_set_count(ky, Count).", // query to benchmark in the context of the loaded module. consider making the query adjustable to tune the run time to ~0.1s
Strategy::Reuse,
btreemap! { "Count" => Value::Integer(2869176.into()) },
btreemap! { "Count" => Term::integer(2869176) },
),
(
"numlist",
"benches/numlist.pl",
"run_numlist(1000000, Head).",
Strategy::Reuse,
btreemap! { "Head" => Value::Integer(1.into())},
btreemap! { "Head" => Term::integer(1) },
),
(
"csv_codename",
"benches/csv.pl",
"get_codename(\"0020\",Name).",
Strategy::Reuse,
btreemap! { "Name" => Value::String("SPACE".into())},
btreemap! { "Name" => Term::string("SPACE") },
),
]
.map(|b| {
Expand Down Expand Up @@ -54,7 +54,7 @@ pub struct PrologBenchmark {
pub filename: &'static str,
pub query: &'static str,
pub strategy: Strategy,
pub bindings: BTreeMap<&'static str, Value>,
pub bindings: BTreeMap<&'static str, Term>,
}

impl PrologBenchmark {
Expand All @@ -64,28 +64,34 @@ impl PrologBenchmark {
.file_stem()
.and_then(|s| s.to_str())
.unwrap();
let mut machine = Machine::new_lib();
let mut machine = MachineBuilder::default().build();
machine.load_module_string(module_name, program);
machine
}

#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
pub fn setup(&self) -> impl FnMut() -> QueryResolution {
pub fn setup(&self) -> impl FnMut() -> Vec<LeafAnswer> {
let mut machine = self.make_machine();
let query = self.query;
move || {
use criterion::black_box;
black_box(machine.run_query(black_box(query.to_string()))).unwrap()
black_box(
machine
.run_query(black_box(query))
.collect::<Result<Vec<_>, _>>()
.unwrap(),
)
}
}
}

#[cfg(test)]
mod test {

#[test]
fn validate_benchmarks() {
use super::prolog_benches;
use scryer_prolog::{QueryMatch, QueryResolution};
use scryer_prolog::LeafAnswer;
use std::{fmt::Write, fs};

struct BenchResult {
Expand All @@ -100,10 +106,13 @@ mod test {
let mut machine = r.make_machine();
let setup_inference_count = machine.get_inference_count();

let result = machine.run_query(r.query.to_string()).unwrap();
let result: Vec<_> = machine
.run_query(r.query)
.collect::<Result<_, _>>()
.unwrap();
let query_inference_count = machine.get_inference_count() - setup_inference_count;

let expected = QueryResolution::Matches(vec![QueryMatch::from(r.bindings.clone())]);
let expected = [LeafAnswer::from_bindings(r.bindings.clone())];
assert_eq!(result, expected, "validating benchmark {}", r.name);

results.push(BenchResult {
Expand Down
6 changes: 2 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

#[macro_use]
extern crate static_assertions;
#[cfg(test)]
#[macro_use]
extern crate maplit;

#[macro_use]
pub(crate) mod macros;
Expand Down Expand Up @@ -50,14 +47,15 @@ pub use machine::config::*;
pub use machine::lib_machine::*;
pub use machine::Machine;

/// Eval a source file in Wasm.
#[cfg(target_arch = "wasm32")]
#[wasm_bindgen]
pub fn eval_code(s: &str) -> String {
use machine::mock_wam::*;

console_error_panic_hook::set_once();

let mut wam = Machine::with_test_streams();
let mut wam = MachineBuilder::default().build();
let bytes = wam.test_load_string(s);
String::from_utf8_lossy(&bytes).to_string()
}
Expand Down
7 changes: 5 additions & 2 deletions src/machine/lib_machine/lib_machine_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ fn complicated_term() {
Term::String("asdf".into()), // String
Term::List(vec![
Term::Integer(42.into()), // Fixnum
Term::Float(2.54.into()), // Float
Term::Float(2.54), // Float
Term::Atom("asdf".into()), // Atom
Term::Atom("a".into()), // Char
Term::Compound(
Expand Down Expand Up @@ -542,7 +542,10 @@ fn differentiate_anonymous_variables() {
fn order_of_variables_in_binding() {
let mut machine = MachineBuilder::default().build();

let complete_answer: Vec<_> = machine.run_query("X = Y, Z = W.").collect::<Result<_,_>>().unwrap();
let complete_answer: Vec<_> = machine
.run_query("X = Y, Z = W.")
.collect::<Result<_, _>>()
.unwrap();

assert_eq!(
complete_answer,
Expand Down

0 comments on commit 463d44a

Please sign in to comment.