Migrate benches
This commit is contained in:
@@ -5,7 +5,7 @@ mod setup;
|
||||
mod iai {
|
||||
use iai_callgrind::{library_benchmark, library_benchmark_group, main};
|
||||
|
||||
use scryer_prolog::QueryResolution;
|
||||
use scryer_prolog::LeafAnswer;
|
||||
|
||||
use super::setup;
|
||||
|
||||
@@ -13,7 +13,7 @@ mod iai {
|
||||
#[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()
|
||||
}
|
||||
|
||||
|
||||
@@ -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> {
|
||||
[
|
||||
@@ -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| {
|
||||
@@ -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 {
|
||||
@@ -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 {
|
||||
@@ -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 {
|
||||
|
||||
@@ -4,9 +4,6 @@
|
||||
|
||||
#[macro_use]
|
||||
extern crate static_assertions;
|
||||
#[cfg(test)]
|
||||
#[macro_use]
|
||||
extern crate maplit;
|
||||
|
||||
#[macro_use]
|
||||
pub(crate) mod macros;
|
||||
@@ -50,6 +47,7 @@ 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 {
|
||||
@@ -57,7 +55,7 @@ pub fn eval_code(s: &str) -> String {
|
||||
|
||||
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()
|
||||
}
|
||||
|
||||
@@ -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(
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user