perf: round 4 — one-pass row paths, drive-selector cache, CalDAV single-parse, streamed Azure, batched hydration

Nine benchmark-gated changes (benches/ROUND4.md; every one ships with a
BEFORE/AFTER bench + equivalence gate, rollback rule as ROUND2/3):

- Row→entity path build: one-pass StoragePath::from_folder_and_name /
  from_joined + normalize_storage_name_owned + alloc-free Display —
  743→417 ns/file-row (1.78x), −5 allocs/row on every listing surface.
- WebDAV drive-selector: per-user readable_cache (single-flight, 30 s
  TTL, explicit invalidation incl. membership + group changes) replaces
  the grants join per request — 441 µs → 0.8 µs (~550x), 0 queries warm.
- CalDAV from_ical/update_ical_data: 8 full IcalParser runs per VEVENT
  → 1 (7.1x per PUT, 4.4x on 50-event imports); alloc-free split_vevents,
  chunk scan without the whole-body uppercase copy (1.4x), borrowed-key
  UID grouping (1.3x), REPORT props no longer cloned.
- PROPFIND emit: partition Vecs dropped (single-pass 404 list) + stack
  rendered RFC 3339/2822 dates, sizes, quoted etags (common::fmt,
  chrono-byte-identical, sweep-tested) on both DAV surfaces — 1.22x
  per page, 17.9→12.0 allocs/row.
- Grant-listing hydration: calendars/address books/playlists batch
  hydrate via = ANY($1) — 15 serial queries → 1 (~13x per sync poll).
- user-flags cache: get→insert → try_get_with single-flight (32→1
  queries per cold herd).
- Azure downloads: whole-blob Vec buffering → streamed SDK pages —
  TTFB 349→4 ms (87x), peak heap 480→1.9 MiB (254x) on 256 MiB blobs;
  new OXICLOUD_AZURE_ENDPOINT_URL override (Azurite/bench hook).
- Face indexing: unbounded per-image tokio::spawn → core-count
  semaphore, permit before blob read — peak heap 1175→176 MiB (6.7x).

