|
| 1 | +//! Statements: input digests over a large snapshot, and the promotion |
| 2 | +//! search over many statements, with their digests cached by the index and |
| 3 | +//! without. |
| 4 | +
|
| 5 | +use std::fs; |
| 6 | + |
| 7 | +use divan::Bencher; |
| 8 | +use divan::counter::ItemsCount; |
| 9 | +use origin_platform::Native; |
| 10 | +use origin_shared::{ |
| 11 | + Base, ChangeId, Digest, EncodedObject, EntryKind, EntryName, Inputs, Kind, ObjectId, Principal, |
| 12 | + Record, RecordId, RepoPath, Revision, RevisionId, Statement, Tree, TreeEntry, TreeId, Verdict, |
| 13 | +}; |
| 14 | +use origin_src::annotations::Annotations; |
| 15 | +use origin_src::fixture::FixtureTree; |
| 16 | +use origin_src::index::Index; |
| 17 | +use origin_src::store::Store; |
| 18 | +use origin_src::tree::Snapshots; |
| 19 | +use origin_src::worktree::{StatCache, Worktree}; |
| 20 | + |
| 21 | +fn main() { |
| 22 | + divan::main(); |
| 23 | +} |
| 24 | + |
| 25 | +/// An initialized store in a new temporary directory. |
| 26 | +fn store() -> (tempfile::TempDir, Store) { |
| 27 | + let dir = tempfile::tempdir().unwrap(); |
| 28 | + let store = Store::new(dir.path().join(".origin"), Native::default()); |
| 29 | + store.init().unwrap(); |
| 30 | + (dir, store) |
| 31 | +} |
| 32 | + |
| 33 | +fn inputs(paths: &[&str], definition: &str) -> Inputs { |
| 34 | + Inputs { |
| 35 | + paths: paths |
| 36 | + .iter() |
| 37 | + .map(|path| RepoPath::new(path).unwrap()) |
| 38 | + .collect(), |
| 39 | + definition: Digest::of(definition.as_bytes()), |
| 40 | + toolchain: Digest::of(b"rustc 1.99.0"), |
| 41 | + external: false, |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +/// The input digest of the 20k-file fixture, for inputs naming two |
| 46 | +/// directories, a file and a missing path. Each run reads the trees anew. |
| 47 | +#[divan::bench(sample_count = 50)] |
| 48 | +fn input_digest(bencher: Bencher<'_, '_>) { |
| 49 | + let dir = tempfile::tempdir().unwrap(); |
| 50 | + FixtureTree::LARGE.write(dir.path()).unwrap(); |
| 51 | + let store = Store::new(dir.path().join(".origin"), Native::default()); |
| 52 | + store.init().unwrap(); |
| 53 | + let (snapshot, _) = Worktree::new(dir.path(), &store) |
| 54 | + .snapshot(&StatCache::default()) |
| 55 | + .unwrap(); |
| 56 | + let inputs = inputs( |
| 57 | + &["dir0", "dir5/sub3", "dir12/sub7/file7820.rs", "missing"], |
| 58 | + "cargo test", |
| 59 | + ); |
| 60 | + bencher.bench(|| { |
| 61 | + Snapshots::new(&store) |
| 62 | + .input_digest(snapshot, &inputs) |
| 63 | + .unwrap() |
| 64 | + }); |
| 65 | +} |
| 66 | + |
| 67 | +/// How many statements the promotion search runs over. |
| 68 | +const STATEMENTS: usize = 20_000; |
| 69 | + |
| 70 | +/// The checks each revision has results of, with the inputs they declare. |
| 71 | +const CHECKS: [(&str, &[&str]); 10] = [ |
| 72 | + ("ci.lint", &["src", "Cargo.toml"]), |
| 73 | + ("ci.build", &["src", "Cargo.toml"]), |
| 74 | + ("ci.test", &["src", "tests", "Cargo.toml"]), |
| 75 | + ("ci.docs", &["src", "docs"]), |
| 76 | + ("ci.links", &["docs"]), |
| 77 | + ("ci.fmt", &["src", "tests"]), |
| 78 | + ("ci.bench", &["src", "benches", "Cargo.toml"]), |
| 79 | + ("ci.audit", &["Cargo.toml"]), |
| 80 | + ("ci.spell", &["docs", "README.md"]), |
| 81 | + ("ci.unit", &["src"]), |
| 82 | +]; |
| 83 | + |
| 84 | +/// A snapshot of a small crate, at version `version` of its sources and |
| 85 | +/// `docs` of its documentation. |
| 86 | +fn snapshot(version: usize, docs: usize) -> Vec<EncodedObject> { |
| 87 | + let file = |content: String| { |
| 88 | + let blob = EncodedObject::blob(content.as_bytes()); |
| 89 | + let entry = TreeEntry { |
| 90 | + kind: EntryKind::File, |
| 91 | + id: blob.id(), |
| 92 | + }; |
| 93 | + (blob, entry) |
| 94 | + }; |
| 95 | + let mut objects = Vec::new(); |
| 96 | + let mut root = Vec::new(); |
| 97 | + for (dir, content) in [ |
| 98 | + ("src", format!("fn v{version}() {{}}")), |
| 99 | + ("tests", format!("#[test] fn t{}() {{}}", version / 2)), |
| 100 | + ("benches", "fn bench() {}".to_owned()), |
| 101 | + ("docs", format!("# Guide {docs}")), |
| 102 | + ] { |
| 103 | + let (blob, entry) = file(content); |
| 104 | + let tree: Tree = [(EntryName::new("a").unwrap(), entry)] |
| 105 | + .into_iter() |
| 106 | + .collect(); |
| 107 | + let tree = tree.encode(); |
| 108 | + root.push(( |
| 109 | + EntryName::new(dir).unwrap(), |
| 110 | + TreeEntry { |
| 111 | + kind: EntryKind::Tree, |
| 112 | + id: tree.id(), |
| 113 | + }, |
| 114 | + )); |
| 115 | + objects.extend([blob, tree]); |
| 116 | + } |
| 117 | + for (name, content) in [("Cargo.toml", "[package]"), ("README.md", "# Readme")] { |
| 118 | + let (blob, entry) = file(content.to_owned()); |
| 119 | + root.push((EntryName::new(name).unwrap(), entry)); |
| 120 | + objects.push(blob); |
| 121 | + } |
| 122 | + objects.push(root.into_iter().collect::<Tree>().encode()); |
| 123 | + objects |
| 124 | +} |
| 125 | + |
| 126 | +/// A store with a revision of its own change for each version, each with a |
| 127 | +/// result of every check, 20k statements in all, and a revision whose |
| 128 | +/// sources match one of them. |
| 129 | +fn promotion_store() -> (tempfile::TempDir, Store, RevisionId) { |
| 130 | + let (dir, store) = store(); |
| 131 | + let revision = |store: &Store, change: usize, version: usize, docs: usize| { |
| 132 | + let objects = snapshot(version, docs); |
| 133 | + let root = TreeId::from(objects.last().unwrap().id()); |
| 134 | + store.write_all(&objects).unwrap(); |
| 135 | + let mut id = [0; 16]; |
| 136 | + id[..8].copy_from_slice(&(change as u64).to_le_bytes()); |
| 137 | + let encoded = Revision { |
| 138 | + change: ChangeId::from_bytes(id), |
| 139 | + predecessors: Vec::new(), |
| 140 | + base: Base::Acceptance(ObjectId::from_bytes([0; 32]).into()), |
| 141 | + deps: Vec::new(), |
| 142 | + snapshot: root, |
| 143 | + description: EncodedObject::blob(b"").id().into(), |
| 144 | + author: "user:bench".parse().unwrap(), |
| 145 | + } |
| 146 | + .encode(); |
| 147 | + store.write_all(std::slice::from_ref(&encoded)).unwrap(); |
| 148 | + RevisionId::from(encoded.id()) |
| 149 | + }; |
| 150 | + let revisions = STATEMENTS / CHECKS.len(); |
| 151 | + let mut statements = Vec::new(); |
| 152 | + for version in 0..revisions { |
| 153 | + let revision = revision(&store, version, version, version % 7); |
| 154 | + statements.extend(CHECKS.map(|(kind, paths)| { |
| 155 | + Statement { |
| 156 | + subject: RecordId::Revision(revision), |
| 157 | + kind: Kind::new(kind).unwrap(), |
| 158 | + verdict: Some(Verdict::Pass), |
| 159 | + issuer: "node:ci-1".parse().unwrap(), |
| 160 | + inputs: Some(inputs(paths, kind)), |
| 161 | + payload: None, |
| 162 | + supersedes: None, |
| 163 | + promoted_from: None, |
| 164 | + } |
| 165 | + .encode() |
| 166 | + })); |
| 167 | + } |
| 168 | + store.write_all(&statements).unwrap(); |
| 169 | + // Version 1000's sources with the docs of other versions: the seven |
| 170 | + // checks that don't read docs promote from version 1000, the two that |
| 171 | + // only read docs from the latest version with those docs. |
| 172 | + let target = revision(&store, revisions, 1000, 3); |
| 173 | + (dir, store, target) |
| 174 | +} |
| 175 | + |
| 176 | +/// Searching 20k statements for those to promote to a revision, with every |
| 177 | +/// digest cached: the cost of each search after the first. |
| 178 | +#[divan::bench(sample_count = 20)] |
| 179 | +fn promotions_cached(bencher: Bencher<'_, '_>) { |
| 180 | + let (_dir, store, target) = promotion_store(); |
| 181 | + let issuer: Principal = "node:queue".parse().unwrap(); |
| 182 | + let mut annotations = Annotations::open(&store).unwrap(); |
| 183 | + assert_eq!(annotations.promotions(target, &issuer).unwrap().len(), 9); |
| 184 | + let index = Index::open(&store).unwrap(); |
| 185 | + bencher |
| 186 | + .counter(ItemsCount::new(STATEMENTS)) |
| 187 | + .with_inputs(|| Annotations::new(&store, index.clone())) |
| 188 | + .bench_local_refs(|annotations| annotations.promotions(target, &issuer).unwrap()); |
| 189 | +} |
| 190 | + |
| 191 | +/// The first search, which computes and remembers the digest of each of |
| 192 | +/// the 20k statements, reading its subject's trees. |
| 193 | +#[divan::bench(sample_count = 10, sample_size = 1)] |
| 194 | +fn promotions_uncached(bencher: Bencher<'_, '_>) { |
| 195 | + let (dir, store, target) = promotion_store(); |
| 196 | + let issuer: Principal = "node:queue".parse().unwrap(); |
| 197 | + let log = dir.path().join(".origin/index/log"); |
| 198 | + bencher |
| 199 | + .counter(ItemsCount::new(STATEMENTS)) |
| 200 | + .with_inputs(|| { |
| 201 | + // Rebuilding without the old index forgets every digest. |
| 202 | + fs::remove_file(&log).unwrap(); |
| 203 | + Annotations::open(&store).unwrap() |
| 204 | + }) |
| 205 | + .bench_local_refs(|annotations| annotations.promotions(target, &issuer).unwrap()); |
| 206 | +} |
0 commit comments