| 1 | //! Engine cost per keystroke and per remote change, through the production |
| 2 | //! interpreter and fuel (native realtime audit, findings 2, 7 and 9). |
| 3 | //! |
| 4 | //! cargo run --release --example realtime -- /tmp/rt-audit/docs/p*.bin |
| 5 | //! |
| 6 | //! Documents come from fixtures/generate-large-doc.mjs. Each keystroke is |
| 7 | //! what CRDTReplica does for one typed character: `display_edit` on the |
| 8 | //! displayed source, the update applied to the authority, the selection's |
| 9 | //! two anchors resolved, and the authority's presentation (`project_delta` |
| 10 | //! where the module has it, else the whole projection). |
| 11 | use docs_native_host::Host; |
| 12 | use serde_json::{json, Value}; |
| 13 | use std::time::Instant; |
| 14 | |
| 15 | fn call(host: &mut Host, request: &Value) -> Result<Vec<u8>, &'static str> { host.call(&serde_json::to_vec(request).unwrap()).map(<[u8]>::to_vec) } |
| 16 | fn median(mut v: Vec<f64>) -> f64 { v.sort_by(|a, b| a.partial_cmp(b).unwrap()); if v.is_empty() { f64::NAN } else { v[v.len() / 2] } } |
| 17 | fn open(bytes: &[u8], client: u64) -> Result<Host, &'static str> { |
| 18 | let mut host = Host::new()?; |
| 19 | call(&mut host, &json!({"op":"open","guid":"bench","client":client}))?; |
| 20 | let mut envelope = vec![255]; envelope.extend_from_slice(bytes); |
| 21 | host.call(&envelope)?; |
| 22 | Ok(host) |
| 23 | } |
| 24 | fn text_offset(projection: &Value) -> u32 { |
| 25 | // The middle of the document, inside a text run. |
| 26 | fn walk(v: &Value, offset: &mut u32, out: &mut Vec<(u32, u32)>) { |
| 27 | match v["type"].as_str().unwrap_or("") { |
| 28 | "text" => { let n: u32 = v["runs"].as_array().unwrap().iter().map(|r| r["insert"].as_str().unwrap().encode_utf16().count() as u32).sum(); out.push((*offset, n)); *offset += n; }, |
| 29 | "hard_break" | "image" => *offset += 1, |
| 30 | "horizontal_rule" => *offset += 2, |
| 31 | tag => { for c in v["children"].as_array().into_iter().flatten() { walk(c, offset, out); } if ["paragraph","heading","code_block"].contains(&tag) { *offset += 1; } }, |
| 32 | } |
| 33 | } |
| 34 | let mut offset = 0; let mut texts = Vec::new(); |
| 35 | for c in projection["children"].as_array().unwrap() { walk(c, &mut offset, &mut texts); } |
| 36 | let (start, n) = texts.iter().copied().filter(|(_, n)| *n > 20).nth(texts.len() / 2).unwrap_or(texts[0]); |
| 37 | start + n / 2 |
| 38 | } |
| 39 | fn main() { |
| 40 | for path in std::env::args().skip(1) { |
| 41 | let name = std::path::Path::new(&path).file_stem().unwrap().to_string_lossy().to_string(); |
| 42 | let bytes = std::fs::read(&path).unwrap(); |
| 43 | let t = Instant::now(); |
| 44 | let (mut authority, mut source) = match (open(&bytes, 11), open(&bytes, 12)) { (Ok(a), Ok(s)) => (a, s), _ => { println!("{name:6} open FAILED (fuel)"); continue; } }; |
| 45 | let opened = t.elapsed().as_secs_f64() * 1e3 / 2.0; |
| 46 | // An unknown op poisons a host, so ask a throwaway one. |
| 47 | let delta = open(b"\0\0", 13).ok().and_then(|mut probe| call(&mut probe, &json!({"op":"project_delta"})).ok()).is_some(); |
| 48 | let projection: Value = match call(&mut authority, &json!({"op":"project"})) { Ok(p) => serde_json::from_slice(&p).unwrap(), Err(e) => { println!("{name:6} project FAILED ({e})"); continue; } }; |
| 49 | let mut at = text_offset(&projection); |
| 50 | let (mut command, mut apply, mut anchors, mut present, mut sizes) = (vec![], vec![], vec![], vec![], vec![]); |
| 51 | let mut failed = None; |
| 52 | for _ in 0..12 { |
| 53 | let t = Instant::now(); |
| 54 | let update = match call(&mut source, &json!({"op":"display_edit","at":at,"length":0,"text":"x"})) { Ok(u) => u, Err(e) => { failed = Some(e); break; } }; |
| 55 | command.push(t.elapsed().as_secs_f64() * 1e3); sizes.push(update.len()); |
| 56 | let t = Instant::now(); |
| 57 | let mut envelope = vec![254]; envelope.extend(&update); |
| 58 | if let Err(e) = authority.call(&envelope) { failed = Some(e); break; } |
| 59 | apply.push(t.elapsed().as_secs_f64() * 1e3); |
| 60 | let t = Instant::now(); |
| 61 | for end in [at + 1, at + 1] { |
| 62 | let anchor: Value = match call(&mut source, &json!({"op":"anchor","at":end})) { Ok(a) => serde_json::from_slice(&a).unwrap(), Err(e) => { failed = Some(e); break; } }; |
| 63 | if let Err(e) = call(&mut authority, &json!({"op":"resolve","anchor":anchor})) { failed = Some(e); break; } |
| 64 | } |
| 65 | anchors.push(t.elapsed().as_secs_f64() * 1e3); |
| 66 | let t = Instant::now(); |
| 67 | let op = if delta { "project_delta" } else { "project" }; |
| 68 | match call(&mut authority, &json!({"op":op})) { Ok(_) => present.push(t.elapsed().as_secs_f64() * 1e3), Err(e) => { failed = Some(e); break; } } |
| 69 | at += 1; |
| 70 | } |
| 71 | let keystroke: f64 = median(command.clone()) + median(apply.clone()) + median(anchors.clone()) + median(present.clone()); |
| 72 | println!("{name:6} open {opened:7.1} ms | display_edit {:7.2} | authority apply {:6.2} | 2 anchors {:7.2} | {} {:7.2} | keystroke {keystroke:7.2} ms | update {} B{}", |
| 73 | median(command), median(apply), median(anchors), if delta { "delta" } else { "project" }, median(present), sizes.iter().max().unwrap_or(&0), |
| 74 | failed.map(|e| format!(" | FAILED: {e}")).unwrap_or_default()); |
| 75 | } |
| 76 | } |
| 77 | |