use std::{env, path::PathBuf, process::Command, time::Duration};
use anyhow::Result;
use arci::{DummyJointTrajectoryClient, JointTrajectoryClient};
use criterion::{criterion_group, criterion_main, Criterion};
use fs_err as fs;
use openrr_plugin::{JointTrajectoryClientProxy, PluginProxy};
fn no_proxy_joint_names(c: &mut Criterion) {
let joint_names: Vec<_> = (0..100).map(|n| n.to_string()).collect();
let client = DummyJointTrajectoryClient::new(joint_names);
c.bench_function("no_proxy_joint_names", |b| b.iter(|| client.joint_names()));
}
fn no_proxy_current_joint_positions(c: &mut Criterion) {
let joint_names: Vec<_> = (0..100).map(|n| n.to_string()).collect();
let client = DummyJointTrajectoryClient::new(joint_names);
c.bench_function("no_proxy_current_joint_positions", |b| {
b.iter(|| client.current_joint_positions().unwrap())
});
}
fn no_proxy_send_joint_positions(c: &mut Criterion) {
let joint_names: Vec<_> = (0..100).map(|n| n.to_string()).collect();
let positions: Vec<_> = (0..joint_names.len()).map(|n| n as f64).collect();
let client = DummyJointTrajectoryClient::new(joint_names);
c.bench_function("no_proxy_send_joint_positions", |b| {
b.iter(|| {
client
.send_joint_positions(positions.clone(), Duration::default())
.unwrap()
})
});
}
fn proxy_same_crate_joint_names(c: &mut Criterion) {
let joint_names: Vec<_> = (0..100).map(|n| n.to_string()).collect();
let client = DummyJointTrajectoryClient::new(joint_names);
let client = JointTrajectoryClientProxy::new(client);
c.bench_function("proxy_same_crate_joint_names", |b| {
b.iter(|| client.joint_names())
});
}
fn proxy_same_crate_current_joint_positions(c: &mut Criterion) {
let joint_names: Vec<_> = (0..100).map(|n| n.to_string()).collect();
let client = DummyJointTrajectoryClient::new(joint_names);
let client = JointTrajectoryClientProxy::new(client);
c.bench_function("proxy_same_crate_current_joint_positions", |b| {
b.iter(|| client.current_joint_positions().unwrap())
});
}
fn proxy_same_crate_send_joint_positions(c: &mut Criterion) {
let joint_names: Vec<_> = (0..100).map(|n| n.to_string()).collect();
let positions: Vec<_> = (0..joint_names.len()).map(|n| n as f64).collect();
let client = DummyJointTrajectoryClient::new(joint_names);
let client = JointTrajectoryClientProxy::new(client);
c.bench_function("proxy_same_crate_send_joint_positions", |b| {
b.iter(|| {
client
.send_joint_positions(positions.clone(), Duration::default())
.unwrap()
})
});
}
fn proxy_diff_crate_joint_names(c: &mut Criterion) {
let plugin_path = test_plugin().unwrap();
let plugin = PluginProxy::from_path(plugin_path).unwrap();
let joint_names: Vec<_> = (0..100).map(|n| n.to_string()).collect();
let client = plugin
.new_joint_trajectory_client(format!(r#"{{ "joint_names": {joint_names:?} }}"#))
.unwrap()
.unwrap();
c.bench_function("proxy_diff_crate_joint_names", |b| {
b.iter(|| client.joint_names())
});
}
fn proxy_diff_crate_current_joint_positions(c: &mut Criterion) {
let plugin_path = test_plugin().unwrap();
let plugin = PluginProxy::from_path(plugin_path).unwrap();
let joint_names: Vec<_> = (0..100).map(|n| n.to_string()).collect();
let client = plugin
.new_joint_trajectory_client(format!(r#"{{ "joint_names": {joint_names:?} }}"#))
.unwrap()
.unwrap();
c.bench_function("proxy_diff_crate_current_joint_positions", |b| {
b.iter(|| client.current_joint_positions().unwrap())
});
}
fn proxy_diff_crate_send_joint_positions(c: &mut Criterion) {
let plugin_path = test_plugin().unwrap();
let plugin = PluginProxy::from_path(plugin_path).unwrap();
let joint_names: Vec<_> = (0..100).map(|n| n.to_string()).collect();
let positions: Vec<_> = (0..joint_names.len()).map(|n| n as f64).collect();
let client = plugin
.new_joint_trajectory_client(format!(r#"{{ "joint_names": {joint_names:?} }}"#))
.unwrap()
.unwrap();
c.bench_function("proxy_diff_crate_send_joint_positions", |b| {
b.iter(|| {
client
.send_joint_positions(positions.clone(), Duration::default())
.unwrap()
})
});
}
fn root_dir() -> PathBuf {
let mut dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
dir.pop(); dir
}
fn test_plugin() -> Result<PathBuf> {
let root_dir = root_dir();
let cargo_toml = format!(
r#"
[package]
name = "test_plugin"
version = "0.0.0"
edition = "2021"
publish = false
[workspace]
[lib]
crate-type = ["cdylib"]
[dependencies]
abi_stable = "0.9"
arci = {{ path = "{0}/arci" }}
openrr-plugin = {{ path = "{0}/openrr-plugin" }}
serde = {{ version = "1", features = ["derive"] }}
serde_json = "1"
[patch.crates-io]
arci = {{ path = "{0}/arci" }}
openrr-plugin = {{ path = "{0}/openrr-plugin" }}
"#,
root_dir.display()
);
let lib_rs = r#"
use arci::DummyJointTrajectoryClient;
use serde::Deserialize;
openrr_plugin::export_plugin!(TestPlugin);
pub struct TestPlugin;
impl openrr_plugin::Plugin for TestPlugin {
fn new_joint_trajectory_client(
&self,
args: String,
) -> Result<Option<Box<dyn arci::JointTrajectoryClient>>, arci::Error> {
let config: TestClientConfig =
serde_json::from_str(&args).map_err(|e| arci::Error::Other(e.into()))?;
Ok(Some(Box::new(DummyJointTrajectoryClient::new(config.joint_names))))
}
}
#[derive(Deserialize)]
struct TestClientConfig {
joint_names: Vec<String>,
}
"#;
let tmpdir_path = root_dir.join("target/test_plugin");
for (path, contents) in [
("src", None),
("src/lib.rs", Some(lib_rs)),
("Cargo.toml", Some(&*cargo_toml)),
] {
let tmppath = &tmpdir_path.join(path);
if let Some(contents) = contents {
fs::write(tmppath, contents)?;
} else if !tmppath.exists() {
fs::create_dir_all(tmppath)?;
}
}
let status = Command::new("cargo")
.args(["build", "--release", "--manifest-path"])
.arg(&tmpdir_path.join("Cargo.toml"))
.status()?;
assert!(status.success());
let plugin_path = tmpdir_path
.join("target/release")
.join(format!("libtest_plugin.{}", env::consts::DLL_EXTENSION));
Ok(plugin_path)
}
criterion_group!(
benches,
no_proxy_joint_names,
no_proxy_current_joint_positions,
no_proxy_send_joint_positions,
proxy_same_crate_joint_names,
proxy_same_crate_current_joint_positions,
proxy_same_crate_send_joint_positions,
proxy_diff_crate_joint_names,
proxy_diff_crate_current_joint_positions,
proxy_diff_crate_send_joint_positions
);
criterion_main!(benches);