#![cfg(all(feature = "vtn", feature = "sqlite"))]
use std::sync::Arc;
use std::time::{Duration, Instant};
use axum::{
Router,
body::Body,
http::{Method, Request, StatusCode, header},
};
use openadr::{
core::FixedClock,
model::{ClientId, Timestamp},
vtn::{
DispatchConfig, Vtn, VtnConfig,
auth::StaticTokenAuth,
notify::{Notifiers, RecordingNotifier},
store::{MemoryStorage, SharedStorage, SqliteStorage},
},
};
use serde_json::{Value, json};
use tower::ServiceExt;
const BL: &str = "bl-secret";
fn now() -> Timestamp {
"2026-02-11T06:00:00Z".parse().unwrap()
}
struct Timing {
samples: Vec<Duration>,
}
impl Timing {
fn new() -> Self {
Self {
samples: Vec::new(),
}
}
fn record(&mut self, d: Duration) {
self.samples.push(d);
}
fn ms(&mut self, percentile: f64) -> f64 {
self.samples.sort();
if self.samples.is_empty() {
return 0.0;
}
let index = ((self.samples.len() as f64 - 1.0) * percentile).round() as usize;
self.samples[index].as_secs_f64() * 1000.0
}
}
struct Harness {
router: Router,
vtn: Vtn,
notifier: Arc<RecordingNotifier>,
}
impl Harness {
async fn new(storage: SharedStorage, mqtt: bool) -> Self {
let notifier = RecordingNotifier::shared();
let mut auth = StaticTokenAuth::new("http://vtn.test/auth/token")
.with_business_logic(BL, ClientId::new("bl").unwrap());
for n in 0..64 {
auth = auth.with_ven(
format!("ven-token-{n}"),
ClientId::new(format!("client-{n}")).unwrap(),
);
}
let mut builder = Vtn::builder()
.storage(storage.clone())
.authenticator(Arc::new(auth))
.clock(Arc::new(FixedClock::new(now())))
.notifier(Notifiers::new().with(notifier.clone()).shared())
.config(VtnConfig {
base_path: "/openadr3/3.1.0".into(),
mqtt_topic_prefix: "openadr3".into(),
..Default::default()
});
if mqtt {
builder = builder.mqtt(openadr::model::MqttNotifierBinding {
uris: vec!["mqtts://broker.test:8883".into()],
serialization: openadr::model::Serialization::Json,
authentication: openadr::model::MqttAuthentication::Anonymous,
});
}
let vtn = builder.build();
Self {
router: vtn.router(),
notifier,
vtn,
}
}
async fn send(&self, method: Method, path: &str, body: Option<Value>) -> (StatusCode, Value) {
self.send_as(BL, method, path, body).await
}
async fn send_as(
&self,
token: &str,
method: Method,
path: &str,
body: Option<Value>,
) -> (StatusCode, Value) {
let req = Request::builder()
.method(method)
.uri(format!("/openadr3/3.1.0{path}"))
.header(header::AUTHORIZATION, format!("Bearer {token}"));
let req = match body {
Some(b) => req
.header(header::CONTENT_TYPE, "application/json")
.body(Body::from(serde_json::to_vec(&b).unwrap()))
.unwrap(),
None => req.body(Body::empty()).unwrap(),
};
let response = self.router.clone().oneshot(req).await.unwrap();
let status = response.status();
let bytes = axum::body::to_bytes(response.into_body(), usize::MAX)
.await
.unwrap();
let value = serde_json::from_slice(&bytes).unwrap_or(Value::Null);
(status, value)
}
async fn program(&self) -> String {
let (status, body) = self
.send(
Method::POST,
"/programs",
Some(json!({ "programName": format!("load-{}", uid()) })),
)
.await;
assert_eq!(status, StatusCode::CREATED, "{body}");
body["id"].as_str().unwrap().to_string()
}
async fn subscriptions(&self, count: usize) {
for n in 0..count {
let (status, body) = self
.send(
Method::POST,
"/subscriptions",
Some(json!({
"clientName": format!("sub-{}-{n}", uid()),
"objectOperations": [{
"objects": ["EVENT"],
"operations": ["CREATE"],
"callbackUrl": format!("https://subscriber-{n}.example.com/hook"),
}],
})),
)
.await;
assert_eq!(status, StatusCode::CREATED, "{body}");
}
}
async fn vens(&self, count: usize) {
for n in 0..count {
let (status, body) = self
.send(
Method::POST,
"/vens",
Some(json!({
"objectType": "BL_VEN_REQUEST",
"clientID": format!("client-{}-{n}", uid()),
"venName": format!("ven-{}-{n}", uid()),
"targets": ["fleet"],
})),
)
.await;
assert_eq!(status, StatusCode::CREATED, "{body}");
}
}
}
async fn measuring() -> tokio::sync::MutexGuard<'static, ()> {
static LOCK: tokio::sync::Mutex<()> = tokio::sync::Mutex::const_new(());
LOCK.lock().await
}
fn uid() -> u64 {
use std::sync::atomic::{AtomicU64, Ordering};
static N: AtomicU64 = AtomicU64::new(0);
N.fetch_add(1, Ordering::Relaxed)
}
fn event_body(program: &str) -> Value {
json!({
"programID": program,
"intervalPeriod": { "start": "2026-02-11T07:00:00Z", "duration": "PT1H" },
"intervals": [{ "id": 0, "payloads": [{ "type": "PRICE", "values": [0.11] }] }],
})
}
enum Backend {
Memory,
Sqlite(std::path::PathBuf),
#[cfg(feature = "postgres")]
Postgres(String),
}
impl Backend {
async fn fresh(&self) -> SharedStorage {
match self {
Backend::Memory => MemoryStorage::shared(),
Backend::Sqlite(dir) => {
let path = dir.join(format!("load-{}.sqlite", uid()));
SqliteStorage::shared(path.to_str().unwrap())
.await
.expect("a fresh SQLite file")
}
#[cfg(feature = "postgres")]
Backend::Postgres(url) => {
let store = openadr::vtn::store::PostgresStorage::open(url)
.await
.expect("the server answered a moment ago");
store.truncate_all().await.expect("a clean database");
Arc::new(store) as SharedStorage
}
}
}
}
async fn backends() -> Vec<(&'static str, Backend)> {
let mut out: Vec<(&'static str, Backend)> = vec![("memory", Backend::Memory)];
let dir = std::env::temp_dir().join(format!("openadr-load-{}", std::process::id()));
if std::fs::create_dir_all(&dir).is_ok() {
out.push(("sqlite", Backend::Sqlite(dir)));
}
#[cfg(feature = "postgres")]
if let Ok(url) = std::env::var("OPENADR_TEST_POSTGRES") {
match openadr::vtn::store::PostgresStorage::open(&url).await {
Ok(_) => out.push(("postgres", Backend::Postgres(url))),
Err(e) => eprintln!("postgres unavailable ({e}); skipping that backend"),
}
}
out
}
#[tokio::test(flavor = "multi_thread")]
#[ignore = "a measurement, not an assertion: run with --ignored --nocapture"]
async fn write_latency_against_subscriber_count() {
let _measuring = measuring().await;
println!("\n== POST /events, by matching subscriptions ==");
println!(
"{:<10} {:>12} {:>10} {:>10} {:>12}",
"backend", "subscribers", "p50 ms", "p95 ms", "queued/write"
);
for (name, backend) in backends().await {
for subscribers in [0usize, 100, 1_000] {
let h = Harness::new(backend.fresh().await, false).await;
let program = h.program().await;
h.subscriptions(subscribers).await;
let mut timing = Timing::new();
for _ in 0..20 {
let started = Instant::now();
let (status, body) = h
.send(Method::POST, "/events", Some(event_body(&program)))
.await;
timing.record(started.elapsed());
assert_eq!(status, StatusCode::CREATED, "{body}");
}
let queued = h.vtn.dispatcher().stats().await.pending;
println!(
"{name:<10} {subscribers:>12} {:>10.2} {:>10.2} {:>12}",
timing.ms(0.50),
timing.ms(0.95),
queued / 20,
);
h.vtn.dispatcher().drain().await;
let _ = h.notifier.delivered();
}
}
}
#[tokio::test(flavor = "multi_thread")]
#[ignore = "a measurement, not an assertion: run with --ignored --nocapture"]
async fn broker_fanout_against_ven_count() {
let _measuring = measuring().await;
println!("\n== POST /events with a broker, by VEN count ==");
println!(
"{:<10} {:>8} {:>10} {:>10} {:>12}",
"backend", "vens", "p50 ms", "p95 ms", "queued/write"
);
for (name, backend) in backends().await {
for vens in [0usize, 100, 1_000] {
let h = Harness::new(backend.fresh().await, true).await;
let program = h.program().await;
h.vens(vens).await;
let mut timing = Timing::new();
for _ in 0..10 {
let started = Instant::now();
let (status, body) = h
.send(Method::POST, "/events", Some(event_body(&program)))
.await;
timing.record(started.elapsed());
assert_eq!(status, StatusCode::CREATED, "{body}");
}
let queued = h.vtn.dispatcher().stats().await.pending;
println!(
"{name:<10} {vens:>8} {:>10.2} {:>10.2} {:>12}",
timing.ms(0.50),
timing.ms(0.95),
queued / 10,
);
h.vtn.dispatcher().drain().await;
let _ = h.notifier.delivered();
}
}
}
#[tokio::test(flavor = "multi_thread")]
#[ignore = "a measurement, not an assertion: run with --ignored --nocapture"]
async fn drain_rate_by_concurrency() {
let _measuring = measuring().await;
println!("\n== outbox drain, 2 000 entries ==");
println!(
"{:<10} {:>12} {:>12} {:>14}",
"backend", "concurrency", "seconds", "entries/sec"
);
for (name, backend) in backends().await {
for concurrency in [1usize, 8, 64] {
let storage = backend.fresh().await;
let h = Harness::new(storage.clone(), false).await;
let program = h.program().await;
h.subscriptions(100).await;
for _ in 0..20 {
h.send(Method::POST, "/events", Some(event_body(&program)))
.await;
}
let dispatcher = openadr::vtn::Dispatcher::new(
storage.clone(),
h.notifier.clone(),
Arc::new(FixedClock::new(now())),
DispatchConfig {
concurrency,
batch: 128,
..Default::default()
},
);
let started = Instant::now();
let drained = dispatcher.drain().await;
let elapsed = started.elapsed().as_secs_f64();
println!(
"{name:<10} {concurrency:>12} {elapsed:>12.3} {:>14.0}",
drained as f64 / elapsed.max(f64::EPSILON)
);
let _ = h.notifier.delivered();
}
}
}
#[tokio::test(flavor = "multi_thread")]
#[ignore = "a measurement, not an assertion: run with --ignored --nocapture"]
async fn report_ingest_rate() {
let _measuring = measuring().await;
println!("\n== POST /reports, 200 reports ==");
println!(
"{:<10} {:>12} {:>6} {:>7} {:>10} {:>10} {:>14}",
"backend", "subscribers", "vens", "broker", "p50 ms", "p95 ms", "reports/sec"
);
for (name, backend) in backends().await {
for (subscribers, vens, mqtt) in [
(0usize, 0usize, false),
(1_000, 0, false),
(0, 0, true),
(0, 1_000, true),
] {
let h = Harness::new(backend.fresh().await, mqtt).await;
let program = h.program().await;
h.subscriptions(subscribers).await;
h.vens(vens).await;
if mqtt {
let (status, body) = h
.send(
Method::POST,
"/vens",
Some(json!({
"objectType": "BL_VEN_REQUEST",
"clientID": "client-0",
"venName": format!("owner-{}", uid()),
})),
)
.await;
assert_eq!(status, StatusCode::CREATED, "{body}");
}
let (status, event) = h
.send(Method::POST, "/events", Some(event_body(&program)))
.await;
assert_eq!(status, StatusCode::CREATED, "{event}");
let event_id = event["id"].as_str().unwrap().to_string();
h.vtn.dispatcher().drain().await;
let mut timing = Timing::new();
let started = Instant::now();
for n in 0..200 {
let body = json!({
"programID": program,
"eventID": event_id,
"clientName": format!("ven-{n}"),
"resources": [{
"resourceName": format!("meter-{n}"),
"intervals": [{
"id": 0,
"payloads": [{ "type": "USAGE", "values": [1.5] }],
}],
}],
});
let at = Instant::now();
let (status, body) = h
.send_as("ven-token-0", Method::POST, "/reports", Some(body))
.await;
timing.record(at.elapsed());
assert_eq!(status, StatusCode::CREATED, "{body}");
}
let elapsed = started.elapsed().as_secs_f64();
println!(
"{name:<10} {subscribers:>12} {vens:>6} {:>7} {:>10.2} {:>10.2} {:>14.0}",
if mqtt { "yes" } else { "no" },
timing.ms(0.50),
timing.ms(0.95),
200.0 / elapsed.max(f64::EPSILON)
);
h.vtn.dispatcher().drain().await;
let _ = h.notifier.delivered();
}
}
}