Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/synapse/blob/main/crates/synapse-bench/tests/c1_tree_cache_bound.rs # synapse A component of the AienOS ecosystem. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `synapse / crates / synapse-bench / tests / c1_tree_cache_bound.rs` 189 lines ยท 7432 bytes Raw Blame History 1 //! C1 acceptance (ADR 0003 section 9 step 4): the shared Merkle tree cache stays under its 2 //! configured bound under the Gate 4 query load, and cold (build) and warm (paging) queries 3 //! are distinguishable. 4 //! 5 //! The load has the Gate 4 query cell's shape (`crates/synapse-bench/src/query.rs`): a 6 //! population of 8 agents x 50 proposals, then 400 samples each of `tree` (every page), 7 //! `diff` (base to changeset), and `read_file` on random changesets, from one reader. The 8 //! bound is set to the minimum so the population does not fit and the store must evict and 9 //! rebuild; every answer is still compared per entry with Git. 10 11 use synapse_api::{TreeCacheConfig, TreeTarget, MIN_TREE_CACHE_BYTES}; 12 use synapse_bench::fixture; 13 use synapse_bench::rng::SplitMix64; 14 use synapse_bench::synapse_arm; 15 use synapse_bench::tree_parity::{first_mismatch, synthetic_fixture, temp_dir, ParityPair}; 16 use synapse_bench::workload::{generate_write_workload, EditOp, Proposal}; 17 use synapse_core::ChangesetId; 18 19 const AGENTS: usize = 8; 20 const OPS_PER_AGENT: usize = 50; 21 const SAMPLES: usize = 400; 22 23 struct Populated { 24 root: std::path::PathBuf, 25 pair: ParityPair, 26 /// (changeset, Git commit, proposal) in submission order. 27 props: Vec<(ChangesetId, String, Proposal)>, 28 } 29 30 fn populate(label: &str) -> Populated { 31 let root = temp_dir(label); 32 let base = synthetic_fixture(&root, 96, 24).unwrap(); 33 let clone = root.join("fixture"); 34 let mut pair = ParityPair::open(&clone, &base, &root.join("work"), 0xC1B, AGENTS).unwrap(); 35 let listed = fixture::list_base_tree(&clone, &base).unwrap(); 36 let full = fixture::load_full_base(&clone, &listed).unwrap(); 37 let files = fixture::editable_from_full_base(&listed, &full); 38 let workload = generate_write_workload(0xC1B, 10_000, AGENTS, OPS_PER_AGENT, &files); 39 let mut props = Vec::new(); 40 for p in workload.iter().flatten() { 41 let (cs, commit) = pair.apply(p).unwrap(); 42 props.push((cs, commit, p.clone())); 43 } 44 Populated { root, pair, props } 45 } 46 47 fn assert_under_bound(pair: &ParityPair, max: usize, when: &str) { 48 let s = pair.svc.tree_store().stats(); 49 assert!( 50 s.total_bytes <= max, 51 "{when}: tree cache holds {} accounted bytes, over the {} byte bound", 52 s.total_bytes, 53 max 54 ); 55 } 56 57 #[test] 58 fn c1_memory_stays_under_the_configured_bound_under_the_gate4_query_load() { 59 let Populated { 60 root, 61 mut pair, 62 props, 63 } = populate("bound"); 64 let max = MIN_TREE_CACHE_BYTES; 65 pair.svc 66 .set_tree_cache_config(TreeCacheConfig { max_bytes: max }) 67 .unwrap(); 68 let n = props.len(); 69 assert_eq!(n, AGENTS * OPS_PER_AGENT); 70 let mut rng = SplitMix64::derive(0xC1B, &[0x0E_E7]); 71 let mut reads = 0usize; 72 for s in 0..SAMPLES { 73 // tree: every page, compared per entry with `git ls-tree -r`. 74 let i = rng.below(n); 75 let (git, syn) = pair 76 .tree_pair(TreeTarget::Changeset(props[i].0), &props[i].1) 77 .unwrap(); 78 assert_eq!(first_mismatch(&git, &syn), None, "tree sample {s}"); 79 assert_under_bound(&pair, max, "after tree"); 80 // diff: base to changeset, compared per entry with `git diff-tree -r --raw`. 81 let i = rng.below(n); 82 let (git, syn) = pair 83 .diff_pair(TreeTarget::Changeset(props[i].0), &props[i].1) 84 .unwrap(); 85 assert_eq!(first_mismatch(&git, &syn), None, "diff sample {s}"); 86 assert_under_bound(&pair, max, "after diff"); 87 // read_file: an upserted file of a random changeset returns the proposal's bytes. 88 let i = rng.below(n); 89 let p = &props[i].2; 90 if let Some((path, content)) = p.edits.iter().find_map(|e| match &e.op { 91 EditOp::Upsert { content, .. } => Some((e.path.clone(), content.clone())), 92 EditOp::Delete => None, 93 }) { 94 let now = synapse_bench::unix_now_ms(); 95 let req = synapse_arm::build_read_request( 96 &pair.ids.agents[0], 97 &pair.ids.repo_id, 98 (TreeTarget::Changeset(props[i].0), path, None), 99 &format!("c1/bound/read/{s}"), 100 now, 101 ) 102 .unwrap(); 103 let bytes = pair.svc.read_file(&req, now).unwrap(); 104 assert_eq!(bytes.as_slice(), content.as_slice(), "read_file sample {s}"); 105 reads += 1; 106 assert_under_bound(&pair, max, "after read_file"); 107 } 108 } 109 let s = pair.svc.tree_store().stats(); 110 println!("c1 bound test stats after the query load: {s:?}"); 111 assert!(reads > 0); 112 assert!( 113 s.high_water_bytes <= max, 114 "high water {} > bound {max}", 115 s.high_water_bytes 116 ); 117 assert!( 118 s.nodes_evicted > 0 && s.roots_evicted > 0, 119 "the load must not fit in the bound, or the test proves nothing: {s:?}" 120 ); 121 assert!(s.memoized_roots < n, "every root still memoized: {s:?}"); 122 let _ = std::fs::remove_dir_all(&root); 123 } 124 125 #[test] 126 fn c1_cold_build_then_warm_paging_with_structural_sharing() { 127 let Populated { 128 root, 129 mut pair, 130 props, 131 } = populate("cold-warm"); 132 // Fresh store: no changeset root is memoized. 133 pair.svc 134 .set_tree_cache_config(TreeCacheConfig::default()) 135 .unwrap(); 136 let base_root = pair 137 .svc 138 .tree_digest(TreeTarget::BaseGitCommit(pair.base_oid), None) 139 .unwrap(); 140 for i in [0usize, props.len() / 2, props.len() - 1] { 141 let target = TreeTarget::Changeset(props[i].0); 142 let before = pair.svc.tree_store().stats(); 143 let root_digest = pair.svc.tree_digest(target, None).unwrap(); 144 let built = pair.svc.tree_store().stats(); 145 assert_eq!( 146 built.root_builds, 147 before.root_builds + 1, 148 "first query is cold" 149 ); 150 assert_ne!( 151 root_digest, base_root, 152 "a changeset with edits has its own root" 153 ); 154 // Structural sharing: a cold build writes only the nodes on the edited paths. 155 let p = &props[i].2; 156 let depth = p 157 .edits 158 .iter() 159 .map(|e| e.path.matches('/').count() + 1) 160 .max() 161 .unwrap(); 162 let new_nodes = built.nodes_inserted - before.nodes_inserted; 163 assert!( 164 new_nodes as usize <= depth * p.edits.len(), 165 "cold build inserted {new_nodes} nodes for {} edits of depth <= {depth}", 166 p.edits.len() 167 ); 168 // Warm: the same root again, then a full paged listing, build nothing. 169 assert_eq!(pair.svc.tree_digest(target, None).unwrap(), root_digest); 170 let (total, first) = pair.svc.tree_page(target, "", 0, 256, None).unwrap(); 171 let mut seen = first.len() as u64; 172 while seen < total { 173 let (_, page) = pair.svc.tree_page(target, "", seen, 256, None).unwrap(); 174 assert!(!page.is_empty()); 175 seen += page.len() as u64; 176 } 177 let warm = pair.svc.tree_store().stats(); 178 assert_eq!( 179 warm.root_builds, built.root_builds, 180 "warm queries build nothing" 181 ); 182 assert!( 183 warm.root_hits >= built.root_hits + 2, 184 "warm queries hit the memo" 185 ); 186 assert_eq!(warm.nodes_inserted, built.nodes_inserted); 187 } 188 let _ = std::fs::remove_dir_all(&root); 189 }