Checks: cargo fmt, clippy --all-features --all-targets -D warnings,
cargo test --workspace (523 passed) + --features test_utils. hurl API
suite and dockerized integration DB not runnable in this environment —
left to CI.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017aJu9ghvuT8WqC31ZEGTBA
This commit is contained in:
Claude
2026-07-17 13:48:37 +00:00
parent 8a73607229
commit 12dc648cff
44 changed files with 5092 additions and 402 deletions
+398
View File
@@ -0,0 +1,398 @@
//! Azure download-path benchmark — whole-blob buffering vs streaming (ROUND4).
//!
//! The old `AzureBlobBackend::get_blob_stream` / `get_blob_range_stream`
//! drained the ENTIRE blob (or range) into one `Vec<u8>` before yielding
//! a single mega-chunk: whole-blob RAM residency per reader, TTFB = full
//! download time, and with `read_prefetch() = 8` the CDC reassembly path
//! could hold 8 entire chunk-blobs at once. AFTER forwards the SDK's
//! page/body streams directly (first page still awaited eagerly so a
//! missing blob is an up-front NotFound).
//!
//! Technique: a local axum stub speaks just enough of the Azure Blob GET
//! REST surface (ranged 16 MiB pages, `x-ms-*` headers) for the REAL
//! `azure_storage_blobs` client — the backend points at it via the new
//! `endpoint_url` override (also the Azurite hook). The stub synthesizes
//! blob bytes deterministically per offset, so it holds no buffer and
//! the peak-live-heap metric isolates the CLIENT path. BEFORE is the old
//! collect-everything logic copied verbatim; AFTER is the real
//! `AzureBlobBackend`. BLAKE3 gates assert byte-identical payloads.
//!
//! Run (no Postgres needed):
//! cargo run --release --features bench --example bench_azure_stream
//! Tunables (env): BENCH_MB (256) blob size, BENCH_TAIL_MB (128) range tail.
use std::alloc::{GlobalAlloc, Layout, System};
use std::env;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::Instant;
use axum::body::Body;
use axum::http::{HeaderMap, Request, Response, StatusCode};
use bytes::Bytes;
use futures::StreamExt;
use oxicloud::application::ports::blob_storage_ports::BlobStorageBackend;
use oxicloud::common::config::AzureStorageConfig;
use oxicloud::infrastructure::services::azure_blob_backend::AzureBlobBackend;
use tokio::net::TcpListener;
// ─── Peak-live-heap tracking allocator ──────────────────────────────────────
static LIVE: AtomicU64 = AtomicU64::new(0);
static PEAK: AtomicU64 = AtomicU64::new(0);
struct PeakAlloc;
fn bump(sz: u64) {
let live = LIVE.fetch_add(sz, Ordering::Relaxed) + sz;
PEAK.fetch_max(live, Ordering::Relaxed);
}
unsafe impl GlobalAlloc for PeakAlloc {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
bump(layout.size() as u64);
unsafe { System.alloc(layout) }
}
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
LIVE.fetch_sub(layout.size() as u64, Ordering::Relaxed);
unsafe { System.dealloc(ptr, layout) }
}
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
if new_size > layout.size() {
bump((new_size - layout.size()) as u64);
} else {
LIVE.fetch_sub((layout.size() - new_size) as u64, Ordering::Relaxed);
}
unsafe { System.realloc(ptr, layout, new_size) }
}
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
bump(layout.size() as u64);
unsafe { System.alloc_zeroed(layout) }
}
}
#[global_allocator]
static GLOBAL: PeakAlloc = PeakAlloc;
// ─── Deterministic blob content (no stored buffer) ──────────────────────────
fn splitmix64(mut z: u64) -> u64 {
z = z.wrapping_add(0x9E3779B97F4A7C15);
z = (z ^ (z >> 30)).wrapping_mul(0xBF58476D1CE4E5B9);
z = (z ^ (z >> 27)).wrapping_mul(0x94D049BB133111EB);
z ^ (z >> 31)
}
/// Fill `out` with the blob bytes at absolute offset `offset`.
fn fill_at(out: &mut [u8], offset: u64) {
let mut i = 0usize;
while i < out.len() {
let abs = offset + i as u64;
let block = abs / 8;
let word = splitmix64(block).to_le_bytes();
let start_in_word = (abs % 8) as usize;
let take = (8 - start_in_word).min(out.len() - i);
out[i..i + take].copy_from_slice(&word[start_in_word..start_in_word + take]);
i += take;
}
}
/// BLAKE3 of an arbitrary blob range, streamed in 1 MiB pieces.
fn expected_hash(offset: u64, len: u64) -> blake3::Hash {
let mut hasher = blake3::Hasher::new();
let mut buf = vec![0u8; 1 << 20];
let mut pos = 0u64;
while pos < len {
let take = ((len - pos) as usize).min(buf.len());
fill_at(&mut buf[..take], offset + pos);
hasher.update(&buf[..take]);
pos += take as u64;
}
hasher.finalize()
}
// ─── Azure Blob GET stub ────────────────────────────────────────────────────
fn parse_range(headers: &HeaderMap) -> Option<(u64, Option<u64>)> {
let raw = headers
.get("x-ms-range")
.or_else(|| headers.get("range"))?
.to_str()
.ok()?;
let spec = raw.strip_prefix("bytes=")?;
let (a, b) = spec.split_once('-')?;
let start: u64 = a.parse().ok()?;
let end: Option<u64> = if b.is_empty() { None } else { b.parse().ok() };
Some((start, end))
}
/// Serve GET {container}/{blob} with ranged responses in streamed 256 KiB
/// frames, synthesizing content per offset — the stub never holds the blob.
async fn stub_azure(blob_len: u64) -> String {
let listener = TcpListener::bind("127.0.0.1:0").await.expect("bind stub");
let addr = listener.local_addr().expect("stub addr");
let app = axum::Router::new().fallback(move |req: Request<Body>| async move {
if req.method() != axum::http::Method::GET {
return Response::builder()
.status(StatusCode::CREATED)
.header("etag", "\"0x1\"")
.header("last-modified", "Thu, 01 Jan 2026 00:00:00 GMT")
.header("x-ms-request-id", "11111111-1111-1111-1111-111111111111")
.header("date", "Thu, 01 Jan 2026 00:00:00 GMT")
.body(Body::empty())
.unwrap();
}
let (start, end_incl) = parse_range(req.headers()).unwrap_or((0, None));
let end_incl = end_incl.unwrap_or(blob_len - 1).min(blob_len - 1);
let this_len = end_incl - start + 1;
// Stream the payload in 256 KiB frames, generated on the fly.
let body_stream = futures::stream::unfold(0u64, move |sent| async move {
if sent >= this_len {
return None;
}
let take = ((this_len - sent) as usize).min(256 * 1024);
let mut frame = vec![0u8; take];
fill_at(&mut frame, start + sent);
Some((
Ok::<Bytes, std::io::Error>(Bytes::from(frame)),
sent + take as u64,
))
});
Response::builder()
.status(StatusCode::PARTIAL_CONTENT)
.header("content-type", "application/octet-stream")
.header("content-length", this_len.to_string())
.header(
"content-range",
format!("bytes {start}-{end_incl}/{blob_len}"),
)
.header("etag", "\"0x1\"")
.header("last-modified", "Thu, 01 Jan 2026 00:00:00 GMT")
.header("x-ms-blob-type", "BlockBlob")
.header("x-ms-lease-status", "unlocked")
.header("x-ms-lease-state", "available")
.header("x-ms-request-id", "11111111-1111-1111-1111-111111111111")
.header("x-ms-version", "2020-04-08")
.header("x-ms-creation-time", "Thu, 01 Jan 2026 00:00:00 GMT")
.header("x-ms-server-encrypted", "true")
.header("date", "Thu, 01 Jan 2026 00:00:00 GMT")
.body(Body::from_stream(body_stream))
.unwrap()
});
tokio::spawn(async move {
axum::serve(listener, app).await.expect("stub serve");
});
format!("http://{addr}/devaccount")
}
// ─── BEFORE: verbatim old collect-everything implementations ────────────────
mod before {
use super::*;
use azure_storage_blobs::prelude::BlobClient;
use oxicloud::application::ports::blob_storage_ports::BlobStream;
/// Old `get_blob_stream` body (drain everything, yield one chunk).
pub async fn get_blob_stream(client: &BlobClient) -> Result<BlobStream, String> {
let mut result_data: Vec<u8> = Vec::new();
let mut stream = client.get().into_stream();
while let Some(response) = stream.next().await {
let response = response.map_err(|e| format!("Failed to get blob: {e}"))?;
let mut body = response.data;
while let Some(chunk) = body.next().await {
let chunk = chunk.map_err(|e| format!("Stream read error: {e}"))?;
result_data.extend_from_slice(&chunk);
}
}
let stream: BlobStream = Box::pin(futures::stream::once(async move {
Ok(Bytes::from(result_data))
}));
Ok(stream)
}
/// Old `get_blob_range_stream` body.
pub async fn get_blob_range_stream(
client: &BlobClient,
start: u64,
end: Option<u64>,
) -> Result<BlobStream, String> {
let range = match end {
Some(e) => azure_core::request_options::Range::new(start, e),
None => azure_core::request_options::Range::new(start, u64::MAX),
};
let mut result_data: Vec<u8> = Vec::new();
let mut stream = client.get().range(range).into_stream();
while let Some(response) = stream.next().await {
let response = response.map_err(|e| format!("Failed to get blob range: {e}"))?;
let mut body = response.data;
while let Some(chunk) = body.next().await {
let chunk = chunk.map_err(|e| format!("Stream range read error: {e}"))?;
result_data.extend_from_slice(&chunk);
}
}
let stream: BlobStream = Box::pin(futures::stream::once(async move {
Ok(Bytes::from(result_data))
}));
Ok(stream)
}
}
// ─── Drain helper: TTFB + wall + hash ───────────────────────────────────────
async fn drain(
stream: oxicloud::application::ports::blob_storage_ports::BlobStream,
t0: Instant,
) -> (f64, f64, blake3::Hash, u64) {
let mut stream = stream;
let mut hasher = blake3::Hasher::new();
let mut ttfb = None;
let mut total = 0u64;
while let Some(chunk) = stream.next().await {
let chunk = chunk.expect("stream chunk");
if ttfb.is_none() {
ttfb = Some(t0.elapsed().as_secs_f64() * 1e3);
}
total += chunk.len() as u64;
hasher.update(&chunk);
}
(
ttfb.unwrap_or(f64::NAN),
t0.elapsed().as_secs_f64() * 1e3,
hasher.finalize(),
total,
)
}
fn reset_peak() {
PEAK.store(LIVE.load(Ordering::Relaxed), Ordering::Relaxed);
}
fn peak_mib() -> f64 {
PEAK.load(Ordering::Relaxed) as f64 / (1024.0 * 1024.0)
}
#[tokio::main(flavor = "multi_thread")]
async fn main() {
let mb: u64 = env::var("BENCH_MB")
.ok()
.and_then(|v| v.parse().ok())
.unwrap_or(256);
let tail_mb: u64 = env::var("BENCH_TAIL_MB")
.ok()
.and_then(|v| v.parse().ok())
.unwrap_or(128);
let blob_len = mb * 1024 * 1024;
let hash = "aabbccdd00112233445566778899eeff00112233445566778899aabbccddeeff";
let endpoint = stub_azure(blob_len).await;
println!("bench_azure_stream — {mb} MiB blob via local stub at {endpoint}\n");
// AFTER: the real backend pointed at the stub via endpoint_url.
let backend = AzureBlobBackend::new(&AzureStorageConfig {
account_name: "devaccount".to_string(),
account_key: base64::Engine::encode(
&base64::engine::general_purpose::STANDARD,
b"benchkeybenchkeybenchkey",
),
container: "blobs".to_string(),
sas_token: None,
endpoint_url: Some(endpoint.clone()),
});
// BEFORE: a raw SDK client at the same endpoint for the verbatim old code.
let creds = azure_storage::StorageCredentials::access_key(
"devaccount",
base64::Engine::encode(
&base64::engine::general_purpose::STANDARD,
b"benchkeybenchkeybenchkey",
),
);
let old_client = azure_storage_blobs::prelude::ClientBuilder::with_location(
azure_storage::CloudLocation::Custom {
account: "devaccount".to_string(),
uri: endpoint.clone(),
},
creds,
)
.container_client("blobs")
.blob_client(format!("{}/{}.blob", &hash[0..2], hash));
let expect_full = expected_hash(0, blob_len);
let tail_start = blob_len - tail_mb * 1024 * 1024;
let expect_tail = expected_hash(tail_start, blob_len - tail_start);
// ── [1] Full-blob download ──────────────────────────────────────────────
reset_peak();
let t0 = Instant::now();
let s = before::get_blob_stream(&old_client)
.await
.expect("before stream");
let (ttfb_b, wall_b, hash_b, len_b) = drain(s, t0).await;
let peak_b = peak_mib();
reset_peak();
let t0 = Instant::now();
let s = backend.get_blob_stream(hash).await.expect("after stream");
let (ttfb_a, wall_a, hash_a, len_a) = drain(s, t0).await;
let peak_a = peak_mib();
println!("[1] full {mb} MiB download TTFB ms wall ms peak live heap MiB");
println!(" BEFORE (collect-then-yield) {ttfb_b:9.1} {wall_b:9.1} {peak_b:10.1}");
println!(
" AFTER (streamed) {ttfb_a:9.1} {wall_a:9.1} {peak_a:10.1} TTFB {:.0}x, heap {:.0}x lower",
ttfb_b / ttfb_a,
peak_b / peak_a
);
// ── [2] Open-ended range (seek to last {tail_mb} MiB) ───────────────────
reset_peak();
let t0 = Instant::now();
let s = before::get_blob_range_stream(&old_client, tail_start, None)
.await
.expect("before range");
let (rttfb_b, rwall_b, rhash_b, rlen_b) = drain(s, t0).await;
let rpeak_b = peak_mib();
reset_peak();
let t0 = Instant::now();
let s = backend
.get_blob_range_stream(hash, tail_start, None)
.await
.expect("after range");
let (rttfb_a, rwall_a, rhash_a, rlen_a) = drain(s, t0).await;
let rpeak_a = peak_mib();
println!("[2] range bytes={tail_start}- ({tail_mb} MiB tail)");
println!(" BEFORE (collect-then-yield) {rttfb_b:9.1} {rwall_b:9.1} {rpeak_b:10.1}");
println!(
" AFTER (streamed) {rttfb_a:9.1} {rwall_a:9.1} {rpeak_a:10.1} TTFB {:.0}x, heap {:.0}x lower",
rttfb_b / rttfb_a,
rpeak_b / rpeak_a
);
// ── Equivalence gates ───────────────────────────────────────────────────
let mut ok = true;
if hash_b != expect_full || hash_a != expect_full || len_b != blob_len || len_a != blob_len {
eprintln!("GATE FAIL full blob: hashes/length differ");
ok = false;
}
if rhash_b != expect_tail || rhash_a != expect_tail || rlen_b != rlen_a {
eprintln!("GATE FAIL range: hashes/length differ");
ok = false;
}
println!(
"\n[gate] BLAKE3(BEFORE) == BLAKE3(AFTER) == source: {}",
if ok { "OK" } else { "FAILED" }
);
if !ok {
std::process::exit(1);
}
}
+572
View File
@@ -0,0 +1,572 @@
//! CalDAV parse-path benchmark — the write-side 8×-reparse and the
//! read-side per-event copies (ROUND4).
//!
//! What changed:
//!
//! • `CalendarEvent::from_ical` funnelled each of its 8 property
//! lookups through an extractor that re-ran the full `IcalParser`
//! (line unfolding + component tree) over the whole body — 8
//! complete parses per VEVENT on every CalDAV PUT, `8·(M+1)` on a
//! master+M-exceptions PUT, `8·N` on an N-event import. Now: one
//! parse, all lookups on the parsed component (value-only lookups
//! also skip the parameter-map build).
//! • `split_vevents` uppercased EVERY line into a fresh String.
//! Now: allocation-free case-insensitive prefix tests.
//! • `extract_vevent_chunk` (read side: every REPORT/GET, per event)
//! allocated a full uppercase copy of the stored body just to find
//! two tags. Now: memchr fast path + alloc-free CI scan fallback.
//! • `group_events_by_uid` (read side, per REPORT) cloned every
//! event's UID String. Now: borrowed keys.
//!
//! The OLD logic is copied verbatim into `mod before`; equivalence
//! gates assert byte-identical parsed fields / chunk slices / grouping
//! across a corpus incl. folded lines, params, VALARM, all-day,
//! exceptions and mixed-case tags (exit 1 on any diff).
//!
//! Run (no Postgres needed):
//! cargo run --release --features bench --example bench_caldav_parse
//! Tunables (env):
//! BENCH_EVENTS (200) BENCH_PASSES (30) BENCH_GROUP_N (5000)
use std::env;
use std::hint::black_box;
use std::time::Instant;
use chrono::{DateTime, TimeZone, Utc};
use oxicloud::application::adapters::caldav_adapter::bench as caldav_bench;
use oxicloud::application::dtos::calendar_dto::CalendarEventDto;
use oxicloud::domain::entities::calendar_event::CalendarEvent;
use uuid::Uuid;
// ─── BEFORE: verbatim copies of the pre-optimization logic ──────────────────
#[allow(clippy::all)]
mod before {
use std::collections::HashMap;
/// Old `parse_first_vevent` — fresh parser per call.
pub fn parse_first_vevent(ical_data: &str) -> Option<ical::parser::ical::component::IcalEvent> {
use std::io::BufReader;
let reader = BufReader::new(ical_data.as_bytes());
let parser = ical::IcalParser::new(reader);
for cal in parser {
let Ok(cal) = cal else { continue };
if let Some(event) = cal.events.into_iter().next() {
return Some(event);
}
}
None
}
/// Old params-aware extractor — one FULL parse per property lookup.
pub fn extract_ical_property_with_params(
ical_data: &str,
property_name: &str,
) -> Option<(String, HashMap<String, Vec<String>>)> {
let event = parse_first_vevent(ical_data)?;
let prop = event
.properties
.into_iter()
.find(|p| p.name.eq_ignore_ascii_case(property_name))?;
let value = prop.value?;
if value.trim().is_empty() {
return None;
}
let mut params: HashMap<String, Vec<String>> = HashMap::new();
if let Some(param_list) = prop.params {
for (name, values) in param_list {
params.insert(name.to_ascii_uppercase(), values);
}
}
Some((value.trim().to_string(), params))
}
pub fn extract_ical_property(ical_data: &str, property_name: &str) -> Option<String> {
extract_ical_property_with_params(ical_data, property_name).map(|(v, _p)| v)
}
/// Comparable subset of the entity fields `from_ical` derives.
#[derive(Debug, PartialEq)]
pub struct BeforeEvent {
pub summary: String,
pub description: Option<String>,
pub location: Option<String>,
pub start_time: chrono::DateTime<chrono::Utc>,
pub end_time: chrono::DateTime<chrono::Utc>,
pub all_day: bool,
pub rrule: Option<String>,
pub ical_uid: Option<String>,
pub recurrence_id: Option<chrono::DateTime<chrono::Utc>>,
}
/// Old `from_ical` body (8 extractor calls = 8 full parses), minus
/// the entity envelope (ids/timestamps — identical on both sides).
pub fn from_ical(ical_data: &str) -> Result<BeforeEvent, String> {
let summary = extract_ical_property(ical_data, "SUMMARY").ok_or("Missing SUMMARY")?;
let (dtstart_value, dtstart_params) =
extract_ical_property_with_params(ical_data, "DTSTART").ok_or("Missing DTSTART")?;
let (dtend_value, _dtend_params) =
extract_ical_property_with_params(ical_data, "DTEND").ok_or("Missing DTEND")?;
let all_day = dtstart_params
.get("VALUE")
.map(|vs| vs.iter().any(|v| v.eq_ignore_ascii_case("DATE")))
.unwrap_or(false);
let start_time = parse_ical_datetime(&dtstart_value, all_day)?;
let end_time = parse_ical_datetime(&dtend_value, all_day)?;
let description = extract_ical_property(ical_data, "DESCRIPTION");
let location = extract_ical_property(ical_data, "LOCATION");
let rrule = extract_ical_property(ical_data, "RRULE");
let ical_uid = extract_ical_property(ical_data, "UID");
let recurrence_id = match extract_ical_property_with_params(ical_data, "RECURRENCE-ID") {
Some((value, params)) => {
let is_date = params
.get("VALUE")
.map(|vs| vs.iter().any(|v| v.eq_ignore_ascii_case("DATE")))
.unwrap_or(false);
parse_ical_datetime(&value, is_date).ok()
}
None => None,
};
Ok(BeforeEvent {
summary,
description,
location,
start_time,
end_time,
all_day,
rrule,
ical_uid,
recurrence_id,
})
}
/// Old datetime parser (verbatim semantics for the two supported forms).
pub fn parse_ical_datetime(
value: &str,
is_date_only: bool,
) -> Result<chrono::DateTime<chrono::Utc>, String> {
use chrono::TimeZone;
if is_date_only {
if value.len() != 8 {
return Err("bad all-day".into());
}
let year: i32 = value[0..4].parse().map_err(|_| "year")?;
let month: u32 = value[4..6].parse().map_err(|_| "month")?;
let day: u32 = value[6..8].parse().map_err(|_| "day")?;
return chrono::NaiveDate::from_ymd_opt(year, month, day)
.map(|d| chrono::Utc.from_utc_datetime(&d.and_hms_opt(0, 0, 0).unwrap()))
.ok_or_else(|| "date".into());
}
if value.len() < 15 || !value.ends_with('Z') {
return Err(format!("bad datetime {value:?}"));
}
let year: i32 = value[0..4].parse().map_err(|_| "year")?;
let month: u32 = value[4..6].parse().map_err(|_| "month")?;
let day: u32 = value[6..8].parse().map_err(|_| "day")?;
let hour: u32 = value[9..11].parse().map_err(|_| "hour")?;
let minute: u32 = value[11..13].parse().map_err(|_| "minute")?;
let second: u32 = value[13..15].parse().map_err(|_| "second")?;
match chrono::NaiveDate::from_ymd_opt(year, month, day) {
Some(date) => match date.and_hms_opt(hour, minute, second) {
Some(datetime) => Ok(chrono::Utc.from_utc_datetime(&datetime)),
None => Err("time".into()),
},
None => Err("date".into()),
}
}
/// Old `split_vevents` — per-line uppercase String.
pub fn split_vevents(ical_data: &str) -> Vec<String> {
let mut blocks = Vec::new();
let mut in_event = false;
let mut current = String::new();
for raw_line in ical_data.split('\n') {
let line = raw_line.trim_end_matches('\r');
let upper = line.trim_start().to_ascii_uppercase();
if upper.starts_with("BEGIN:VEVENT") {
in_event = true;
current.clear();
}
if in_event {
current.push_str(line);
current.push_str("\r\n");
}
if in_event && upper.starts_with("END:VEVENT") {
blocks.push(std::mem::take(&mut current));
in_event = false;
}
}
blocks
}
/// Old `extract_vevent_chunk` — full uppercase copy of the body.
pub fn extract_vevent_chunk(ical_data: &str) -> Option<&str> {
let upper = ical_data.to_ascii_uppercase();
let begin = upper.find("BEGIN:VEVENT")?;
let after_begin = &upper[begin..];
let rel_end = after_begin.find("END:VEVENT")?;
let end_tag_end = begin + rel_end + "END:VEVENT".len();
let mut end = end_tag_end;
if ical_data[end..].starts_with('\r') {
end += 1;
}
if ical_data[end..].starts_with('\n') {
end += 1;
}
Some(&ical_data[begin..end])
}
/// Old `group_events_by_uid` — String-keyed map, UID cloned per event.
pub fn group_events_by_uid<'a>(
events: &'a [oxicloud::application::dtos::calendar_dto::CalendarEventDto],
) -> Vec<Vec<&'a oxicloud::application::dtos::calendar_dto::CalendarEventDto>> {
let mut order: Vec<String> = Vec::new();
let mut buckets: HashMap<
String,
Vec<&'a oxicloud::application::dtos::calendar_dto::CalendarEventDto>,
> = HashMap::new();
for event in events {
let key = event.ical_uid.clone();
if !buckets.contains_key(&key) {
order.push(key.clone());
}
buckets.entry(key).or_default().push(event);
}
let mut out = Vec::with_capacity(order.len());
for uid in order {
let mut bucket = buckets.remove(&uid).unwrap_or_default();
bucket.sort_by_key(|e| e.recurrence_id.is_some());
out.push(bucket);
}
out
}
}
// ─── Corpus ─────────────────────────────────────────────────────────────────
/// A realistic ~1.3 KiB VEVENT: params on DTSTART, folded DESCRIPTION,
/// three ATTENDEEs with CN/PARTSTAT, ORGANIZER, VALARM, CATEGORIES,
/// STATUS and X-props. `variant` 0 = timed master with RRULE, 1 = all-day,
/// 2 = exception override (RECURRENCE-ID).
fn build_vevent_body(i: usize, variant: usize) -> String {
let uid = format!("evt-{i:05}@oxicloud.bench");
let mut v = String::with_capacity(1400);
v.push_str("BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//OxiCloud//NONSGML Calendar//EN\r\n");
v.push_str("BEGIN:VEVENT\r\n");
v.push_str(&format!("UID:{uid}\r\n"));
v.push_str("DTSTAMP:20260701T120000Z\r\n");
match variant {
1 => {
v.push_str("DTSTART;VALUE=DATE:20260810\r\n");
v.push_str("DTEND;VALUE=DATE:20260811\r\n");
}
2 => {
v.push_str("DTSTART:20260812T090000Z\r\n");
v.push_str("DTEND:20260812T100000Z\r\n");
v.push_str("RECURRENCE-ID:20260812T090000Z\r\n");
}
_ => {
v.push_str("DTSTART:20260805T090000Z\r\n");
v.push_str("DTEND:20260805T103000Z\r\n");
v.push_str("RRULE:FREQ=WEEKLY;BYDAY=TU,TH;UNTIL=20261231T000000Z\r\n");
}
}
v.push_str(&format!(
"SUMMARY:Sprint review #{i} — métricas y datos\r\n"
));
v.push_str(
"DESCRIPTION:Repaso de los objetivos del sprint con el equipo completo\\, in\r\n cluyendo demo de la nueva vista de fotos y el plan de la ronda de rendimien\r\n to número cuatro.\r\n",
);
v.push_str("LOCATION:Sala Turing — 3ª planta\r\n");
v.push_str("ORGANIZER;CN=Ana García:mailto:ana@example.com\r\n");
v.push_str(
"ATTENDEE;CN=Luis Pérez;PARTSTAT=ACCEPTED;ROLE=REQ-PARTICIPANT:mailto:luis@example.com\r\n",
);
v.push_str("ATTENDEE;CN=Sam Chen;PARTSTAT=NEEDS-ACTION;RSVP=TRUE:mailto:sam@example.com\r\n");
v.push_str("ATTENDEE;CN=Río Núñez;PARTSTAT=TENTATIVE:mailto:rio@example.com\r\n");
v.push_str("CATEGORIES:TRABAJO,EQUIPO\r\n");
v.push_str("STATUS:CONFIRMED\r\n");
v.push_str("SEQUENCE:2\r\n");
v.push_str("TRANSP:OPAQUE\r\n");
v.push_str("X-OXICLOUD-ROUND:4\r\n");
v.push_str("BEGIN:VALARM\r\nACTION:DISPLAY\r\nDESCRIPTION:Reminder\r\nTRIGGER:-PT15M\r\nEND:VALARM\r\n");
v.push_str("END:VEVENT\r\n");
v.push_str("END:VCALENDAR\r\n");
v
}
/// N-event import body (master + exception pairs inside one VCALENDAR).
fn build_import_body(n_events: usize) -> String {
let mut v = String::with_capacity(n_events * 1400);
v.push_str("BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Foreign//Client//EN\r\n");
for i in 0..n_events {
let single = build_vevent_body(i, i % 3);
// Extract just the VEVENT block from the standalone body.
let begin = single.find("BEGIN:VEVENT").unwrap();
let end = single.find("END:VEVENT").unwrap() + "END:VEVENT\r\n".len();
v.push_str(&single[begin..end]);
}
v.push_str("END:VCALENDAR\r\n");
v
}
fn make_dto(i: usize, uid: &str, recurrence: Option<DateTime<Utc>>) -> CalendarEventDto {
CalendarEventDto {
id: Uuid::from_u128(i as u128).to_string(),
calendar_id: Uuid::nil().to_string(),
summary: format!("Evento {i}"),
description: None,
location: None,
start_time: Utc.with_ymd_and_hms(2026, 8, 5, 9, 0, 0).unwrap(),
end_time: Utc.with_ymd_and_hms(2026, 8, 5, 10, 0, 0).unwrap(),
all_day: false,
rrule: None,
ical_uid: uid.to_string(),
recurrence_id: recurrence,
ical_data: build_vevent_body(i, if recurrence.is_some() { 2 } else { 0 }),
created_at: Utc.with_ymd_and_hms(2026, 7, 1, 0, 0, 0).unwrap(),
updated_at: Utc.with_ymd_and_hms(2026, 7, 1, 0, 0, 0).unwrap(),
}
}
fn p50(mut xs: Vec<f64>) -> f64 {
xs.sort_by(|a, b| a.partial_cmp(b).unwrap());
xs[xs.len() / 2]
}
fn time_passes<T>(passes: usize, mut f: impl FnMut() -> T) -> f64 {
let mut per_pass = Vec::with_capacity(passes);
for _ in 0..passes {
let t0 = Instant::now();
black_box(f());
per_pass.push(t0.elapsed().as_secs_f64() * 1e6);
}
p50(per_pass)
}
fn main() {
let n_events: usize = env::var("BENCH_EVENTS")
.ok()
.and_then(|v| v.parse().ok())
.unwrap_or(200);
let passes: usize = env::var("BENCH_PASSES")
.ok()
.and_then(|v| v.parse().ok())
.unwrap_or(30);
let group_n: usize = env::var("BENCH_GROUP_N")
.ok()
.and_then(|v| v.parse().ok())
.unwrap_or(5000);
let calendar_id = Uuid::nil();
let bodies: Vec<String> = (0..n_events).map(|i| build_vevent_body(i, i % 3)).collect();
let import_body = build_import_body(50);
println!("bench_caldav_parse — {n_events} bodies, {passes} passes\n");
// ── [1] from_ical: single-event PUT path ────────────────────────────────
let t_before = time_passes(passes, || {
for b in &bodies {
black_box(before::from_ical(b).expect("before parse"));
}
}) / n_events as f64;
let t_after = time_passes(passes, || {
for b in &bodies {
black_box(CalendarEvent::from_ical(calendar_id, b.clone()).expect("after parse"));
}
}) / n_events as f64;
// The AFTER side clones the body (the real API takes it by value) —
// measure that clone alone so the comparison can subtract it.
let t_clone = time_passes(passes, || {
for b in &bodies {
black_box(b.clone());
}
}) / n_events as f64;
println!("[1] from_ical µs/event (8-parse chain vs single parse)");
println!(" BEFORE {t_before:8.2}");
println!(
" AFTER {t_after:8.2} (incl. {t_clone:.2} body clone) {:.1}x",
t_before / (t_after - t_clone)
);
// ── [2] parse_all_events: 50-event import PUT ───────────────────────────
let t_before_imp = time_passes(passes, || {
let blocks = before::split_vevents(&import_body);
let mut out = Vec::with_capacity(blocks.len());
for block in blocks {
let wrapped = format!(
"BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//OxiCloud//NONSGML Calendar//EN\r\n{}END:VCALENDAR\r\n",
block,
);
out.push(before::from_ical(&wrapped).expect("before import"));
}
out
});
let t_after_imp = time_passes(passes, || {
CalendarEvent::parse_all_events(calendar_id, &import_body).expect("after import")
});
println!("[2] parse_all_events µs/50-event import body");
println!(" BEFORE {t_before_imp:8.1}");
println!(
" AFTER {t_after_imp:8.1} {:.1}x",
t_before_imp / t_after_imp
);
// ── [3] extract_vevent_chunk: REPORT/GET read path ──────────────────────
let t_chunk_before = time_passes(passes, || {
for b in &bodies {
black_box(before::extract_vevent_chunk(b));
}
}) / n_events as f64
* 1000.0;
let t_chunk_after = time_passes(passes, || {
for b in &bodies {
black_box(caldav_bench::extract_vevent_chunk(b));
}
}) / n_events as f64
* 1000.0;
println!("[3] extract_vevent_chunk ns/event (uppercase copy vs direct scan)");
println!(" BEFORE {t_chunk_before:8.0}");
println!(
" AFTER {t_chunk_after:8.0} {:.1}x",
t_chunk_before / t_chunk_after
);
// ── [4] group_events_by_uid: REPORT fold ────────────────────────────────
// 80% masters, 20% exception overrides sharing a master's UID.
let dtos: Vec<CalendarEventDto> = (0..group_n)
.map(|i| {
if i % 5 == 4 {
let master = i - 1;
make_dto(
i,
&format!("evt-{master:05}@oxicloud.bench"),
Some(Utc.with_ymd_and_hms(2026, 8, 12, 9, 0, 0).unwrap()),
)
} else {
make_dto(i, &format!("evt-{i:05}@oxicloud.bench"), None)
}
})
.collect();
let t_grp_before = time_passes(passes, || black_box(before::group_events_by_uid(&dtos)));
let t_grp_after = time_passes(passes, || {
black_box(caldav_bench::group_events_by_uid(&dtos))
});
println!("[4] group_events_by_uid µs/{group_n} events (String keys vs borrowed)");
println!(" BEFORE {t_grp_before:8.1}");
println!(
" AFTER {t_grp_after:8.1} {:.1}x",
t_grp_before / t_grp_after
);
// ── [5] Equivalence gates ───────────────────────────────────────────────
let mut ok = true;
// Gate A: from_ical field identity across the corpus + edge bodies.
let mut gate_bodies: Vec<String> = bodies.clone();
gate_bodies.push(build_vevent_body(9990, 1));
gate_bodies.push(build_vevent_body(9991, 2));
// Mixed-case tags + LF-only line endings (foreign client shapes).
gate_bodies.push(
"begin:vcalendar\nversion:2.0\nbegin:vevent\nuid:mixed-case@x\nsummary:Mixed Case\ndtstart:20260801T080000Z\ndtend:20260801T090000Z\nend:vevent\nend:vcalendar\n"
.to_string(),
);
for b in &gate_bodies {
let bf = before::from_ical(b);
let af = CalendarEvent::from_ical(calendar_id, b.clone());
match (bf, af) {
(Ok(bf), Ok(af)) => {
let same = bf.summary == af.summary()
&& bf.description.as_deref() == af.description()
&& bf.location.as_deref() == af.location()
&& bf.start_time == *af.start_time()
&& bf.end_time == *af.end_time()
&& bf.all_day == af.all_day()
&& bf.rrule.as_deref() == af.rrule()
&& bf.ical_uid.as_deref() == Some(af.ical_uid())
&& bf.recurrence_id.as_ref() == af.recurrence_id();
if !same {
eprintln!("GATE A FAIL: field mismatch for body:\n{b}\n before={bf:?}");
ok = false;
}
}
(Err(_), Err(_)) => {}
(bf, af) => {
eprintln!(
"GATE A FAIL: error parity broke (before_ok={} after_ok={}) for body:\n{b}",
bf.is_ok(),
af.is_ok()
);
ok = false;
}
}
}
// Gate B: parse_all_events equivalence on the import body — same
// events, same wrapped per-row ical_data.
let after_events =
CalendarEvent::parse_all_events(calendar_id, &import_body).expect("import parses");
let before_blocks = before::split_vevents(&import_body);
if after_events.len() != before_blocks.len() {
eprintln!(
"GATE B FAIL: event count {} != block count {}",
after_events.len(),
before_blocks.len()
);
ok = false;
}
for (evt, block) in after_events.iter().zip(&before_blocks) {
let wrapped = format!(
"BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//OxiCloud//NONSGML Calendar//EN\r\n{}END:VCALENDAR\r\n",
block,
);
if evt.ical_data() != wrapped {
eprintln!("GATE B FAIL: wrapped ical_data mismatch");
ok = false;
break;
}
let bf = before::from_ical(&wrapped).expect("before parses wrapped");
if bf.summary != evt.summary() || bf.recurrence_id.as_ref() != evt.recurrence_id() {
eprintln!("GATE B FAIL: field mismatch on wrapped block");
ok = false;
break;
}
}
// Gate C: chunk slices byte-identical (incl. mixed-case + no-terminator).
let mut chunk_bodies = bodies.clone();
chunk_bodies.push("BEGIN:VCALENDAR\r\nbegin:vevent\r\nUID:x@y\r\nend:vevent".to_string());
chunk_bodies.push("no vevent here at all".to_string());
for b in &chunk_bodies {
if before::extract_vevent_chunk(b) != caldav_bench::extract_vevent_chunk(b) {
eprintln!("GATE C FAIL: chunk mismatch for body:\n{b}");
ok = false;
}
}
// Gate D: grouping identity — same UID order, same per-bucket rows.
let g_before = before::group_events_by_uid(&dtos);
let g_after = caldav_bench::group_events_by_uid(&dtos);
let shape = |g: &Vec<Vec<&CalendarEventDto>>| -> Vec<Vec<(String, bool)>> {
g.iter()
.map(|bucket| {
bucket
.iter()
.map(|e| (e.id.clone(), e.recurrence_id.is_some()))
.collect()
})
.collect()
};
if shape(&g_before) != shape(&g_after) {
eprintln!("GATE D FAIL: grouping mismatch");
ok = false;
}
println!(
"[5] Equivalence gates: {}",
if ok { "OK (byte-identical)" } else { "FAILED" }
);
if !ok {
std::process::exit(1);
}
}
+333
View File
@@ -0,0 +1,333 @@
//! WebDAV drive-selector resolution benchmark — grants join/request vs moka.
//!
//! Every native `/webdav/<selector>/…` request (all verbs; MOVE and COPY
//! twice) resolved its scope through `lookup_drive_selector` →
//! `DriveRepository::list_readable_by`: a role_grants ⋈ drives ⋈ folders
//! join with inline transitive-group expansion, GROUP BY + MIN(role) +
//! ORDER BY — per request, uncached. The same join also ran per request
//! in search, trash listing and the `GET /api/drives` picker.
//!
//! AFTER wires the per-user `readable_cache` (30 s TTL, single-flight,
//! explicit invalidation on every membership/lifecycle mutation) into
//! `DrivePgRepository` — this bench drives the REAL repository (cache,
//! `try_get_with` and the per-hit `Vec` clone included), not a synthetic
//! lookup, against the verbatim BEFORE query.
//!
//! Run (needs Postgres up; reads DATABASE_URL from .env):
//! cargo run --release --features bench --example bench_drive_selector
//! Tunables (env): BENCH_POOL (20), BENCH_SECONDS (4), BENCH_CONCURRENCIES ("8,64").
use std::env;
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::{Duration, Instant};
use oxicloud::domain::repositories::drive_repository::DriveRepository;
use oxicloud::infrastructure::repositories::pg::DrivePgRepository;
use sqlx::postgres::PgPoolOptions;
use sqlx::{PgPool, Row};
use uuid::Uuid;
fn env_or<T: std::str::FromStr>(key: &str, default: T) -> T {
env::var(key)
.ok()
.and_then(|v| v.parse().ok())
.unwrap_or(default)
}
struct Seeded {
user_id: Uuid,
}
/// user → personal drive (default) + two shared drives, each with a
/// role_grant for the user — the shape a typical DAV-syncing member of a
/// small team resolves on every request.
async fn seed(pool: &PgPool) -> Seeded {
let mut tx = pool.begin().await.expect("begin");
let user_id: Uuid = sqlx::query_scalar(
"INSERT INTO auth.users (username, email, role)
VALUES ('bench_drivesel', 'bench_drivesel@bench.invalid', 'user')
RETURNING id",
)
.fetch_one(&mut *tx)
.await
.expect("seed user");
// (name, kind, default_for_user, role)
let drives: [(&str, &str, Option<Uuid>, &str); 3] = [
("Personal", "personal", Some(user_id), "owner"),
("Equipo Diseño", "shared", None, "editor"),
("Archivo 2026", "shared", None, "viewer"),
];
for (name, kind, default_for, role) in drives {
let drive_id: Uuid = sqlx::query_scalar(
"INSERT INTO storage.drives (kind, default_for_user) VALUES ($1, $2) RETURNING id",
)
.bind(kind)
.bind(default_for)
.fetch_one(&mut *tx)
.await
.expect("seed drive");
let folder_id: Uuid = sqlx::query_scalar(
"INSERT INTO storage.folders (name, path, lpath, drive_id)
VALUES ($1, '/' || $1, 'x', $2) RETURNING id",
)
.bind(name)
.bind(drive_id)
.fetch_one(&mut *tx)
.await
.expect("seed folder");
sqlx::query("UPDATE storage.drives SET root_folder_id = $1 WHERE id = $2")
.bind(folder_id)
.bind(drive_id)
.execute(&mut *tx)
.await
.expect("stamp root");
sqlx::query(
"INSERT INTO storage.role_grants
(subject_type, subject_id, resource_type, resource_id, role, granted_by)
VALUES ('user', $1, 'drive', $2, $3::storage.grant_role, $1)",
)
.bind(user_id)
.bind(drive_id)
.bind(role)
.execute(&mut *tx)
.await
.expect("seed grant");
}
tx.commit().await.expect("commit");
Seeded { user_id }
}
async fn cleanup(pool: &PgPool, user_id: Uuid) {
// Drives/folders/grants cascade off the user via the grant cleanup
// trigger + explicit deletes (drives carry no owner FK).
let ids: Vec<Uuid> = sqlx::query_scalar(
"SELECT resource_id FROM storage.role_grants
WHERE subject_type = 'user' AND subject_id = $1 AND resource_type = 'drive'",
)
.bind(user_id)
.fetch_all(pool)
.await
.unwrap_or_default();
for id in ids {
let _ = sqlx::query(
"DELETE FROM storage.role_grants WHERE resource_type='drive' AND resource_id=$1",
)
.bind(id)
.execute(pool)
.await;
let root: Option<Uuid> =
sqlx::query_scalar("SELECT root_folder_id FROM storage.drives WHERE id = $1")
.bind(id)
.fetch_optional(pool)
.await
.ok()
.flatten();
let _ = sqlx::query("DELETE FROM storage.drives WHERE id = $1")
.bind(id)
.execute(pool)
.await;
if let Some(root) = root {
let _ = sqlx::query("DELETE FROM storage.folders WHERE id = $1")
.bind(root)
.execute(pool)
.await;
}
}
let _ = sqlx::query("DELETE FROM auth.users WHERE id = $1")
.bind(user_id)
.execute(pool)
.await;
}
/// The exact production BEFORE — `list_readable_by`'s query, verbatim.
async fn one_op_before(pool: &PgPool, user_id: Uuid, queries: &AtomicUsize) -> Vec<(Uuid, String)> {
let rows = sqlx::query(
r#"
SELECT d.id, d.kind, d.default_for_user, d.root_folder_id,
d.quota_bytes, d.used_bytes, d.policies,
d.created_at, d.updated_at,
f.name AS root_folder_name,
MIN(g.role)::text AS caller_role
FROM storage.drives d
JOIN storage.folders f ON f.id = d.root_folder_id
JOIN storage.role_grants g
ON g.resource_type = 'drive'
AND g.resource_id = d.id
WHERE (
(g.subject_type = 'user' AND g.subject_id = $1)
OR (g.subject_type = 'group' AND g.subject_id IN
(SELECT storage.caller_group_ids($1)))
)
AND (g.expires_at IS NULL OR g.expires_at > NOW())
GROUP BY d.id, d.kind, d.default_for_user, d.root_folder_id,
d.quota_bytes, d.used_bytes, d.policies,
d.created_at, d.updated_at, f.name
ORDER BY (d.default_for_user IS NULL) ASC,
LOWER(f.name) ASC
"#,
)
.bind(user_id)
.fetch_all(pool)
.await
.expect("grants join");
queries.fetch_add(1, Ordering::Relaxed);
rows.iter()
.map(|r| {
(
r.get::<Uuid, _>("id"),
r.get::<String, _>("root_folder_name"),
)
})
.collect()
}
struct Stats {
rps: f64,
p50: f64,
p95: f64,
p99: f64,
}
fn summarize(mut lats: Vec<f64>, secs: u64) -> Stats {
lats.sort_by(|a, b| a.partial_cmp(b).unwrap());
let n = lats.len();
let pct = |p: f64| {
if n == 0 {
0.0
} else {
lats[((n as f64 * p) as usize).min(n - 1)]
}
};
Stats {
rps: n as f64 / secs as f64,
p50: pct(0.50),
p95: pct(0.95),
p99: pct(0.99),
}
}
#[tokio::main(flavor = "multi_thread")]
async fn main() {
dotenvy::dotenv().ok();
let url = env::var("DATABASE_URL")
.or_else(|_| env::var("OXICLOUD_DB_CONNECTION_STRING"))
.expect("set DATABASE_URL — the dev Postgres URL");
let pool_size: u32 = env_or("BENCH_POOL", 20);
let secs: u64 = env_or("BENCH_SECONDS", 4);
let concurrencies: Vec<usize> = env::var("BENCH_CONCURRENCIES")
.ok()
.map(|s| s.split(',').filter_map(|x| x.trim().parse().ok()).collect())
.unwrap_or_else(|| vec![8, 64]);
let pool = Arc::new(
PgPoolOptions::new()
.max_connections(pool_size)
.min_connections(pool_size)
.acquire_timeout(Duration::from_secs(10))
.connect(&url)
.await
.expect("connect Postgres"),
);
let seeded = seed(&pool).await;
let user_id = seeded.user_id;
// AFTER = the real repository with its readable_cache.
let repo = Arc::new(DrivePgRepository::new(pool.clone()));
// ── Equivalence gate: BEFORE rows == repo output (cold), == warm hit ──
let gate_q = AtomicUsize::new(0);
let before_rows = one_op_before(&pool, user_id, &gate_q).await;
let cold: Vec<(Uuid, String)> = repo
.list_readable_by(user_id)
.await
.expect("repo list")
.into_iter()
.map(|d| (d.drive.id, d.root_folder_name))
.collect();
let warm: Vec<(Uuid, String)> = repo
.list_readable_by(user_id)
.await
.expect("repo list warm")
.into_iter()
.map(|d| (d.drive.id, d.root_folder_name))
.collect();
if before_rows != cold || cold != warm {
eprintln!(
"EQUIVALENCE GATE FAILED:\n before={before_rows:?}\n cold={cold:?}\n warm={warm:?}"
);
cleanup(&pool, user_id).await;
std::process::exit(1);
}
if before_rows.len() != 3 {
eprintln!("seed expected 3 readable drives, got {}", before_rows.len());
cleanup(&pool, user_id).await;
std::process::exit(1);
}
println!("\n#################################################################");
println!("# WebDAV drive-selector: BEFORE (grants join/req) vs AFTER (cache)");
println!("# pool={pool_size} window={secs}s/run drives/user=3");
println!("#################################################################\n");
println!(
"| {:>5} | {:<6} | {:>10} | {:>9} | {:>9} | {:>9} | {:>9} |",
"conc", "mode", "req/s", "p50 µs", "p95 µs", "p99 µs", "queries"
);
for &conc in &concurrencies {
for mode in ["BEFORE", "AFTER"] {
let queries = Arc::new(AtomicUsize::new(0));
let deadline = Instant::now() + Duration::from_secs(secs);
let mut handles = Vec::new();
for _ in 0..conc {
let pool = pool.clone();
let repo = repo.clone();
let queries = queries.clone();
let mode = mode.to_string();
handles.push(tokio::spawn(async move {
let mut lats = Vec::new();
while Instant::now() < deadline {
let t = Instant::now();
if mode == "BEFORE" {
std::hint::black_box(one_op_before(&pool, user_id, &queries).await);
} else {
let v = repo.list_readable_by(user_id).await.expect("repo list");
std::hint::black_box(v);
}
lats.push(t.elapsed().as_secs_f64() * 1_000_000.0);
if mode == "AFTER" {
// cache hit is sub-µs; yield so the loop doesn't
// monopolise workers and skew the run count.
tokio::task::yield_now().await;
}
}
lats
}));
}
let mut all = Vec::new();
for h in handles {
all.extend(h.await.unwrap());
}
let s = summarize(all, secs);
println!(
"| {:>5} | {:<6} | {:>10.0} | {:>9.2} | {:>9.2} | {:>9.2} | {:>9} |",
conc,
mode,
s.rps,
s.p50,
s.p95,
s.p99,
queries.load(Ordering::Relaxed)
);
}
}
cleanup(&pool, user_id).await;
println!("\n(BEFORE = the verbatim list_readable_by join per request; AFTER = the");
println!(" real DrivePgRepository serving from its per-user readable_cache —");
println!(" try_get_with single-flight + per-hit Vec clone included. Equivalence");
println!(" gate asserts identical (id, name) sequences: BEFORE == cold == warm.)");
}
+173
View File
@@ -0,0 +1,173 @@
//! Face-indexing fan-out benchmark — unbounded spawn vs semaphore (ROUND4).
//!
//! `FaceIndexingService::spawn_index` fired one `tokio::spawn` per
//! uploaded/copied image with NO ceiling; each task reads the full blob
//! into RAM and decodes it before inference. A bulk upload of N photos
//! therefore held up to N decoded images in flight simultaneously.
//! AFTER: an `Arc<Semaphore>` sized to the effective core count
//! (`OXICLOUD_FACES_INDEX_CONCURRENCY` override), permit acquired BEFORE
//! the blob read — the exact `ThumbnailService::decode_semaphore`
//! invariant ("peak memory = permits × image size").
//!
//! This is a *pattern* bench (like POOL-CONCURRENCY / RUNTIME): the real
//! service needs Postgres + an ONNX model, so the task body models the
//! dominant costs — full-file read + JPEG decode on the deterministic
//! `bench_support` photo corpus — while the spawn/permit shape is copied
//! from the service verbatim. Metrics: wall time, PEAK LIVE HEAP (exact,
//! via counting allocator), decode results asserted identical.
//!
//! Run (no Postgres needed):
//! cargo run --release --features bench --example bench_faces_bound
//! Tunables (env): BENCH_IMAGES (48), BENCH_PERMITS (effective cores).
use std::alloc::{GlobalAlloc, Layout, System};
use std::env;
use std::hint::black_box;
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
use std::time::Instant;
// ─── Peak-live-heap tracking allocator ──────────────────────────────────────
static LIVE: AtomicU64 = AtomicU64::new(0);
static PEAK: AtomicU64 = AtomicU64::new(0);
struct PeakAlloc;
fn bump(sz: u64) {
let live = LIVE.fetch_add(sz, Ordering::Relaxed) + sz;
PEAK.fetch_max(live, Ordering::Relaxed);
}
unsafe impl GlobalAlloc for PeakAlloc {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
bump(layout.size() as u64);
unsafe { System.alloc(layout) }
}
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
LIVE.fetch_sub(layout.size() as u64, Ordering::Relaxed);
unsafe { System.dealloc(ptr, layout) }
}
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
if new_size > layout.size() {
bump((new_size - layout.size()) as u64);
} else {
LIVE.fetch_sub((layout.size() - new_size) as u64, Ordering::Relaxed);
}
unsafe { System.realloc(ptr, layout, new_size) }
}
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
bump(layout.size() as u64);
unsafe { System.alloc_zeroed(layout) }
}
}
#[global_allocator]
static GLOBAL: PeakAlloc = PeakAlloc;
/// The modelled per-image work: full blob read (as `index_file` does via
/// `tokio::fs::read`) + JPEG decode (the analyzer's first step).
async fn index_one(path: std::path::PathBuf, dims: Arc<AtomicUsize>) {
let bytes = tokio::fs::read(&path).await.expect("read blob");
let img = tokio::task::spawn_blocking(move || image::load_from_memory(&bytes).expect("decode"))
.await
.expect("join decode");
dims.fetch_add((img.width() + img.height()) as usize, Ordering::Relaxed);
black_box(img);
}
fn effective_parallelism() -> usize {
std::thread::available_parallelism()
.map(|n| n.get())
.unwrap_or(2)
}
#[tokio::main(flavor = "multi_thread")]
async fn main() {
let images: usize = env::var("BENCH_IMAGES")
.ok()
.and_then(|v| v.parse().ok())
.unwrap_or(48);
let permits: usize = env::var("BENCH_PERMITS")
.ok()
.and_then(|v| v.parse().ok())
.unwrap_or_else(effective_parallelism);
// Deterministic photo corpus (12 MP JPEG case) → one temp file per
// "upload" so each task pays a real filesystem read.
let corpus = oxicloud::bench_support::load_or_generate();
let jpeg = corpus
.iter()
.max_by_key(|c| c.bytes.len())
.expect("corpus nonempty");
println!(
"bench_faces_bound — {images} images ({} · {:.1} MiB encoded), permits={permits}\n",
jpeg.name,
jpeg.bytes.len() as f64 / (1024.0 * 1024.0)
);
let dir = tempfile::tempdir().expect("tempdir");
let mut paths = Vec::with_capacity(images);
for i in 0..images {
let p = dir.path().join(format!("{i}.blob"));
std::fs::write(&p, &jpeg.bytes).expect("write blob");
paths.push(p);
}
// ── BEFORE: unbounded spawn per image (the old spawn_index shape) ──
let dims_before = Arc::new(AtomicUsize::new(0));
PEAK.store(LIVE.load(Ordering::Relaxed), Ordering::Relaxed);
let t0 = Instant::now();
let mut handles = Vec::with_capacity(images);
for p in &paths {
let p = p.clone();
let dims = dims_before.clone();
handles.push(tokio::spawn(async move {
index_one(p, dims).await;
}));
}
for h in handles {
h.await.unwrap();
}
let wall_before = t0.elapsed().as_secs_f64() * 1e3;
let peak_before = PEAK.load(Ordering::Relaxed) as f64 / (1024.0 * 1024.0);
// ── AFTER: same spawn shape + semaphore permit before the read ──
let dims_after = Arc::new(AtomicUsize::new(0));
let semaphore = Arc::new(tokio::sync::Semaphore::new(permits));
PEAK.store(LIVE.load(Ordering::Relaxed), Ordering::Relaxed);
let t0 = Instant::now();
let mut handles = Vec::with_capacity(images);
for p in &paths {
let p = p.clone();
let dims = dims_after.clone();
let semaphore = semaphore.clone();
handles.push(tokio::spawn(async move {
let _permit = semaphore
.acquire_owned()
.await
.expect("semaphore never closes");
index_one(p, dims).await;
}));
}
for h in handles {
h.await.unwrap();
}
let wall_after = t0.elapsed().as_secs_f64() * 1e3;
let peak_after = PEAK.load(Ordering::Relaxed) as f64 / (1024.0 * 1024.0);
println!(" wall ms peak live heap MiB");
println!("BEFORE (unbounded) {wall_before:8.1} {peak_before:10.1}");
println!(
"AFTER (semaphore {permits:>2}) {wall_after:8.1} {peak_after:10.1} heap {:.1}x lower",
peak_before / peak_after
);
// ── Equivalence gate: identical decode results ──
let db = dims_before.load(Ordering::Relaxed);
let da = dims_after.load(Ordering::Relaxed);
if db != da || db == 0 {
eprintln!("GATE FAIL: dimension sums differ (before={db} after={da})");
std::process::exit(1);
}
println!("\n[gate] OK — all {images} images decoded identically in both modes");
}
+436
View File
@@ -0,0 +1,436 @@
//! Grant-listing hydration N+1 benchmark + user-flags herd (ROUND4).
//!
//! [1-3] After `list_incoming_grants`, the CalDAV calendar discovery,
//! CardDAV book discovery and playlist listing each hydrated their K
//! accessible resources with K SERIAL point SELECTs (one
//! `WHERE id = $1` round-trip per resource, awaited in a loop) on every
//! client sync poll / dashboard load. AFTER: one `WHERE id = ANY($1)`
//! round-trip via the new `find_*_by_ids` batch methods — this bench
//! drives the REAL repositories both ways (the single-get methods still
//! exist for point lookups).
//!
//! [4] `get_user_flags` (called by the auth middleware on EVERY
//! authenticated request) used a get→insert cache: on each 30 s TTL
//! expiry, all in-flight requests of that user fired the SELECT
//! concurrently. AFTER: `try_get_with` single-flight. The bench
//! replicates both cache patterns around the real `UserPgRepository`
//! query, herd-style.
//!
//! Equivalence gates: identical id sets from loop vs batch for all
//! three resources; identical flags from every herd caller.
//!
//! Run (needs Postgres up; reads DATABASE_URL from .env):
//! cargo run --release --features bench --example bench_n1_hydration
//! Tunables (env): BENCH_RESOURCES (15), BENCH_PASSES (200), BENCH_HERD (32).
use std::collections::HashSet;
use std::env;
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::{Duration, Instant};
use oxicloud::domain::repositories::address_book_repository::AddressBookRepository;
use oxicloud::domain::repositories::calendar_repository::CalendarRepository;
use oxicloud::domain::repositories::playlist_repository::PlaylistRepository;
use oxicloud::infrastructure::repositories::pg::{
AddressBookPgRepository, CalendarPgRepository, PlaylistPgRepository, UserPgRepository,
};
use sqlx::PgPool;
use sqlx::postgres::PgPoolOptions;
use uuid::Uuid;
fn env_or<T: std::str::FromStr>(key: &str, default: T) -> T {
env::var(key)
.ok()
.and_then(|v| v.parse().ok())
.unwrap_or(default)
}
struct Seeded {
user_id: Uuid,
calendar_ids: Vec<Uuid>,
book_ids: Vec<Uuid>,
playlist_ids: Vec<Uuid>,
}
async fn seed(pool: &PgPool, n: usize) -> Seeded {
let user_id: Uuid = sqlx::query_scalar(
"INSERT INTO auth.users (username, email, role)
VALUES ('bench_n1', 'bench_n1@bench.invalid', 'user') RETURNING id",
)
.fetch_one(pool)
.await
.expect("seed user");
let mut calendar_ids = Vec::with_capacity(n);
let mut book_ids = Vec::with_capacity(n);
let mut playlist_ids = Vec::with_capacity(n);
for i in 0..n {
calendar_ids.push(
sqlx::query_scalar(
"INSERT INTO caldav.calendars (id, name, owner_id, color)
VALUES (gen_random_uuid(), $1, $2, '#3788d8') RETURNING id",
)
.bind(format!("Calendario {i}"))
.bind(user_id)
.fetch_one(pool)
.await
.expect("seed calendar"),
);
book_ids.push(
sqlx::query_scalar(
"INSERT INTO carddav.address_books (id, name, owner_id)
VALUES (gen_random_uuid(), $1, $2) RETURNING id",
)
.bind(format!("Libreta {i}"))
.bind(user_id)
.fetch_one(pool)
.await
.expect("seed book"),
);
playlist_ids.push(
sqlx::query_scalar(
"INSERT INTO audio.playlists (name, owner_id)
VALUES ($1, $2) RETURNING id",
)
.bind(format!("Lista {i}"))
.bind(user_id)
.fetch_one(pool)
.await
.expect("seed playlist"),
);
}
Seeded {
user_id,
calendar_ids,
book_ids,
playlist_ids,
}
}
async fn cleanup(pool: &PgPool, s: &Seeded) {
let _ = sqlx::query("DELETE FROM caldav.calendars WHERE owner_id = $1")
.bind(s.user_id)
.execute(pool)
.await;
let _ = sqlx::query("DELETE FROM carddav.address_books WHERE owner_id = $1")
.bind(s.user_id)
.execute(pool)
.await;
let _ = sqlx::query("DELETE FROM audio.playlists WHERE owner_id = $1")
.bind(s.user_id)
.execute(pool)
.await;
let _ = sqlx::query("DELETE FROM auth.users WHERE id = $1")
.bind(s.user_id)
.execute(pool)
.await;
}
fn p50(mut xs: Vec<f64>) -> f64 {
xs.sort_by(|a, b| a.partial_cmp(b).unwrap());
xs[xs.len() / 2]
}
async fn bench_pair<FB, FA, TB, TA>(
label: &str,
passes: usize,
n: usize,
mut before: FB,
mut after: FA,
) where
FB: AsyncFnMut() -> TB,
FA: AsyncFnMut() -> TA,
{
let mut lb = Vec::with_capacity(passes);
let mut la = Vec::with_capacity(passes);
for _ in 0..passes {
let t0 = Instant::now();
std::hint::black_box(before().await);
lb.push(t0.elapsed().as_secs_f64() * 1e3);
let t0 = Instant::now();
std::hint::black_box(after().await);
la.push(t0.elapsed().as_secs_f64() * 1e3);
}
let b = p50(lb);
let a = p50(la);
println!("[{label}] ms/listing (p50, K={n})");
println!(" BEFORE (K point SELECTs) {b:8.3} ({n} queries)");
println!(
" AFTER (1 × = ANY) {a:8.3} (1 query) {:.1}x",
b / a
);
}
#[tokio::main(flavor = "multi_thread")]
async fn main() {
dotenvy::dotenv().ok();
let url = env::var("DATABASE_URL")
.or_else(|_| env::var("OXICLOUD_DB_CONNECTION_STRING"))
.expect("set DATABASE_URL — the dev Postgres URL");
let n: usize = env_or("BENCH_RESOURCES", 15);
let passes: usize = env_or("BENCH_PASSES", 200);
let herd: usize = env_or("BENCH_HERD", 32);
let pool = Arc::new(
PgPoolOptions::new()
.max_connections(40)
.min_connections(40)
.acquire_timeout(Duration::from_secs(10))
.connect(&url)
.await
.expect("connect Postgres"),
);
let seeded = seed(&pool, n).await;
let cal_repo = CalendarPgRepository::new(pool.clone());
let book_repo = AddressBookPgRepository::new(pool.clone());
let pl_repo = PlaylistPgRepository::new(pool.clone());
println!("bench_n1_hydration — {n} resources/listing, {passes} passes, herd={herd}\n");
// ── [1] calendars ──
bench_pair(
"1 calendars",
passes,
n,
async || {
let mut out = Vec::with_capacity(n);
for id in &seeded.calendar_ids {
if let Ok(c) = cal_repo.find_calendar_by_id(id).await {
out.push(c);
}
}
out
},
async || {
cal_repo
.find_calendars_by_ids(&seeded.calendar_ids)
.await
.expect("batch calendars")
},
)
.await;
// ── [2] address books ──
bench_pair(
"2 address books",
passes,
n,
async || {
let mut out = Vec::with_capacity(n);
for id in &seeded.book_ids {
if let Ok(Some(b)) = book_repo.get_address_book_by_id(id).await {
out.push(b);
}
}
out
},
async || {
book_repo
.get_address_books_by_ids(&seeded.book_ids)
.await
.expect("batch books")
},
)
.await;
// ── [3] playlists ──
bench_pair(
"3 playlists",
passes,
n,
async || {
let mut out = Vec::with_capacity(n);
for id in &seeded.playlist_ids {
if let Ok(p) = pl_repo.find_playlist_by_id(id).await {
out.push(p);
}
}
out
},
async || {
pl_repo
.find_playlists_by_ids(&seeded.playlist_ids)
.await
.expect("batch playlists")
},
)
.await;
// ── Equivalence gates ──
let mut ok = true;
{
let loop_ids: HashSet<Uuid> = {
let mut s = HashSet::new();
for id in &seeded.calendar_ids {
if let Ok(c) = cal_repo.find_calendar_by_id(id).await {
s.insert(*c.id());
}
}
s
};
let batch_ids: HashSet<Uuid> = cal_repo
.find_calendars_by_ids(&seeded.calendar_ids)
.await
.expect("batch")
.iter()
.map(|c| *c.id())
.collect();
if loop_ids != batch_ids {
eprintln!("GATE FAIL calendars: {loop_ids:?} != {batch_ids:?}");
ok = false;
}
// Missing ids drop out on both sides.
let with_ghost: Vec<Uuid> = seeded
.calendar_ids
.iter()
.copied()
.chain([Uuid::new_v4()])
.collect();
let ghost_ids: HashSet<Uuid> = cal_repo
.find_calendars_by_ids(&with_ghost)
.await
.expect("batch+ghost")
.iter()
.map(|c| *c.id())
.collect();
if ghost_ids != batch_ids {
eprintln!("GATE FAIL calendars: ghost id changed result");
ok = false;
}
}
{
let loop_ids: HashSet<Uuid> = {
let mut s = HashSet::new();
for id in &seeded.book_ids {
if let Ok(Some(b)) = book_repo.get_address_book_by_id(id).await {
s.insert(*b.id());
}
}
s
};
let batch_ids: HashSet<Uuid> = book_repo
.get_address_books_by_ids(&seeded.book_ids)
.await
.expect("batch")
.iter()
.map(|b| *b.id())
.collect();
if loop_ids != batch_ids {
eprintln!("GATE FAIL books");
ok = false;
}
}
{
let loop_ids: HashSet<Uuid> = {
let mut s = HashSet::new();
for id in &seeded.playlist_ids {
if let Ok(p) = pl_repo.find_playlist_by_id(id).await {
s.insert(*p.id());
}
}
s
};
let batch_ids: HashSet<Uuid> = pl_repo
.find_playlists_by_ids(&seeded.playlist_ids)
.await
.expect("batch")
.iter()
.map(|p| *p.id())
.collect();
if loop_ids != batch_ids {
eprintln!("GATE FAIL playlists");
ok = false;
}
}
// ── [4] user-flags herd: get→insert vs try_get_with ─────────────────────
let user_repo = Arc::new(UserPgRepository::new(pool.clone()));
let queries = Arc::new(AtomicUsize::new(0));
// BEFORE: sync moka get/insert — every cold caller queries.
let sync_cache: moka::sync::Cache<Uuid, oxicloud::domain::entities::user::UserFlags> =
moka::sync::Cache::builder()
.max_capacity(10_000)
.time_to_live(Duration::from_secs(30))
.build();
let t0 = Instant::now();
let mut handles = Vec::new();
for _ in 0..herd {
let cache = sync_cache.clone();
let repo = user_repo.clone();
let queries = queries.clone();
let uid = seeded.user_id;
handles.push(tokio::spawn(async move {
if let Some(f) = cache.get(&uid) {
return f;
}
queries.fetch_add(1, Ordering::Relaxed);
let f = repo.get_user_flags(uid).await.expect("flags");
cache.insert(uid, f);
f
}));
}
let mut before_flags = Vec::new();
for h in handles {
before_flags.push(h.await.unwrap());
}
let before_wall = t0.elapsed().as_secs_f64() * 1e3;
let before_queries = queries.swap(0, Ordering::Relaxed);
// AFTER: future moka try_get_with — one query per herd.
let future_cache: moka::future::Cache<Uuid, oxicloud::domain::entities::user::UserFlags> =
moka::future::Cache::builder()
.max_capacity(10_000)
.time_to_live(Duration::from_secs(30))
.build();
let t0 = Instant::now();
let mut handles = Vec::new();
for _ in 0..herd {
let cache = future_cache.clone();
let repo = user_repo.clone();
let queries = queries.clone();
let uid = seeded.user_id;
handles.push(tokio::spawn(async move {
cache
.try_get_with(uid, async {
queries.fetch_add(1, Ordering::Relaxed);
repo.get_user_flags(uid).await
})
.await
.expect("flags")
}));
}
let mut after_flags = Vec::new();
for h in handles {
after_flags.push(h.await.unwrap());
}
let after_wall = t0.elapsed().as_secs_f64() * 1e3;
let after_queries = queries.load(Ordering::Relaxed);
println!("[4] user-flags cold-cache herd of {herd}");
println!(" BEFORE (get→insert) {before_wall:7.2} ms {before_queries} queries");
println!(" AFTER (try_get_with) {after_wall:7.2} ms {after_queries} queries");
for f in before_flags.iter().chain(&after_flags) {
if *f != before_flags[0] {
eprintln!("GATE FAIL user flags mismatch");
ok = false;
}
}
cleanup(&pool, &seeded).await;
println!(
"\n[gate] {}",
if ok {
"OK (identical result sets)"
} else {
"FAILED"
}
);
if !ok {
std::process::exit(1);
}
}
+801
View File
@@ -0,0 +1,801 @@
//! PROPFIND per-row XML emit benchmark — Vec churn + format-interpreter
//! dates (ROUND4).
//!
//! For EVERY file/folder row of every PROPFIND page the old writers paid:
//! • a `partition` into two throwaway `Vec<&QualifiedName>`s (+ a third
//! for the 404 list) — even though the requested-props writer already
//! skips unknown names itself;
//! • `to_rfc3339()` + `to_rfc2822()` — chrono's format-spec interpreter
//! plus a heap String each;
//! • `size.to_string()` and a `format!("\"{etag}\"")`.
//!
//! AFTER: single-pass 404 computation (usually-empty Vec), stack-rendered
//! dates/sizes (`common::fmt`, byte-identical, chrono fallback for
//! out-of-range), exactly-sized etag quoting.
//!
//! The OLD writers are copied verbatim into `mod before`; the gate
//! asserts byte-identical multistatus XML for named-prop (typical sync
//! client set + unknown props), AllProp (with quota), and dead-prop
//! carrying rows. Exit 1 on any diff.
//!
//! Run (no Postgres needed):
//! cargo run --release --features bench --example bench_propfind_xml
//! Tunables (env): BENCH_ROWS (1000), BENCH_PASSES (200)
use std::alloc::{GlobalAlloc, Layout, System};
use std::env;
use std::hint::black_box;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::Instant;
use oxicloud::application::adapters::webdav_adapter::{
PropFindRequest, PropFindType, QualifiedName, bench as dav_bench,
};
use oxicloud::application::dtos::file_dto::FileDto;
use oxicloud::application::dtos::folder_dto::FolderDto;
use oxicloud::domain::entities::file::File;
use oxicloud::domain::entities::folder::Folder;
use uuid::Uuid;
// ─── Counting allocator ─────────────────────────────────────────────────────
static ALLOC_CALLS: AtomicU64 = AtomicU64::new(0);
struct CountingAlloc;
unsafe impl GlobalAlloc for CountingAlloc {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
ALLOC_CALLS.fetch_add(1, Ordering::Relaxed);
unsafe { System.alloc(layout) }
}
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
unsafe { System.dealloc(ptr, layout) }
}
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
ALLOC_CALLS.fetch_add(1, Ordering::Relaxed);
unsafe { System.realloc(ptr, layout, new_size) }
}
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
ALLOC_CALLS.fetch_add(1, Ordering::Relaxed);
unsafe { System.alloc_zeroed(layout) }
}
}
#[global_allocator]
static GLOBAL: CountingAlloc = CountingAlloc;
// ─── BEFORE: verbatim copy of the old per-row writers ───────────────────────
#[allow(clippy::all)]
mod before {
use chrono::Utc;
use oxicloud::application::adapters::webdav_adapter::{
PropFindRequest, PropFindType, QualifiedName,
};
use oxicloud::application::dtos::file_dto::FileDto;
use oxicloud::application::dtos::folder_dto::FolderDto;
use quick_xml::Writer;
use quick_xml::events::{BytesEnd, BytesStart, BytesText, Event};
use std::io::Write;
type Result<T> = std::result::Result<T, quick_xml::Error>;
fn folder_prop_is_known(prop: &QualifiedName, quota: Option<(i64, Option<i64>)>) -> bool {
if prop.namespace != "DAV:" {
return false;
}
match prop.name.as_str() {
"resourcetype" | "displayname" | "creationdate" | "getlastmodified" | "getetag"
| "getcontentlength" | "getcontenttype" => true,
"quota-used-bytes" => quota.is_some(),
"quota-available-bytes" => quota.is_some_and(|(_, available)| available.is_some()),
_ => false,
}
}
fn file_prop_is_known(prop: &QualifiedName) -> bool {
prop.namespace == "DAV:"
&& matches!(
prop.name.as_str(),
"resourcetype"
| "displayname"
| "getcontenttype"
| "getcontentlength"
| "creationdate"
| "getlastmodified"
| "getetag"
)
}
fn write_qname_empty<W: Write>(xml_writer: &mut Writer<W>, prop: &QualifiedName) -> Result<()> {
if prop.namespace.is_empty() {
xml_writer.write_event(Event::Empty(BytesStart::new(prop.name.as_str())))?;
} else if prop.namespace == "DAV:" {
xml_writer.write_event(Event::Empty(BytesStart::new(format!("D:{}", prop.name))))?;
} else {
let tag = format!("X:{}", prop.name);
let mut start = BytesStart::new(tag.as_str());
start.push_attribute(("xmlns:X", prop.namespace.as_str()));
xml_writer.write_event(Event::Empty(start))?;
}
Ok(())
}
fn write_unknown_props_404<W: Write>(
xml_writer: &mut Writer<W>,
unknown: &[&QualifiedName],
) -> Result<()> {
if unknown.is_empty() {
return Ok(());
}
xml_writer.write_event(Event::Start(BytesStart::new("D:propstat")))?;
xml_writer.write_event(Event::Start(BytesStart::new("D:prop")))?;
for prop in unknown {
write_qname_empty(xml_writer, prop)?;
}
xml_writer.write_event(Event::End(BytesEnd::new("D:prop")))?;
xml_writer.write_event(Event::Start(BytesStart::new("D:status")))?;
xml_writer.write_event(Event::Text(BytesText::new("HTTP/1.1 404 Not Found")))?;
xml_writer.write_event(Event::End(BytesEnd::new("D:status")))?;
xml_writer.write_event(Event::End(BytesEnd::new("D:propstat")))?;
Ok(())
}
fn write_dead_props_propstat<W: Write>(
xml_writer: &mut Writer<W>,
dead_props: &[(QualifiedName, Option<String>)],
) -> Result<()> {
if dead_props.is_empty() {
return Ok(());
}
xml_writer.write_event(Event::Start(BytesStart::new("D:propstat")))?;
xml_writer.write_event(Event::Start(BytesStart::new("D:prop")))?;
for (name, value) in dead_props {
let tag = if name.namespace.is_empty() {
name.name.clone()
} else {
format!("X:{}", name.name)
};
let mut start = BytesStart::new(tag.as_str());
if !name.namespace.is_empty() {
start.push_attribute(("xmlns:X", name.namespace.as_str()));
}
match value {
Some(v) if !v.is_empty() => {
xml_writer.write_event(Event::Start(start))?;
xml_writer.write_event(Event::Text(BytesText::new(v)))?;
xml_writer.write_event(Event::End(BytesEnd::new(tag.as_str())))?;
}
_ => {
xml_writer.write_event(Event::Empty(start))?;
}
}
}
xml_writer.write_event(Event::End(BytesEnd::new("D:prop")))?;
xml_writer.write_event(Event::Start(BytesStart::new("D:status")))?;
xml_writer.write_event(Event::Text(BytesText::new("HTTP/1.1 200 OK")))?;
xml_writer.write_event(Event::End(BytesEnd::new("D:status")))?;
xml_writer.write_event(Event::End(BytesEnd::new("D:propstat")))?;
Ok(())
}
fn write_quota_props<W: Write>(
xml_writer: &mut Writer<W>,
used_bytes: i64,
available_bytes: Option<i64>,
) -> Result<()> {
xml_writer.write_event(Event::Start(BytesStart::new("D:quota-used-bytes")))?;
xml_writer.write_event(Event::Text(BytesText::new(&used_bytes.to_string())))?;
xml_writer.write_event(Event::End(BytesEnd::new("D:quota-used-bytes")))?;
if let Some(available_bytes) = available_bytes {
xml_writer.write_event(Event::Start(BytesStart::new("D:quota-available-bytes")))?;
xml_writer.write_event(Event::Text(BytesText::new(&available_bytes.to_string())))?;
xml_writer.write_event(Event::End(BytesEnd::new("D:quota-available-bytes")))?;
}
Ok(())
}
fn write_folder_standard_props<W: Write>(
xml_writer: &mut Writer<W>,
folder: &FolderDto,
quota: Option<(i64, Option<i64>)>,
) -> Result<()> {
xml_writer.write_event(Event::Start(BytesStart::new("D:resourcetype")))?;
xml_writer.write_event(Event::Empty(BytesStart::new("D:collection")))?;
xml_writer.write_event(Event::End(BytesEnd::new("D:resourcetype")))?;
xml_writer.write_event(Event::Start(BytesStart::new("D:displayname")))?;
xml_writer.write_event(Event::Text(BytesText::new(&folder.name)))?;
xml_writer.write_event(Event::End(BytesEnd::new("D:displayname")))?;
xml_writer.write_event(Event::Start(BytesStart::new("D:creationdate")))?;
let created_at = chrono::DateTime::<Utc>::from_timestamp(folder.created_at as i64, 0)
.unwrap_or_else(Utc::now);
xml_writer.write_event(Event::Text(BytesText::new(&created_at.to_rfc3339())))?;
xml_writer.write_event(Event::End(BytesEnd::new("D:creationdate")))?;
xml_writer.write_event(Event::Start(BytesStart::new("D:getlastmodified")))?;
let modified_at = chrono::DateTime::<Utc>::from_timestamp(folder.modified_at as i64, 0)
.unwrap_or_else(Utc::now);
xml_writer.write_event(Event::Text(BytesText::new(&modified_at.to_rfc2822())))?;
xml_writer.write_event(Event::End(BytesEnd::new("D:getlastmodified")))?;
xml_writer.write_event(Event::Start(BytesStart::new("D:getetag")))?;
xml_writer.write_event(Event::Text(BytesText::new(&format!("\"{}\"", folder.etag))))?;
xml_writer.write_event(Event::End(BytesEnd::new("D:getetag")))?;
xml_writer.write_event(Event::Start(BytesStart::new("D:getcontentlength")))?;
xml_writer.write_event(Event::Text(BytesText::new("0")))?;
xml_writer.write_event(Event::End(BytesEnd::new("D:getcontentlength")))?;
xml_writer.write_event(Event::Start(BytesStart::new("D:getcontenttype")))?;
xml_writer.write_event(Event::Text(BytesText::new("httpd/unix-directory")))?;
xml_writer.write_event(Event::End(BytesEnd::new("D:getcontenttype")))?;
if let Some((used, available)) = quota {
write_quota_props(xml_writer, used, available)?;
}
Ok(())
}
fn write_file_standard_props<W: Write>(
xml_writer: &mut Writer<W>,
file: &FileDto,
) -> Result<()> {
xml_writer.write_event(Event::Empty(BytesStart::new("D:resourcetype")))?;
xml_writer.write_event(Event::Start(BytesStart::new("D:displayname")))?;
xml_writer.write_event(Event::Text(BytesText::new(&file.name)))?;
xml_writer.write_event(Event::End(BytesEnd::new("D:displayname")))?;
xml_writer.write_event(Event::Start(BytesStart::new("D:getcontenttype")))?;
xml_writer.write_event(Event::Text(BytesText::new(&file.mime_type)))?;
xml_writer.write_event(Event::End(BytesEnd::new("D:getcontenttype")))?;
xml_writer.write_event(Event::Start(BytesStart::new("D:getcontentlength")))?;
xml_writer.write_event(Event::Text(BytesText::new(&file.size.to_string())))?;
xml_writer.write_event(Event::End(BytesEnd::new("D:getcontentlength")))?;
xml_writer.write_event(Event::Start(BytesStart::new("D:creationdate")))?;
let created_at = chrono::DateTime::<Utc>::from_timestamp(file.created_at as i64, 0)
.unwrap_or_else(Utc::now);
xml_writer.write_event(Event::Text(BytesText::new(&created_at.to_rfc3339())))?;
xml_writer.write_event(Event::End(BytesEnd::new("D:creationdate")))?;
xml_writer.write_event(Event::Start(BytesStart::new("D:getlastmodified")))?;
let modified_at = chrono::DateTime::<Utc>::from_timestamp(file.modified_at as i64, 0)
.unwrap_or_else(Utc::now);
xml_writer.write_event(Event::Text(BytesText::new(&modified_at.to_rfc2822())))?;
xml_writer.write_event(Event::End(BytesEnd::new("D:getlastmodified")))?;
xml_writer.write_event(Event::Start(BytesStart::new("D:getetag")))?;
xml_writer.write_event(Event::Text(BytesText::new(&format!("\"{}\"", file.etag))))?;
xml_writer.write_event(Event::End(BytesEnd::new("D:getetag")))?;
Ok(())
}
fn write_folder_requested_props<W: Write>(
xml_writer: &mut Writer<W>,
folder: &FolderDto,
props: &[&QualifiedName],
quota: Option<(i64, Option<i64>)>,
) -> Result<()> {
for prop in props {
if prop.namespace == "DAV:" {
match prop.name.as_str() {
"resourcetype" => {
xml_writer.write_event(Event::Start(BytesStart::new("D:resourcetype")))?;
xml_writer.write_event(Event::Empty(BytesStart::new("D:collection")))?;
xml_writer.write_event(Event::End(BytesEnd::new("D:resourcetype")))?;
}
"displayname" => {
xml_writer.write_event(Event::Start(BytesStart::new("D:displayname")))?;
xml_writer.write_event(Event::Text(BytesText::new(&folder.name)))?;
xml_writer.write_event(Event::End(BytesEnd::new("D:displayname")))?;
}
"creationdate" => {
xml_writer.write_event(Event::Start(BytesStart::new("D:creationdate")))?;
let created_at =
chrono::DateTime::<Utc>::from_timestamp(folder.created_at as i64, 0)
.unwrap_or_else(Utc::now);
xml_writer
.write_event(Event::Text(BytesText::new(&created_at.to_rfc3339())))?;
xml_writer.write_event(Event::End(BytesEnd::new("D:creationdate")))?;
}
"getlastmodified" => {
xml_writer
.write_event(Event::Start(BytesStart::new("D:getlastmodified")))?;
let modified_at =
chrono::DateTime::<Utc>::from_timestamp(folder.modified_at as i64, 0)
.unwrap_or_else(Utc::now);
xml_writer
.write_event(Event::Text(BytesText::new(&modified_at.to_rfc2822())))?;
xml_writer.write_event(Event::End(BytesEnd::new("D:getlastmodified")))?;
}
"getetag" => {
xml_writer.write_event(Event::Start(BytesStart::new("D:getetag")))?;
xml_writer.write_event(Event::Text(BytesText::new(&format!(
"\"{}\"",
folder.etag
))))?;
xml_writer.write_event(Event::End(BytesEnd::new("D:getetag")))?;
}
"getcontentlength" => {
xml_writer
.write_event(Event::Start(BytesStart::new("D:getcontentlength")))?;
xml_writer.write_event(Event::Text(BytesText::new("0")))?;
xml_writer.write_event(Event::End(BytesEnd::new("D:getcontentlength")))?;
}
"getcontenttype" => {
xml_writer
.write_event(Event::Start(BytesStart::new("D:getcontenttype")))?;
xml_writer
.write_event(Event::Text(BytesText::new("httpd/unix-directory")))?;
xml_writer.write_event(Event::End(BytesEnd::new("D:getcontenttype")))?;
}
"quota-used-bytes" => {
if let Some((used, _)) = quota {
xml_writer
.write_event(Event::Start(BytesStart::new("D:quota-used-bytes")))?;
xml_writer
.write_event(Event::Text(BytesText::new(&used.to_string())))?;
xml_writer
.write_event(Event::End(BytesEnd::new("D:quota-used-bytes")))?;
}
}
"quota-available-bytes" => {
if let Some((_, Some(available))) = quota {
xml_writer.write_event(Event::Start(BytesStart::new(
"D:quota-available-bytes",
)))?;
xml_writer
.write_event(Event::Text(BytesText::new(&available.to_string())))?;
xml_writer.write_event(Event::End(BytesEnd::new(
"D:quota-available-bytes",
)))?;
}
}
_ => {}
}
}
}
Ok(())
}
fn write_file_requested_props<W: Write>(
xml_writer: &mut Writer<W>,
file: &FileDto,
props: &[&QualifiedName],
) -> Result<()> {
for prop in props {
if prop.namespace == "DAV:" {
match prop.name.as_str() {
"resourcetype" => {
xml_writer.write_event(Event::Empty(BytesStart::new("D:resourcetype")))?;
}
"displayname" => {
xml_writer.write_event(Event::Start(BytesStart::new("D:displayname")))?;
xml_writer.write_event(Event::Text(BytesText::new(&file.name)))?;
xml_writer.write_event(Event::End(BytesEnd::new("D:displayname")))?;
}
"getcontenttype" => {
xml_writer
.write_event(Event::Start(BytesStart::new("D:getcontenttype")))?;
xml_writer.write_event(Event::Text(BytesText::new(&file.mime_type)))?;
xml_writer.write_event(Event::End(BytesEnd::new("D:getcontenttype")))?;
}
"getcontentlength" => {
xml_writer
.write_event(Event::Start(BytesStart::new("D:getcontentlength")))?;
xml_writer
.write_event(Event::Text(BytesText::new(&file.size.to_string())))?;
xml_writer.write_event(Event::End(BytesEnd::new("D:getcontentlength")))?;
}
"creationdate" => {
xml_writer.write_event(Event::Start(BytesStart::new("D:creationdate")))?;
let created_at =
chrono::DateTime::<Utc>::from_timestamp(file.created_at as i64, 0)
.unwrap_or_else(Utc::now);
xml_writer
.write_event(Event::Text(BytesText::new(&created_at.to_rfc3339())))?;
xml_writer.write_event(Event::End(BytesEnd::new("D:creationdate")))?;
}
"getlastmodified" => {
xml_writer
.write_event(Event::Start(BytesStart::new("D:getlastmodified")))?;
let modified_at =
chrono::DateTime::<Utc>::from_timestamp(file.modified_at as i64, 0)
.unwrap_or_else(Utc::now);
xml_writer
.write_event(Event::Text(BytesText::new(&modified_at.to_rfc2822())))?;
xml_writer.write_event(Event::End(BytesEnd::new("D:getlastmodified")))?;
}
"getetag" => {
xml_writer.write_event(Event::Start(BytesStart::new("D:getetag")))?;
xml_writer.write_event(Event::Text(BytesText::new(&format!(
"\"{}\"",
file.etag
))))?;
xml_writer.write_event(Event::End(BytesEnd::new("D:getetag")))?;
}
_ => {}
}
}
}
Ok(())
}
pub fn write_file_response_with_dead_props<W: Write>(
xml_writer: &mut Writer<W>,
file: &FileDto,
request: &PropFindRequest,
href: &str,
dead_props: &[(QualifiedName, Option<String>)],
) -> Result<()> {
xml_writer.write_event(Event::Start(BytesStart::new("D:response")))?;
xml_writer.write_event(Event::Start(BytesStart::new("D:href")))?;
xml_writer.write_event(Event::Text(BytesText::new(href)))?;
xml_writer.write_event(Event::End(BytesEnd::new("D:href")))?;
let relevant_dead: Vec<_> = match &request.prop_find_type {
PropFindType::Prop(requested) => dead_props
.iter()
.filter(|(name, _)| requested.iter().any(|r| r == name))
.cloned()
.collect(),
PropFindType::AllProp => dead_props.to_vec(),
PropFindType::PropName => vec![],
};
let dead_name_set: std::collections::HashSet<&QualifiedName> =
relevant_dead.iter().map(|(n, _)| n).collect();
match &request.prop_find_type {
PropFindType::Prop(props) => {
let (known, unknown): (Vec<_>, Vec<_>) =
props.iter().partition(|p| file_prop_is_known(p));
let truly_unknown: Vec<_> = unknown
.into_iter()
.filter(|p| !dead_name_set.contains(*p))
.collect();
xml_writer.write_event(Event::Start(BytesStart::new("D:propstat")))?;
xml_writer.write_event(Event::Start(BytesStart::new("D:prop")))?;
write_file_requested_props(xml_writer, file, &known)?;
xml_writer.write_event(Event::End(BytesEnd::new("D:prop")))?;
xml_writer.write_event(Event::Start(BytesStart::new("D:status")))?;
xml_writer.write_event(Event::Text(BytesText::new("HTTP/1.1 200 OK")))?;
xml_writer.write_event(Event::End(BytesEnd::new("D:status")))?;
xml_writer.write_event(Event::End(BytesEnd::new("D:propstat")))?;
write_unknown_props_404(xml_writer, &truly_unknown)?;
}
other => {
xml_writer.write_event(Event::Start(BytesStart::new("D:propstat")))?;
xml_writer.write_event(Event::Start(BytesStart::new("D:prop")))?;
match other {
PropFindType::AllProp => {
write_file_standard_props(xml_writer, file)?;
}
PropFindType::PropName => {
// not exercised in this bench
}
PropFindType::Prop(_) => unreachable!(),
}
xml_writer.write_event(Event::End(BytesEnd::new("D:prop")))?;
xml_writer.write_event(Event::Start(BytesStart::new("D:status")))?;
xml_writer.write_event(Event::Text(BytesText::new("HTTP/1.1 200 OK")))?;
xml_writer.write_event(Event::End(BytesEnd::new("D:status")))?;
xml_writer.write_event(Event::End(BytesEnd::new("D:propstat")))?;
}
}
write_dead_props_propstat(xml_writer, &relevant_dead)?;
xml_writer.write_event(Event::End(BytesEnd::new("D:response")))?;
Ok(())
}
pub fn write_folder_response_with_dead_props<W: Write>(
xml_writer: &mut Writer<W>,
folder: &FolderDto,
request: &PropFindRequest,
href: &str,
dead_props: &[(QualifiedName, Option<String>)],
quota: Option<(i64, Option<i64>)>,
) -> Result<()> {
xml_writer.write_event(Event::Start(BytesStart::new("D:response")))?;
xml_writer.write_event(Event::Start(BytesStart::new("D:href")))?;
xml_writer.write_event(Event::Text(BytesText::new(href)))?;
xml_writer.write_event(Event::End(BytesEnd::new("D:href")))?;
let relevant_dead: Vec<_> = match &request.prop_find_type {
PropFindType::Prop(requested) => dead_props
.iter()
.filter(|(name, _)| requested.iter().any(|r| r == name))
.cloned()
.collect(),
PropFindType::AllProp => dead_props.to_vec(),
PropFindType::PropName => vec![],
};
let dead_name_set: std::collections::HashSet<&QualifiedName> =
relevant_dead.iter().map(|(n, _)| n).collect();
match &request.prop_find_type {
PropFindType::Prop(props) => {
let (known, unknown): (Vec<_>, Vec<_>) =
props.iter().partition(|p| folder_prop_is_known(p, quota));
let truly_unknown: Vec<_> = unknown
.into_iter()
.filter(|p| !dead_name_set.contains(*p))
.collect();
xml_writer.write_event(Event::Start(BytesStart::new("D:propstat")))?;
xml_writer.write_event(Event::Start(BytesStart::new("D:prop")))?;
write_folder_requested_props(xml_writer, folder, &known, quota)?;
xml_writer.write_event(Event::End(BytesEnd::new("D:prop")))?;
xml_writer.write_event(Event::Start(BytesStart::new("D:status")))?;
xml_writer.write_event(Event::Text(BytesText::new("HTTP/1.1 200 OK")))?;
xml_writer.write_event(Event::End(BytesEnd::new("D:status")))?;
xml_writer.write_event(Event::End(BytesEnd::new("D:propstat")))?;
write_unknown_props_404(xml_writer, &truly_unknown)?;
}
other => {
xml_writer.write_event(Event::Start(BytesStart::new("D:propstat")))?;
xml_writer.write_event(Event::Start(BytesStart::new("D:prop")))?;
match other {
PropFindType::AllProp => {
write_folder_standard_props(xml_writer, folder, quota)?;
}
PropFindType::PropName => {}
PropFindType::Prop(_) => unreachable!(),
}
xml_writer.write_event(Event::End(BytesEnd::new("D:prop")))?;
xml_writer.write_event(Event::Start(BytesStart::new("D:status")))?;
xml_writer.write_event(Event::Text(BytesText::new("HTTP/1.1 200 OK")))?;
xml_writer.write_event(Event::End(BytesEnd::new("D:status")))?;
xml_writer.write_event(Event::End(BytesEnd::new("D:propstat")))?;
}
}
write_dead_props_propstat(xml_writer, &relevant_dead)?;
xml_writer.write_event(Event::End(BytesEnd::new("D:response")))?;
Ok(())
}
}
// ─── Corpus ─────────────────────────────────────────────────────────────────
fn build_files(rows: usize) -> Vec<FileDto> {
(0..rows)
.map(|i| {
// Timestamp mix: epoch edge, padded-day dates, recent, far future.
let created = [0u64, 1_120_176_000, 1_700_000_000, 4_102_444_799][i % 4];
let f = File::from_materialized_row(
Uuid::from_u128(i as u128).to_string(),
format!("informe-{i}.pdf"),
Some("/Personal/Projects/2026"),
(i as u64) * 3_517 + 42,
"application/pdf".to_string(),
Some(Uuid::nil().to_string()),
created,
created + 86_400 * (i as u64 % 300),
format!("{:032x}", i * 2_654_435_761),
None,
None,
)
.expect("valid file");
FileDto::from(f)
})
.collect()
}
fn build_folders(rows: usize) -> Vec<FolderDto> {
(0..rows)
.map(|i| {
let created = [0u64, 1_120_176_000, 1_700_000_000, 4_102_444_799][i % 4];
let f = Folder::from_materialized_row(
Uuid::from_u128((1_000_000 + i) as u128).to_string(),
format!("Carpeta {i}"),
format!("/Personal/Carpeta {i}"),
None,
Uuid::nil(),
created,
created + 3_600,
created + 7_200,
None,
None,
)
.expect("valid folder");
FolderDto::from(f)
})
.collect()
}
/// The prop set DAVx⁵/rclone-style clients poll with, plus two unknown
/// names so the 404 path is exercised.
fn sync_request() -> PropFindRequest {
PropFindRequest {
prop_find_type: PropFindType::Prop(vec![
QualifiedName::new("DAV:", "resourcetype"),
QualifiedName::new("DAV:", "displayname"),
QualifiedName::new("DAV:", "getcontenttype"),
QualifiedName::new("DAV:", "getcontentlength"),
QualifiedName::new("DAV:", "getlastmodified"),
QualifiedName::new("DAV:", "getetag"),
QualifiedName::new("DAV:", "lockdiscovery"),
QualifiedName::new("http://owncloud.org/ns", "fileid"),
]),
}
}
fn allprop_request() -> PropFindRequest {
PropFindRequest {
prop_find_type: PropFindType::AllProp,
}
}
fn p50(mut xs: Vec<f64>) -> f64 {
xs.sort_by(|a, b| a.partial_cmp(b).unwrap());
xs[xs.len() / 2]
}
const QUOTA: Option<(i64, Option<i64>)> = Some((123_456_789, Some(9_876_543_210)));
fn render_before(
files: &[FileDto],
folders: &[FolderDto],
request: &PropFindRequest,
dead: &[(QualifiedName, Option<String>)],
) -> Vec<u8> {
let mut out = Vec::with_capacity(1 << 20);
let mut w = quick_xml::Writer::new(&mut out);
for (i, folder) in folders.iter().enumerate() {
let dead = if i % 7 == 0 { dead } else { &[] };
before::write_folder_response_with_dead_props(
&mut w,
folder,
request,
"/webdav/Personal/",
dead,
QUOTA,
)
.expect("before folder row");
}
for (i, file) in files.iter().enumerate() {
let dead = if i % 7 == 0 { dead } else { &[] };
before::write_file_response_with_dead_props(
&mut w,
file,
request,
"/webdav/Personal/informe.pdf",
dead,
)
.expect("before file row");
}
out
}
fn render_after(
files: &[FileDto],
folders: &[FolderDto],
request: &PropFindRequest,
dead: &[(QualifiedName, Option<String>)],
) -> Vec<u8> {
let mut out = Vec::with_capacity(1 << 20);
let mut w = quick_xml::Writer::new(&mut out);
for (i, folder) in folders.iter().enumerate() {
let dead = if i % 7 == 0 { dead } else { &[] };
dav_bench::write_folder_propfind_row(
&mut w,
folder,
request,
"/webdav/Personal/",
dead,
QUOTA,
)
.expect("after folder row");
}
for (i, file) in files.iter().enumerate() {
let dead = if i % 7 == 0 { dead } else { &[] };
dav_bench::write_file_propfind_row(
&mut w,
file,
request,
"/webdav/Personal/informe.pdf",
dead,
)
.expect("after file row");
}
out
}
fn main() {
let rows: usize = env::var("BENCH_ROWS")
.ok()
.and_then(|v| v.parse().ok())
.unwrap_or(1000);
let passes: usize = env::var("BENCH_PASSES")
.ok()
.and_then(|v| v.parse().ok())
.unwrap_or(200);
let files = build_files(rows);
let folders = build_folders(rows / 10);
let total_rows = files.len() + folders.len();
let dead: Vec<(QualifiedName, Option<String>)> = vec![(
QualifiedName::new("http://example.com/ns", "color"),
Some("azul".to_string()),
)];
let sync_req = sync_request();
let all_req = allprop_request();
println!(
"bench_propfind_xml — {} files + {} folders/page, {passes} passes\n",
files.len(),
folders.len()
);
for (label, req) in [("named-prop (sync set)", &sync_req), ("allprop", &all_req)] {
let mut lat_before = Vec::with_capacity(passes);
let mut lat_after = Vec::with_capacity(passes);
for _ in 0..passes {
let t0 = Instant::now();
black_box(render_before(&files, &folders, req, &dead));
lat_before.push(t0.elapsed().as_secs_f64() * 1e6);
let t0 = Instant::now();
black_box(render_after(&files, &folders, req, &dead));
lat_after.push(t0.elapsed().as_secs_f64() * 1e6);
}
let b = p50(lat_before);
let a = p50(lat_after);
let s0 = ALLOC_CALLS.load(Ordering::Relaxed);
black_box(render_before(&files, &folders, req, &dead));
let ab = (ALLOC_CALLS.load(Ordering::Relaxed) - s0) as f64 / total_rows as f64;
let s0 = ALLOC_CALLS.load(Ordering::Relaxed);
black_box(render_after(&files, &folders, req, &dead));
let aa = (ALLOC_CALLS.load(Ordering::Relaxed) - s0) as f64 / total_rows as f64;
println!("[{label}] µs/page (p50) + allocs/row");
println!(" BEFORE {b:9.1} µs {ab:6.2} allocs/row");
println!(
" AFTER {a:9.1} µs {aa:6.2} allocs/row {:.2}x",
b / a
);
}
// ── Equivalence gate: byte-identical multistatus XML ────────────────────
let mut ok = true;
for req in [&sync_req, &all_req] {
let xb = render_before(&files, &folders, req, &dead);
let xa = render_after(&files, &folders, req, &dead);
if xb != xa {
ok = false;
let diff_at = xb.iter().zip(&xa).position(|(a, b)| a != b).unwrap_or(0);
let lo = diff_at.saturating_sub(120);
eprintln!(
"GATE FAIL ({:?}): first diff at byte {diff_at}\n BEFORE: …{}…\n AFTER: …{}…",
match req.prop_find_type {
PropFindType::Prop(_) => "prop",
PropFindType::AllProp => "allprop",
PropFindType::PropName => "propname",
},
String::from_utf8_lossy(&xb[lo..(diff_at + 120).min(xb.len())]),
String::from_utf8_lossy(&xa[lo..(diff_at + 120).min(xa.len())]),
);
}
}
println!(
"\n[gate] multistatus XML: {}",
if ok { "OK (byte-identical)" } else { "FAILED" }
);
if !ok {
std::process::exit(1);
}
}
+669
View File
@@ -0,0 +1,669 @@
//! PG row → entity path materialization benchmark — the per-listing-row
//! `make_file_path` split→rejoin + NFC-copy chain (ROUND3 follow-up).
//!
//! Every listing row (PROPFIND batches, photos timeline, search pages,
//! by-ids enrichment, subtree ZIP streams) used to pay this chain:
//!
//! • files: `format!("{fp}/{name}")` temp → `StoragePath::from_string`
//! split (one `String` per segment + `Vec`) → constructor NFC-copies
//! the already-NFC name → `Display`/`join` re-joins the segments it
//! just split into `path_string` (join temp + unsized `to_string`).
//! • folders: same minus the format temp — the materialized `path`
//! column arrives owned, is split, dropped, and re-joined into an
//! identical `String`.
//!
//! The optimized path builds segments + joined string in ONE pass
//! (`StoragePath::from_folder_and_name` / `from_joined`, the latter
//! reusing the owned input when canonical) and normalizes the owned name
//! without the always-copy (`normalize_storage_name_owned`).
//!
//! The OLD logic is copied verbatim into `mod before` so one binary
//! reports BEFORE vs AFTER side by side; an equivalence gate asserts
//! byte-identical (name, path_string, segments) triples — including
//! adversarial non-canonical inputs — and error parity for invalid
//! names (exit 1 on any diff).
//!
//! Sections:
//! 1. File row wall time (p50 ns/row over BENCH_PASSES passes)
//! 2. Folder row wall time (same)
//! 3. Alloc calls/row (counting allocator wrapping System — the lib
//! crate sets no global allocator; mimalloc lives in main.rs only)
//! 4. Equivalence gate (realistic corpus + adversarial set)
//!
//! Run (no Postgres needed):
//! cargo run --release --features bench --example bench_row_path
//! Tunables (env):
//! BENCH_ROWS (10000) BENCH_PASSES (100)
use std::alloc::{GlobalAlloc, Layout, System};
use std::env;
use std::hint::black_box;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::Instant;
use oxicloud::domain::entities::file::File;
use oxicloud::domain::entities::folder::Folder;
use uuid::Uuid;
// ─── Counting allocator (Section 3) ─────────────────────────────────────────
static ALLOC_CALLS: AtomicU64 = AtomicU64::new(0);
struct CountingAlloc;
unsafe impl GlobalAlloc for CountingAlloc {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
ALLOC_CALLS.fetch_add(1, Ordering::Relaxed);
unsafe { System.alloc(layout) }
}
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
unsafe { System.dealloc(ptr, layout) }
}
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
ALLOC_CALLS.fetch_add(1, Ordering::Relaxed);
unsafe { System.realloc(ptr, layout, new_size) }
}
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
ALLOC_CALLS.fetch_add(1, Ordering::Relaxed);
unsafe { System.alloc_zeroed(layout) }
}
}
#[global_allocator]
static GLOBAL: CountingAlloc = CountingAlloc;
// ─── BEFORE: verbatim copy of the pre-optimization chain ────────────────────
/// Pre-optimization reference implementation. `OldStoragePath` +
/// `normalize_storage_name` + `make_file_path` + the constructor bodies
/// are copied byte-for-byte from the old `path_service.rs` /
/// `file.rs` / `folder.rs` / repository code so the equivalence gate
/// proves the optimized paths change nothing observable.
#[allow(clippy::all)]
mod before {
use unicode_normalization::{IsNormalized, UnicodeNormalization, is_nfc_quick};
use uuid::Uuid;
/// Old borrowing normalize — allocates a copy even on the NFC fast path.
fn normalize_storage_name(name: &str) -> String {
if is_nfc_quick(name.chars()) == IsNormalized::Yes {
return name.to_string();
}
name.nfc().collect()
}
fn validate_storage_name(name: &str) -> Result<(), &'static str> {
if name.is_empty() {
return Err("name cannot be empty");
}
if name.contains('/') || name.contains('\\') {
return Err("name must not contain '/' or '\\'");
}
if name.contains('\0') {
return Err("name must not contain null bytes");
}
if name == "." || name == ".." {
return Err("'.' and '..' are not valid names");
}
Ok(())
}
pub struct OldStoragePath {
pub segments: Vec<String>,
}
impl OldStoragePath {
fn is_safe_segment(s: &str) -> bool {
!s.is_empty() && s != "." && s != ".." && !s.contains('/')
}
fn from_string(path: &str) -> Self {
let segments = path
.split('/')
.filter(|s| Self::is_safe_segment(s))
.map(|s| s.to_string())
.collect();
Self { segments }
}
}
/// Old `Display` impl (join temp) driven through the std `ToString`
/// blanket — the exact `storage_path.to_string()` the constructors ran.
impl std::fmt::Display for OldStoragePath {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if self.segments.is_empty() {
write!(f, "/")
} else {
write!(f, "/{}", self.segments.join("/"))
}
}
}
/// Old repository helper (identical copies lived in the read + write
/// file repositories).
fn make_file_path(folder_path: Option<&str>, file_name: &str) -> OldStoragePath {
match folder_path {
Some(fp) if !fp.is_empty() => OldStoragePath::from_string(&format!("{fp}/{file_name}")),
_ => OldStoragePath::from_string(file_name),
}
}
/// Entity-shaped product so BEFORE pays the same field moves the real
/// constructors pay; only the path/name chain differs from AFTER.
/// Fields exist to be *built* (cost parity), not read.
#[allow(dead_code)]
pub struct BeforeFile {
pub id: String,
pub name: String,
pub storage_path: OldStoragePath,
pub path_string: String,
pub size: u64,
pub mime_type: String,
pub folder_id: Option<String>,
pub created_at: u64,
pub modified_at: u64,
pub blob_hash: String,
pub created_by: Option<Uuid>,
pub updated_by: Option<Uuid>,
}
/// Old `row_to_file` + `File::with_timestamps_blob_hash_and_provenance`.
#[allow(clippy::too_many_arguments)]
pub fn file_row(
id: String,
name: String,
folder_path: Option<&str>,
size: u64,
mime_type: String,
folder_id: Option<String>,
created_at: u64,
modified_at: u64,
blob_hash: String,
created_by: Option<Uuid>,
updated_by: Option<Uuid>,
) -> Result<BeforeFile, String> {
let storage_path = make_file_path(folder_path, &name);
let name = normalize_storage_name(&name);
if let Err(reason) = validate_storage_name(&name) {
return Err(format!("{name}: {reason}"));
}
// Store the path string for serialization compatibility
let path_string = storage_path.to_string();
Ok(BeforeFile {
id,
name,
storage_path,
path_string,
size,
mime_type,
folder_id,
created_at,
modified_at,
blob_hash,
created_by,
updated_by,
})
}
#[allow(dead_code)]
pub struct BeforeFolder {
pub id: String,
pub name: String,
pub storage_path: OldStoragePath,
pub path_string: String,
pub parent_id: Option<String>,
pub drive_id: Uuid,
pub created_at: u64,
pub modified_at: u64,
pub tree_modified_at: u64,
pub created_by: Option<Uuid>,
pub updated_by: Option<Uuid>,
}
/// Old `row_to_folder` + `Folder::with_timestamps_tree_and_provenance`.
#[allow(clippy::too_many_arguments)]
pub fn folder_row(
id: String,
name: String,
path: String,
parent_id: Option<String>,
drive_id: Uuid,
created_at: u64,
modified_at: u64,
tree_modified_at: u64,
created_by: Option<Uuid>,
updated_by: Option<Uuid>,
) -> Result<BeforeFolder, String> {
let storage_path = OldStoragePath::from_string(&path);
let name = normalize_storage_name(&name);
if let Err(reason) = validate_storage_name(&name) {
return Err(format!("{name}: {reason}"));
}
let path_string = storage_path.to_string();
Ok(BeforeFolder {
id,
name,
storage_path,
path_string,
parent_id,
drive_id,
created_at,
modified_at,
tree_modified_at,
created_by,
updated_by,
})
}
}
// ─── Corpus ─────────────────────────────────────────────────────────────────
struct Row {
id: String,
name: String,
folder_path: Option<String>,
mime: String,
}
/// Deterministic LCG so runs are reproducible.
struct Lcg(u64);
impl Lcg {
fn next(&mut self) -> u64 {
self.0 = self
.0
.wrapping_mul(6364136223846793005)
.wrapping_add(1442695040888963407);
self.0 >> 33
}
fn pick<'a>(&mut self, xs: &[&'a str]) -> &'a str {
xs[(self.next() as usize) % xs.len()]
}
}
const SEGMENTS: &[&str] = &[
"Personal",
"Projects",
"2026",
"Q3 Reports",
"Fotos de familia",
"Archive",
"Contabilidad",
"src",
"Diseño gráfico",
"backup-2026-07",
];
const NAMES: &[&str] = &[
"informe-final.pdf",
"IMG_20260714_183042.jpg",
"Presupuesto Q3 2026.xlsx",
"Capture d\u{2019}\u{00E9}cran.png", // NFC accents — the common Unicode case
"notes.md",
"vacaciones-c\u{00F3}rdoba.mp4",
"main.rs",
"espa\u{00F1}ol.txt",
];
fn build_corpus(rows: usize) -> Vec<Row> {
let mut rng = Lcg(0x0c1_f00d);
(0..rows)
.map(|i| {
let depth = (rng.next() % 6) as usize; // 0..=5
let folder_path = if depth == 0 {
None
} else {
let mut p = String::new();
for _ in 0..depth {
p.push('/');
p.push_str(rng.pick(SEGMENTS));
}
Some(p)
};
Row {
id: Uuid::from_u128(i as u128).to_string(),
name: format!("{}-{}", i, rng.pick(NAMES)),
folder_path,
mime: "application/octet-stream".to_string(),
}
})
.collect()
}
fn p50(mut xs: Vec<f64>) -> f64 {
xs.sort_by(|a, b| a.partial_cmp(b).unwrap());
xs[xs.len() / 2]
}
// ─── Runners ────────────────────────────────────────────────────────────────
fn run_file_before(corpus: &[Row]) -> before::BeforeFile {
let mut last = None;
for r in corpus {
let f = before::file_row(
r.id.clone(),
r.name.clone(),
r.folder_path.as_deref(),
1234,
r.mime.clone(),
Some(r.id.clone()),
1_700_000_000,
1_750_000_000,
"aabbccddeeff00112233445566778899".to_string(),
None,
None,
)
.expect("valid row");
last = Some(f);
}
last.unwrap()
}
fn run_file_after(corpus: &[Row]) -> File {
let mut last = None;
for r in corpus {
let f = File::from_materialized_row(
r.id.clone(),
r.name.clone(),
r.folder_path.as_deref(),
1234,
r.mime.clone(),
Some(r.id.clone()),
1_700_000_000,
1_750_000_000,
"aabbccddeeff00112233445566778899".to_string(),
None,
None,
)
.expect("valid row");
last = Some(f);
}
last.unwrap()
}
fn folder_full_path(r: &Row) -> String {
match &r.folder_path {
Some(p) => format!("{}/{}", p, r.name),
None => format!("/{}", r.name),
}
}
fn run_folder_before(corpus: &[Row]) -> before::BeforeFolder {
let mut last = None;
for r in corpus {
let f = before::folder_row(
r.id.clone(),
r.name.clone(),
folder_full_path(r),
Some(r.id.clone()),
Uuid::nil(),
1_700_000_000,
1_750_000_000,
1_750_000_000,
None,
None,
)
.expect("valid row");
last = Some(f);
}
last.unwrap()
}
fn run_folder_after(corpus: &[Row]) -> Folder {
let mut last = None;
for r in corpus {
let f = Folder::from_materialized_row(
r.id.clone(),
r.name.clone(),
folder_full_path(r),
Some(r.id.clone()),
Uuid::nil(),
1_700_000_000,
1_750_000_000,
1_750_000_000,
None,
None,
)
.expect("valid row");
last = Some(f);
}
last.unwrap()
}
fn time_ns_per_row<T>(passes: usize, rows: usize, mut f: impl FnMut() -> T) -> f64 {
let mut per_pass = Vec::with_capacity(passes);
for _ in 0..passes {
let t0 = Instant::now();
black_box(f());
per_pass.push(t0.elapsed().as_nanos() as f64 / rows as f64);
}
p50(per_pass)
}
fn allocs_per_row<T>(rows: usize, mut f: impl FnMut() -> T) -> f64 {
let start = ALLOC_CALLS.load(Ordering::Relaxed);
black_box(f());
(ALLOC_CALLS.load(Ordering::Relaxed) - start) as f64 / rows as f64
}
// ─── Equivalence gate ───────────────────────────────────────────────────────
fn gate_file(name: &str, folder_path: Option<&str>) -> bool {
let b = before::file_row(
"id".into(),
name.to_string(),
folder_path,
0,
"m".into(),
None,
0,
0,
String::new(),
None,
None,
);
let a = File::from_materialized_row(
"id".into(),
name.to_string(),
folder_path,
0,
"m".into(),
None,
0,
0,
String::new(),
None,
None,
);
match (b, a) {
(Ok(b), Ok(a)) => {
let seg_a: Vec<String> = a.storage_path().segments().to_vec();
if b.name != a.name()
|| b.path_string != a.path_string()
|| b.storage_path.segments != seg_a
{
eprintln!(
"GATE FAIL file name={name:?} fp={folder_path:?}\n BEFORE name={:?} path={:?} segs={:?}\n AFTER name={:?} path={:?} segs={:?}",
b.name,
b.path_string,
b.storage_path.segments,
a.name(),
a.path_string(),
seg_a
);
return false;
}
true
}
(Err(_), Err(_)) => true, // error parity
(b, a) => {
eprintln!(
"GATE FAIL file name={name:?} fp={folder_path:?}: error parity broke (before_ok={} after_ok={})",
b.is_ok(),
a.is_ok()
);
false
}
}
}
fn gate_folder(name: &str, path: &str) -> bool {
let b = before::folder_row(
"id".into(),
name.to_string(),
path.to_string(),
None,
Uuid::nil(),
0,
0,
0,
None,
None,
);
let a = Folder::from_materialized_row(
"id".into(),
name.to_string(),
path.to_string(),
None,
Uuid::nil(),
0,
0,
0,
None,
None,
);
match (b, a) {
(Ok(b), Ok(a)) => {
let seg_a: Vec<String> = a.storage_path().segments().to_vec();
if b.name != a.name()
|| b.path_string != a.path_string()
|| b.storage_path.segments != seg_a
{
eprintln!(
"GATE FAIL folder name={name:?} path={path:?}\n BEFORE name={:?} path={:?} segs={:?}\n AFTER name={:?} path={:?} segs={:?}",
b.name,
b.path_string,
b.storage_path.segments,
a.name(),
a.path_string(),
seg_a
);
return false;
}
true
}
(Err(_), Err(_)) => true,
(b, a) => {
eprintln!(
"GATE FAIL folder name={name:?} path={path:?}: error parity broke (before_ok={} after_ok={})",
b.is_ok(),
a.is_ok()
);
false
}
}
}
fn main() {
let rows: usize = env::var("BENCH_ROWS")
.ok()
.and_then(|v| v.parse().ok())
.unwrap_or(10_000);
let passes: usize = env::var("BENCH_PASSES")
.ok()
.and_then(|v| v.parse().ok())
.unwrap_or(100);
let corpus = build_corpus(rows);
println!("bench_row_path — {rows} rows, {passes} passes (p50 ns/row)");
println!();
// Warm-up
black_box(run_file_before(&corpus));
black_box(run_file_after(&corpus));
black_box(run_folder_before(&corpus));
black_box(run_folder_after(&corpus));
// [1] file rows
let f_before = time_ns_per_row(passes, rows, || run_file_before(&corpus));
let f_after = time_ns_per_row(passes, rows, || run_file_after(&corpus));
println!("[1] File row (path chain + entity build)");
println!(" BEFORE {f_before:8.1} ns/row");
println!(
" AFTER {f_after:8.1} ns/row {:.2}x",
f_before / f_after
);
// [2] folder rows
let d_before = time_ns_per_row(passes, rows, || run_folder_before(&corpus));
let d_after = time_ns_per_row(passes, rows, || run_folder_after(&corpus));
println!("[2] Folder row (path chain + entity build)");
println!(" BEFORE {d_before:8.1} ns/row");
println!(
" AFTER {d_after:8.1} ns/row {:.2}x",
d_before / d_after
);
// [3] allocs/row
let fa_before = allocs_per_row(rows, || run_file_before(&corpus));
let fa_after = allocs_per_row(rows, || run_file_after(&corpus));
let da_before = allocs_per_row(rows, || run_folder_before(&corpus));
let da_after = allocs_per_row(rows, || run_folder_after(&corpus));
println!("[3] Alloc calls/row");
println!(" File BEFORE {fa_before:6.2} AFTER {fa_after:6.2}");
println!(" Folder BEFORE {da_before:6.2} AFTER {da_after:6.2}");
// [4] equivalence gate — realistic corpus + adversarial inputs
let mut ok = true;
for r in &corpus {
ok &= gate_file(&r.name, r.folder_path.as_deref());
ok &= gate_folder(&r.name, &folder_full_path(r));
}
// Adversarial: non-canonical paths, traversal, NFD names, empties.
let adversarial_files: &[(&str, Option<&str>)] = &[
("file.txt", None),
("file.txt", Some("")),
("file.txt", Some("/")),
("file.txt", Some("a//b")),
("file.txt", Some("/a/b/")),
("file.txt", Some("../etc")),
("file.txt", Some("a/./b")),
("file.txt", Some("//")),
// NFD name (decomposed é): DB rows are NFC by invariant, but the
// chain must stay byte-identical even for un-normalized input.
("cafe\u{0301}.txt", Some("/a")),
("", Some("/a")), // error parity
("..", Some("/a")), // error parity
("nul\0l.txt", Some("/a")), // error parity
("a\\b.txt", Some("/a")), // error parity
];
for (n, fp) in adversarial_files {
ok &= gate_file(n, *fp);
}
let adversarial_folders: &[(&str, &str)] = &[
("Docs", "/Docs"),
("Docs", "Docs"),
("Docs", "/a//Docs"),
("Docs", "/a/Docs/"),
("Docs", "/"),
("Docs", ""),
("Docs", "/../Docs"),
("Doc\u{0301}s", "/a/Doc\u{0301}s"), // NFD in both
];
for (n, p) in adversarial_folders {
ok &= gate_folder(n, p);
}
println!(
"[4] Equivalence gate: {}",
if ok { "OK (byte-identical)" } else { "FAILED" }
);
if !ok {
std::process::exit(1);
}
}