use plux_mock::{MockManager, MockPlugin};
use plux_rs::prelude::*;
use std::collections::HashMap;
use std::thread;
use std::time::{Duration, Instant};
use crate::plugins::benchmark;
use crate::plugins::utils::get_plugin_path;
mod plugins;
#[plux_rs::function]
fn calculate(_: (), a: &i32, b: &i32) -> i32 {
thread::sleep(Duration::from_millis(10));
a + b * 2
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut plugins: HashMap<Bundle, MockPlugin<FunctionOutput>> = HashMap::new();
benchmark::plugin1_v1::insert_plugin(&mut plugins);
benchmark::plugin2_v1::insert_plugin(&mut plugins);
benchmark::plugin3_v1::insert_plugin(&mut plugins);
let mut loader = SimpleLoader::new();
loader.context(move |mut ctx| {
ctx.register_manager(MockManager::from_plugins(plugins))?;
ctx.register_function(calculate());
ctx.register_request(Request::new(
"compute".to_string(),
vec![VariableType::I32, VariableType::I32],
Some(VariableType::I32),
));
Ok::<(), Box<dyn std::error::Error>>(())
})?;
let paths = [
benchmark::plugin1_v1::FILENAME,
benchmark::plugin2_v1::FILENAME,
benchmark::plugin3_v1::FILENAME,
]
.into_iter()
.map(|filename| get_plugin_path(format!("benchmark/{}", filename)))
.collect::<Vec<_>>();
let start = Instant::now();
let bundles = loader
.load_plugins(paths.iter().map(|path| path.to_str().unwrap()))
.unwrap();
let load_time = start.elapsed();
println!("Loaded {} plugins in {:?}", bundles.len(), load_time);
let start = Instant::now();
for _ in 0..100 {
for bundle in &bundles {
let plugin = loader.get_plugin_by_bundle(bundle).unwrap();
let _ = plugin
.call_request("compute", &[10.into(), 20.into()])
.unwrap();
}
}
let exec_time = start.elapsed();
println!("Executed plugin functions 3000 times in {:?}", exec_time);
let start = Instant::now();
for _ in 0..100 {
let _ = loader
.par_call_request("compute", &[10.into(), 20.into()])
.unwrap();
}
let parallel_exec_time = start.elapsed();
println!(
"Executed plugin functions 3000 times in parallel in {:?}",
parallel_exec_time
);
for bundle in &bundles {
loader.unload_plugin_by_bundle(bundle).unwrap();
}
loader.stop().unwrap();
Ok(())
}