fdf445d2b0
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
219 lines
6.9 KiB
Rust
219 lines
6.9 KiB
Rust
//! `Drive::is_empty` benchmark — full-drive `COUNT(*)` sum vs short-circuit
|
|
//! `EXISTS OR EXISTS`.
|
|
//!
|
|
//! The drive-deletion precheck only needs a boolean, but the old query
|
|
//! aggregated every live folder AND file in the drive (two full index/heap
|
|
//! scans) to compare the sum with 0. `EXISTS` stops at the first matching
|
|
//! row, so a populated drive answers from one probe.
|
|
//!
|
|
//! Both query shapes run against the same seeded data; the equivalence
|
|
//! gate asserts identical booleans for a populated and an empty drive.
|
|
//!
|
|
//! Run (needs Postgres up; reads DATABASE_URL from .env):
|
|
//! cargo run --release --features bench --example bench_drive_is_empty
|
|
//! Tunables (env): BENCH_FILES (100000), BENCH_REPS (25)
|
|
|
|
use std::env;
|
|
use std::time::Instant;
|
|
|
|
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)
|
|
}
|
|
|
|
async fn seed_drive(pool: &PgPool, files: usize) -> Uuid {
|
|
// Drive + root folder must commit together (deferred root-folder trigger).
|
|
let mut tx = pool.begin().await.expect("begin");
|
|
let drive_id: Uuid = sqlx::query_scalar(
|
|
"INSERT INTO storage.drives (kind, quota_bytes) VALUES ('shared', NULL) RETURNING id",
|
|
)
|
|
.fetch_one(&mut *tx)
|
|
.await
|
|
.expect("drive");
|
|
let root: Uuid = sqlx::query_scalar(
|
|
"INSERT INTO storage.folders (name, path, lpath, drive_id)
|
|
VALUES ('bench_is_empty', '/bench_is_empty', 'bench_is_empty', $1)
|
|
RETURNING id",
|
|
)
|
|
.bind(drive_id)
|
|
.fetch_one(&mut *tx)
|
|
.await
|
|
.expect("root");
|
|
sqlx::query("UPDATE storage.drives SET root_folder_id = $1 WHERE id = $2")
|
|
.bind(root)
|
|
.bind(drive_id)
|
|
.execute(&mut *tx)
|
|
.await
|
|
.expect("stamp root");
|
|
tx.commit().await.expect("commit");
|
|
|
|
if files > 0 {
|
|
sqlx::query(
|
|
"INSERT INTO storage.files (name, folder_id, blob_hash, size, mime_type, drive_id)
|
|
SELECT 'f' || i, $1,
|
|
'benchempty00000000000000000000000000000000000000000000000000000',
|
|
1024, 'image/jpeg', $2
|
|
FROM generate_series(1, $3) AS i",
|
|
)
|
|
.bind(root)
|
|
.bind(drive_id)
|
|
.bind(files as i32)
|
|
.execute(pool)
|
|
.await
|
|
.expect("seed files");
|
|
}
|
|
drive_id
|
|
}
|
|
|
|
async fn cleanup(pool: &PgPool, drive_id: Uuid) {
|
|
sqlx::query("DELETE FROM storage.files WHERE drive_id = $1")
|
|
.bind(drive_id)
|
|
.execute(pool)
|
|
.await
|
|
.ok();
|
|
sqlx::query("UPDATE storage.drives SET root_folder_id = NULL WHERE id = $1")
|
|
.bind(drive_id)
|
|
.execute(pool)
|
|
.await
|
|
.ok();
|
|
sqlx::query("DELETE FROM storage.folders WHERE drive_id = $1")
|
|
.bind(drive_id)
|
|
.execute(pool)
|
|
.await
|
|
.ok();
|
|
sqlx::query("DELETE FROM storage.drives WHERE id = $1")
|
|
.bind(drive_id)
|
|
.execute(pool)
|
|
.await
|
|
.ok();
|
|
}
|
|
|
|
/// BEFORE — verbatim old query shape.
|
|
async fn is_empty_count(pool: &PgPool, drive_id: Uuid) -> bool {
|
|
let count: (i64,) = sqlx::query_as(
|
|
r#"
|
|
SELECT (
|
|
(SELECT COUNT(*) FROM storage.folders
|
|
WHERE drive_id = $1 AND parent_id IS NOT NULL AND NOT is_trashed)
|
|
+ (SELECT COUNT(*) FROM storage.files
|
|
WHERE drive_id = $1 AND NOT is_trashed)
|
|
)
|
|
"#,
|
|
)
|
|
.bind(drive_id)
|
|
.fetch_one(pool)
|
|
.await
|
|
.expect("count query");
|
|
count.0 == 0
|
|
}
|
|
|
|
/// AFTER — the production EXISTS shape.
|
|
async fn is_empty_exists(pool: &PgPool, drive_id: Uuid) -> bool {
|
|
let occupied: (bool,) = sqlx::query_as(
|
|
r#"
|
|
SELECT EXISTS(
|
|
SELECT 1 FROM storage.folders
|
|
WHERE drive_id = $1 AND parent_id IS NOT NULL AND NOT is_trashed)
|
|
OR EXISTS(
|
|
SELECT 1 FROM storage.files
|
|
WHERE drive_id = $1 AND NOT is_trashed)
|
|
"#,
|
|
)
|
|
.bind(drive_id)
|
|
.fetch_one(pool)
|
|
.await
|
|
.expect("exists query");
|
|
!occupied.0
|
|
}
|
|
|
|
#[tokio::main(flavor = "current_thread")]
|
|
async fn main() {
|
|
dotenvy::dotenv().ok();
|
|
let url = env::var("DATABASE_URL").expect("set DATABASE_URL — the dev Postgres URL");
|
|
let pool = PgPoolOptions::new()
|
|
.max_connections(4)
|
|
.connect(&url)
|
|
.await
|
|
.expect("connect");
|
|
|
|
let files: usize = env_or("BENCH_FILES", 100_000);
|
|
let reps: usize = env_or("BENCH_REPS", 25);
|
|
|
|
let populated = seed_drive(&pool, files).await;
|
|
let empty = seed_drive(&pool, 0).await;
|
|
|
|
// Equivalence gate on both data shapes.
|
|
assert_eq!(
|
|
is_empty_count(&pool, populated).await,
|
|
is_empty_exists(&pool, populated).await,
|
|
"populated drive verdict differs"
|
|
);
|
|
assert_eq!(
|
|
is_empty_count(&pool, empty).await,
|
|
is_empty_exists(&pool, empty).await,
|
|
"empty drive verdict differs"
|
|
);
|
|
assert!(!is_empty_exists(&pool, populated).await);
|
|
assert!(is_empty_exists(&pool, empty).await);
|
|
println!("# equivalence gate: identical booleans on populated + empty drives — OK");
|
|
|
|
// Warm both shapes.
|
|
for _ in 0..3 {
|
|
is_empty_count(&pool, populated).await;
|
|
is_empty_exists(&pool, populated).await;
|
|
}
|
|
|
|
let mut rows = Vec::new();
|
|
for (label, drive) in [("populated (100k files)", populated), ("empty", empty)] {
|
|
let t = Instant::now();
|
|
for _ in 0..reps {
|
|
std::hint::black_box(is_empty_count(&pool, drive).await);
|
|
}
|
|
let before_ms = t.elapsed().as_secs_f64() * 1e3 / reps as f64;
|
|
|
|
let t = Instant::now();
|
|
for _ in 0..reps {
|
|
std::hint::black_box(is_empty_exists(&pool, drive).await);
|
|
}
|
|
let after_ms = t.elapsed().as_secs_f64() * 1e3 / reps as f64;
|
|
rows.push((label, before_ms, after_ms));
|
|
}
|
|
|
|
println!("\n#################################################################");
|
|
println!("# Drive::is_empty — COUNT(*) sum vs EXISTS OR EXISTS");
|
|
println!("# files={files} reps={reps} (ms per call)");
|
|
println!("#################################################################\n");
|
|
println!(
|
|
"| {:<24} | {:>14} | {:>14} | {:>8} |",
|
|
"drive", "BEFORE ms", "AFTER ms", "speedup"
|
|
);
|
|
let mut populated_gain = 0.0;
|
|
for (label, before_ms, after_ms) in &rows {
|
|
println!(
|
|
"| {:<24} | {:>14.3} | {:>14.3} | {:>7.1}x |",
|
|
label,
|
|
before_ms,
|
|
after_ms,
|
|
before_ms / after_ms
|
|
);
|
|
if label.starts_with("populated") {
|
|
populated_gain = before_ms / after_ms;
|
|
}
|
|
}
|
|
|
|
cleanup(&pool, populated).await;
|
|
cleanup(&pool, empty).await;
|
|
|
|
if populated_gain <= 1.0 {
|
|
eprintln!("\nGATE FAIL: EXISTS not faster on the populated drive — rollback");
|
|
std::process::exit(1);
|
|
}
|
|
println!("\nGATE PASS: identical verdicts, populated drive {populated_gain:.1}x faster.");
|
|
}
|