Migrate benches

This commit is contained in:
bakaq
2024-10-12 19:08:45 -03:00
parent e21c772181
commit 3d3baeed82
4 changed files with 29 additions and 19 deletions

View File

@@ -5,7 +5,7 @@ mod setup;
mod iai { mod iai {
use iai_callgrind::{library_benchmark, library_benchmark_group, main}; use iai_callgrind::{library_benchmark, library_benchmark_group, main};
use scryer_prolog::QueryResolution; use scryer_prolog::LeafAnswer;
use super::setup; use super::setup;
@@ -13,7 +13,7 @@ mod iai {
#[bench::count_edges(setup::prolog_benches()["count_edges"].setup())] #[bench::count_edges(setup::prolog_benches()["count_edges"].setup())]
#[bench::numlist(setup::prolog_benches()["numlist"].setup())] #[bench::numlist(setup::prolog_benches()["numlist"].setup())]
#[bench::csv_codename(setup::prolog_benches()["csv_codename"].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() run()
} }

View File

@@ -1,7 +1,7 @@
use std::{collections::BTreeMap, fs, path::Path}; use std::{collections::BTreeMap, fs, path::Path};
use maplit::btreemap; use maplit::btreemap;
use scryer_prolog::{Machine, QueryResolution, Value}; use scryer_prolog::{LeafAnswer, Machine, MachineBuilder, Term};
pub fn prolog_benches() -> BTreeMap<&'static str, PrologBenchmark> { 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 "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 "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, Strategy::Reuse,
btreemap! { "Count" => Value::Integer(2869176.into()) }, btreemap! { "Count" => Term::integer(2869176) },
), ),
( (
"numlist", "numlist",
"benches/numlist.pl", "benches/numlist.pl",
"run_numlist(1000000, Head).", "run_numlist(1000000, Head).",
Strategy::Reuse, Strategy::Reuse,
btreemap! { "Head" => Value::Integer(1.into())}, btreemap! { "Head" => Term::integer(1) },
), ),
( (
"csv_codename", "csv_codename",
"benches/csv.pl", "benches/csv.pl",
"get_codename(\"0020\",Name).", "get_codename(\"0020\",Name).",
Strategy::Reuse, Strategy::Reuse,
btreemap! { "Name" => Value::String("SPACE".into())}, btreemap! { "Name" => Term::string("SPACE") },
), ),
] ]
.map(|b| { .map(|b| {
@@ -54,7 +54,7 @@ pub struct PrologBenchmark {
pub filename: &'static str, pub filename: &'static str,
pub query: &'static str, pub query: &'static str,
pub strategy: Strategy, pub strategy: Strategy,
pub bindings: BTreeMap<&'static str, Value>, pub bindings: BTreeMap<&'static str, Term>,
} }
impl PrologBenchmark { impl PrologBenchmark {
@@ -64,28 +64,34 @@ impl PrologBenchmark {
.file_stem() .file_stem()
.and_then(|s| s.to_str()) .and_then(|s| s.to_str())
.unwrap(); .unwrap();
let mut machine = Machine::new_lib(); let mut machine = MachineBuilder::default().build();
machine.load_module_string(module_name, program); machine.load_module_string(module_name, program);
machine machine
} }
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))] #[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 mut machine = self.make_machine();
let query = self.query; let query = self.query;
move || { move || {
use criterion::black_box; 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)] #[cfg(test)]
mod test { mod test {
#[test] #[test]
fn validate_benchmarks() { fn validate_benchmarks() {
use super::prolog_benches; use super::prolog_benches;
use scryer_prolog::{QueryMatch, QueryResolution}; use scryer_prolog::LeafAnswer;
use std::{fmt::Write, fs}; use std::{fmt::Write, fs};
struct BenchResult { struct BenchResult {
@@ -100,10 +106,13 @@ mod test {
let mut machine = r.make_machine(); let mut machine = r.make_machine();
let setup_inference_count = machine.get_inference_count(); 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 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); assert_eq!(result, expected, "validating benchmark {}", r.name);
results.push(BenchResult { results.push(BenchResult {

View File

@@ -4,9 +4,6 @@
#[macro_use] #[macro_use]
extern crate static_assertions; extern crate static_assertions;
#[cfg(test)]
#[macro_use]
extern crate maplit;
#[macro_use] #[macro_use]
pub(crate) mod macros; pub(crate) mod macros;
@@ -50,6 +47,7 @@ pub use machine::config::*;
pub use machine::lib_machine::*; pub use machine::lib_machine::*;
pub use machine::Machine; pub use machine::Machine;
/// Eval a source file in Wasm.
#[cfg(target_arch = "wasm32")] #[cfg(target_arch = "wasm32")]
#[wasm_bindgen] #[wasm_bindgen]
pub fn eval_code(s: &str) -> String { pub fn eval_code(s: &str) -> String {
@@ -57,7 +55,7 @@ pub fn eval_code(s: &str) -> String {
console_error_panic_hook::set_once(); 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); let bytes = wam.test_load_string(s);
String::from_utf8_lossy(&bytes).to_string() String::from_utf8_lossy(&bytes).to_string()
} }

View File

@@ -420,7 +420,7 @@ fn complicated_term() {
Term::String("asdf".into()), // String Term::String("asdf".into()), // String
Term::List(vec![ Term::List(vec![
Term::Integer(42.into()), // Fixnum Term::Integer(42.into()), // Fixnum
Term::Float(2.54.into()), // Float Term::Float(2.54), // Float
Term::Atom("asdf".into()), // Atom Term::Atom("asdf".into()), // Atom
Term::Atom("a".into()), // Char Term::Atom("a".into()), // Char
Term::Compound( Term::Compound(
@@ -542,7 +542,10 @@ fn differentiate_anonymous_variables() {
fn order_of_variables_in_binding() { fn order_of_variables_in_binding() {
let mut machine = MachineBuilder::default().build(); 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!( assert_eq!(
complete_answer, complete_answer,