Files
Oxicloud/examples/bench_capabilities_static.rs
T
Claude fdf445d2b0 perf: round 9 — decorator PUT reactivation, session/search/dedup alloc purges, PROPFIND join!, folder-level cascade
Benchmark-gated round (benches/ROUND9.md): every change carries a
BEFORE/AFTER bench with equivalence/safety gates; verdicts below are from
the committed harnesses on 4 cores / local PG 16.

Backend:
- Blob decorators (Retry/Cached) now forward put_blob_from_bytes_unsynced
  + sync_blobs — the trait default had silently reinstated HEAD-before-PUT
  per chunk on decorated remote stacks, undoing ROUND3 §8. Full production
  stack: 500 probes -> 0, 1.9x wall at 10 ms RTT (bench_s3_put §3).
- NC PROPFIND per-page enrichment triple (favorites / oc:fileid / dead
  props) overlapped with tokio::join!: 2.07x local, 2.86x at 5 ms RTT
  (bench_nc_enrich_join, injected-latency decide-by-bench).
- Search enrichment consumes its DTOs and carries the interned Arc<str>
  display fields end-to-end (SearchFileResultDto type change, OpenAPI
  shape preserved): enrich_file 2.0x, 11.6 -> 2.2 allocs/row; the NC
  REPORT conversion stops re-running all three classifiers per row
  (bench_search_enrich).
- NC session Arc end-to-end: SharedNcSession extractor (8 -> 0 allocs),
  Arc<FolderDto> chroot cache (4 -> 0/hit), single shared Arc<CurrentUser>
  + lazy span render (11 -> 6/build) (bench_nc_session).
- Storage micro-pack: atomic create_new chunk writes (2.1x fresh),
  stream_chunks over the manifest Arc (4097 -> 0 allocs/read incl. the
  Range path), manifest single-flight (herd 64 -> 1 loads), hex_lower for
  chunk Content-MD5 (18 -> 1 allocs) (bench_storage_micro).
- OCS capabilities memoized into OnceLock<[Bytes;2]>: 237x, 102 -> 0
  allocs/poll, byte-identical (bench_capabilities_static).
- Drive::is_empty COUNT(*) sum -> EXISTS: 34.4x on a 100k-file drive
  (bench_drive_is_empty).
- favorites/recents row-map ROUND7 port: path/name/blob_hash moved,
  -2.75 allocs/row (bench_resource_row_map §2).
- Folder rows decode binary UUIDs (ROUND6 §10 port): 1.03-1.07x page
  fetch, honest verdict incl. one noise-band wash documented
  (bench_folder_uuid_decode).
- Authz: file cascade decision decomposed into memoized folder-level
  decision + direct-grant lookup (ROUND8 deferred item). Cold shared-album
  first view 592 -> 418 µs/thumb; warm path unchanged; safety gates incl.
  new direct-grant sibling isolation, revoke-flush re-verified, full
  integration authz suite green (bench_thumbnail_cascade_cache).

Frontend (vitest gates committed beside the code):
- resolveLabel/resolveRecipient O(directory) scan -> id-keyed Map: 13.9x
  (recipients.bench.test.ts).
- ResourceList selection-prune effect skips when nothing is selected
  (100 -> 0 Set builds per drain) and the photos timeline reads a
  listener-fed mobile flag instead of matchMedia per recompute
  (listDerives.bench.test.ts).

Verification: cargo fmt + clippy --all-features --all-targets -D warnings
clean; 524 unit + 554 integration (--cfg integration_tests) tests pass;
frontend npm run check clean with 293 vitest tests green.

Deferred with rationale in ROUND9.md: CalDAV authz-before-fetch reorder
(maintainer sign-off), per-page batched parent resolution, JWT-claims
Arc<str>, batch_operations signature widening.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XDc9VtXvskJ6dnMRraSndn
2026-07-18 16:12:04 +00:00

158 lines
5.6 KiB
Rust

