|
| 1 | +//! The landing queue: the latency of one landing, made on the target's tip |
| 2 | +//! or rebased onto it, with and without a requirement, of a target landing |
| 3 | +//! another after long histories, and the throughput of several proposers |
| 4 | +//! landing different changes on one target at once. Run on a real disk: |
| 5 | +//! syncs are most of the cost. |
| 6 | +
|
| 7 | +use std::thread; |
| 8 | + |
| 9 | +use divan::Bencher; |
| 10 | +use divan::counter::ItemsCount; |
| 11 | +use origin_platform::Native; |
| 12 | +use origin_shared::{ |
| 13 | + AcceptanceId, Base, ChangeId, EncodedObject, EntryKind, EntryName, Kind, NodeKey, |
| 14 | + PrincipalName, Record, RecordId, Revision, Statement, TargetName, Tree, TreeEntry, Verdict, |
| 15 | +}; |
| 16 | +use origin_src::annotations::Annotations; |
| 17 | +use origin_src::queue::{Landing, Queue}; |
| 18 | +use origin_src::store::Store; |
| 19 | + |
| 20 | +fn main() { |
| 21 | + divan::main(); |
| 22 | +} |
| 23 | + |
| 24 | +/// A repository whose `policy` and `main` the node `queue` orders. |
| 25 | +struct Repo { |
| 26 | + _dir: tempfile::TempDir, |
| 27 | + store: Store, |
| 28 | + key: NodeKey, |
| 29 | +} |
| 30 | + |
| 31 | +impl Repo { |
| 32 | + /// A repository where landing on `main` requires `require`, a TOML |
| 33 | + /// array. |
| 34 | + fn new(require: &str) -> Self { |
| 35 | + let dir = tempfile::tempdir().unwrap(); |
| 36 | + let store = Store::new(dir.path().join(".origin"), Native::default()); |
| 37 | + store.init().unwrap(); |
| 38 | + let repo = Self { |
| 39 | + _dir: dir, |
| 40 | + store, |
| 41 | + key: NodeKey::from_bytes(&[7; 32]), |
| 42 | + }; |
| 43 | + let policy = format!( |
| 44 | + "[nodes]\nqueue = \"{}\"\n[groups]\nrunners = [\"node:ci\"]\n\ |
| 45 | + [targets.policy]\nauthority = \"node:queue\"\n\ |
| 46 | + [targets.feature]\nauthority = \"node:queue\"\n\ |
| 47 | + [targets.main]\nauthority = \"node:queue\"\nrequire = {require}\n", |
| 48 | + repo.key.public() |
| 49 | + ); |
| 50 | + repo.queue().init(&policy).unwrap(); |
| 51 | + repo |
| 52 | + } |
| 53 | + |
| 54 | + fn queue(&self) -> Queue<'_> { |
| 55 | + Queue::new(&self.store, &self.key, PrincipalName::new("queue").unwrap()) |
| 56 | + } |
| 57 | + |
| 58 | + fn tip(&self) -> AcceptanceId { |
| 59 | + self.tip_of(&TargetName::MAIN) |
| 60 | + } |
| 61 | + |
| 62 | + fn tip_of(&self, target: &TargetName) -> AcceptanceId { |
| 63 | + self.store.head(target).unwrap().unwrap().acceptance |
| 64 | + } |
| 65 | + |
| 66 | + /// Writes a change made on `base`, a tip of `main`, adding the file |
| 67 | + /// `file-<n>`. |
| 68 | + fn change(&self, n: usize, base: AcceptanceId) -> ChangeId { |
| 69 | + let mut tree: Tree = self |
| 70 | + .store |
| 71 | + .get(self.store.get(base).unwrap().body().state) |
| 72 | + .unwrap(); |
| 73 | + let content = EncodedObject::blob(format!("fn f{n}() {{}}\n").as_bytes()); |
| 74 | + tree.insert( |
| 75 | + EntryName::new(format!("file-{n}")).unwrap(), |
| 76 | + TreeEntry { |
| 77 | + kind: EntryKind::File, |
| 78 | + id: content.id(), |
| 79 | + }, |
| 80 | + ); |
| 81 | + let tree = tree.encode(); |
| 82 | + let mut id = [0; 16]; |
| 83 | + id[..8].copy_from_slice(&(n as u64).to_le_bytes()); |
| 84 | + let change = ChangeId::from_bytes(id); |
| 85 | + let revision = Revision { |
| 86 | + change, |
| 87 | + predecessors: Vec::new(), |
| 88 | + base: Base::Acceptance(base), |
| 89 | + deps: Vec::new(), |
| 90 | + snapshot: tree.id().into(), |
| 91 | + description: EncodedObject::blob(b"").id().into(), |
| 92 | + author: "user:bench".parse().unwrap(), |
| 93 | + } |
| 94 | + .encode(); |
| 95 | + let description = EncodedObject::blob(b""); |
| 96 | + self.store.write_all(&[content, tree, description]).unwrap(); |
| 97 | + self.store.write_all(&[revision]).unwrap(); |
| 98 | + change |
| 99 | + } |
| 100 | + |
| 101 | + fn land(&self, change: ChangeId) { |
| 102 | + self.land_on(change, &TargetName::MAIN); |
| 103 | + } |
| 104 | + |
| 105 | + fn land_on(&self, change: ChangeId, onto: &TargetName) { |
| 106 | + let landings = self.queue().land_change(change, onto).unwrap(); |
| 107 | + assert!( |
| 108 | + matches!(landings[..], [Landing::Landed { .. }]), |
| 109 | + "{landings:?}" |
| 110 | + ); |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +/// One landing of a change made on the tip: no rebase, no requirement. |
| 115 | +#[divan::bench(sample_count = 50)] |
| 116 | +fn land_on_tip(bencher: Bencher<'_, '_>) { |
| 117 | + let repo = Repo::new("[]"); |
| 118 | + let mut n = 0; |
| 119 | + bencher |
| 120 | + .with_inputs(|| { |
| 121 | + n += 1; |
| 122 | + repo.change(n, repo.tip()) |
| 123 | + }) |
| 124 | + .bench_local_values(|change| repo.land(change)); |
| 125 | +} |
| 126 | + |
| 127 | +/// One landing of a change made on an older tip, which the queue rebases |
| 128 | +/// into a new revision. |
| 129 | +#[divan::bench(sample_count = 50)] |
| 130 | +fn land_rebased(bencher: Bencher<'_, '_>) { |
| 131 | + let repo = Repo::new("[]"); |
| 132 | + let first = repo.tip(); |
| 133 | + let mut n = 0; |
| 134 | + bencher |
| 135 | + .with_inputs(|| { |
| 136 | + n += 1; |
| 137 | + repo.change(n, first) |
| 138 | + }) |
| 139 | + .bench_local_values(|change| repo.land(change)); |
| 140 | +} |
| 141 | + |
| 142 | +/// One landing of a change made on the tip, with the `ci.test` result |
| 143 | +/// `main` requires. |
| 144 | +#[divan::bench(sample_count = 50)] |
| 145 | +fn land_with_requirement(bencher: Bencher<'_, '_>) { |
| 146 | + let repo = Repo::new(r#"[{ kind = "ci.test", verdict = "pass", issuer = "group:runners" }]"#); |
| 147 | + let mut n = 0; |
| 148 | + bencher |
| 149 | + .with_inputs(|| { |
| 150 | + n += 1; |
| 151 | + let change = repo.change(n, repo.tip()); |
| 152 | + let annotations = Annotations::open(&repo.store).unwrap(); |
| 153 | + let head = annotations.index().heads(change)[0]; |
| 154 | + let result = Statement { |
| 155 | + subject: RecordId::Revision(head), |
| 156 | + kind: Kind::new("ci.test").unwrap(), |
| 157 | + verdict: Some(Verdict::Pass), |
| 158 | + issuer: "node:ci".parse().unwrap(), |
| 159 | + inputs: None, |
| 160 | + payload: None, |
| 161 | + supersedes: None, |
| 162 | + promoted_from: None, |
| 163 | + }; |
| 164 | + annotations.issue(&result, None).unwrap(); |
| 165 | + change |
| 166 | + }) |
| 167 | + .bench_local_values(|change| repo.land(change)); |
| 168 | +} |
| 169 | + |
| 170 | +/// How many changes each target lands before they merge. |
| 171 | +const HISTORY: usize = 1_000; |
| 172 | + |
| 173 | +/// Landing the tip of `feature` on `main` once each landed `HISTORY` |
| 174 | +/// changes since `feature` forked, after `feature` lands one more: the |
| 175 | +/// merge base reads both histories whole. |
| 176 | +#[divan::bench(sample_count = 20, sample_size = 1)] |
| 177 | +fn land_target_after_long_histories(bencher: Bencher<'_, '_>) { |
| 178 | + let repo = Repo::new("[]"); |
| 179 | + let feature = TargetName::new("feature").unwrap(); |
| 180 | + repo.queue() |
| 181 | + .create_target(&feature, &TargetName::MAIN) |
| 182 | + .unwrap(); |
| 183 | + for n in 0..HISTORY { |
| 184 | + repo.land(repo.change(n, repo.tip())); |
| 185 | + let change = repo.change(HISTORY + n, repo.tip_of(&feature)); |
| 186 | + repo.land_on(change, &feature); |
| 187 | + } |
| 188 | + let mut n = 2 * HISTORY; |
| 189 | + bencher |
| 190 | + .with_inputs(|| { |
| 191 | + n += 1; |
| 192 | + let change = repo.change(n, repo.tip_of(&feature)); |
| 193 | + repo.land_on(change, &feature); |
| 194 | + }) |
| 195 | + .bench_local_values(|()| { |
| 196 | + let landing = repo |
| 197 | + .queue() |
| 198 | + .land_target(&feature, &TargetName::MAIN) |
| 199 | + .unwrap(); |
| 200 | + assert!(matches!(landing, Landing::Landed { .. }), "{landing:?}"); |
| 201 | + }); |
| 202 | +} |
| 203 | + |
| 204 | +/// How many changes each proposer lands. |
| 205 | +const EACH: usize = 8; |
| 206 | + |
| 207 | +/// `proposers` threads landing `EACH` changes each on `main` at once, all |
| 208 | +/// made on its first tip, so all but the first are rebased. Threads lock |
| 209 | +/// the queue as processes do: the lock belongs to the open file. |
| 210 | +#[divan::bench(args = [1, 2, 4, 8], sample_count = 5, sample_size = 1)] |
| 211 | +fn concurrent_proposers(bencher: Bencher<'_, '_>, proposers: usize) { |
| 212 | + bencher |
| 213 | + .counter(ItemsCount::new(proposers * EACH)) |
| 214 | + .with_inputs(|| { |
| 215 | + let repo = Repo::new("[]"); |
| 216 | + let first = repo.tip(); |
| 217 | + let changes: Vec<_> = (0..proposers * EACH) |
| 218 | + .map(|n| repo.change(n, first)) |
| 219 | + .collect(); |
| 220 | + (repo, changes) |
| 221 | + }) |
| 222 | + .bench_local_values(|(repo, changes)| { |
| 223 | + thread::scope(|scope| { |
| 224 | + for changes in changes.chunks(EACH) { |
| 225 | + let repo = &repo; |
| 226 | + scope.spawn(move || { |
| 227 | + for &change in changes { |
| 228 | + repo.land(change); |
| 229 | + } |
| 230 | + }); |
| 231 | + } |
| 232 | + }); |
| 233 | + (repo, changes) |
| 234 | + }); |
| 235 | +} |
0 commit comments