//! OCS capabilities poll benchmark — rebuild-per-request vs memoized bytes.
//!
//! `/ocs/v{1,2}.php/cloud/capabilities` returns a payload that is
//! process-invariant (pure config: base URL + emulated NC version), yet
//! every NC desktop/mobile client polls it on connect and periodically.
//! The old handler re-built the ~40-node `json!` tree — including a
//! `std::env::var("OXICLOUD_BASE_URL")` lookup and three `format!`s —
//! and re-serialized it on EVERY poll. Round 9 serializes both versions
//! once into a `OnceLock<[Bytes; 2]>`; a poll is a `Bytes` refcount bump.
//!
//! The BEFORE arm is the production payload builder invoked per request
//! (via the bench wrapper) + `serde_json::to_vec`, exactly the old
//! handler flow (`Json(payload)` serializes with `to_vec`). The AFTER
//! arm is the memoized-bytes flow. The equivalence gate asserts the
//! served bytes are identical.
//!
//! Run (no Postgres needed):
//! cargo run --release --features bench --example bench_capabilities_static
//! Tunables (env): BENCH_POLLS (50000)
use std::alloc::{GlobalAlloc, Layout, System};
use std::env;
use std::hint::black_box;
use std::sync::OnceLock;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::Instant;
use bytes::Bytes;
use oxicloud::interfaces::nextcloud::ocs_handler::capabilities_payload_for_bench;
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;
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)
}
const EMULATED: (u32, u32, u32) = (28, 0, 4);
const VERSION_STRING: &str = "28.0.4";
/// BEFORE flow, verbatim shape: env lookup + tree build + serialize per poll.
fn before_poll(ocs_version: u8) -> Vec<u8> {
let base_url =
env::var("OXICLOUD_BASE_URL").unwrap_or_else(|_| "http://localhost:8086".to_string());
let payload = capabilities_payload_for_bench(&base_url, EMULATED, VERSION_STRING, ocs_version);
serde_json::to_vec(&payload).expect("serialize")
}
/// AFTER flow: the production memoization shape (OnceLock + Bytes clone).
fn after_poll(cache: &OnceLock<[Bytes; 2]>, ocs_version: u8) -> Bytes {
let bodies = cache.get_or_init(|| {
let base_url =
env::var("OXICLOUD_BASE_URL").unwrap_or_else(|_| "http://localhost:8086".to_string());
[1u8, 2u8].map(|v| {
Bytes::from(
serde_json::to_vec(&capabilities_payload_for_bench(
&base_url,
EMULATED,
VERSION_STRING,
v,
))
.expect("serialize"),
)
})
});
bodies[usize::from(ocs_version != 1)].clone()
}
fn main() {
let polls: usize = env_or("BENCH_POLLS", 50_000);
let cache: OnceLock<[Bytes; 2]> = OnceLock::new();
// Equivalence gate: identical served bytes for both OCS versions.
for v in [1u8, 2u8] {
assert_eq!(
before_poll(v),
after_poll(&cache, v).as_ref(),
"capabilities v{v} bytes differ"
);
}
println!("# equivalence gate: v1 + v2 served bytes identical — OK");
let a0 = ALLOC_CALLS.load(Ordering::Relaxed);
let t = Instant::now();
for i in 0..polls {
black_box(before_poll(if i % 2 == 0 { 1 } else { 2 }));
}
let before_ms = t.elapsed().as_secs_f64() * 1e3;
let before_allocs = ALLOC_CALLS.load(Ordering::Relaxed) - a0;
let a1 = ALLOC_CALLS.load(Ordering::Relaxed);
let t = Instant::now();
for i in 0..polls {
black_box(after_poll(&cache, if i % 2 == 0 { 1 } else { 2 }));
}
let after_ms = t.elapsed().as_secs_f64() * 1e3;
let after_allocs = ALLOC_CALLS.load(Ordering::Relaxed) - a1;
println!("\n#################################################################");
println!("# OCS capabilities poll — rebuild+serialize vs memoized Bytes");
println!("# polls={polls}");
println!("#################################################################\n");
println!(
"| {:<26} | {:>10} | {:>12} | {:>12} |",
"arm", "wall ms", "allocs", "allocs/poll"
);
println!(
"| {:<26} | {:>10.1} | {:>12} | {:>12.2} |",
"BEFORE (rebuild)",
before_ms,
before_allocs,
before_allocs as f64 / polls as f64
);
println!(
"| {:<26} | {:>10.1} | {:>12} | {:>12.2} |",
"AFTER (memoized)",
after_ms,
after_allocs,
after_allocs as f64 / polls as f64
);
println!(
"\n{:.1}x faster, {:.0}x fewer allocs",
before_ms / after_ms,
before_allocs as f64 / after_allocs.max(1) as f64
);
if after_ms >= before_ms || after_allocs >= before_allocs {
eprintln!("GATE FAIL: memoized arm not strictly better — rollback");
std::process::exit(1);
}
println!("GATE PASS");
}