project_note
Clone
https://git.tahinli.com/Tahinli/project_note.git
0 watching·0 forks·Rust·3.6 MiB·c49f5e7·Tahinli·1 month ago·2 commits
| 1 | use criterion::{criterion_group, criterion_main, Criterion}; |
| 2 | #[cfg(feature = "test_utils")] |
| 3 | mod sync { |
| 4 | |
| 5 | use super::*; |
| 6 | use bench_utils::{get_automerge_actions, TextAction}; |
| 7 | use loro_internal::{cursor::PosType, encoding::ExportMode, LoroDoc}; |
| 8 | |
| 9 | pub fn b4(c: &mut Criterion) { |
| 10 | let actions = get_automerge_actions(); |
| 11 | let mut b = c.benchmark_group("encode_with_sync"); |
| 12 | b.sample_size(10); |
| 13 | b.bench_function("update", |b| { |
| 14 | b.iter(|| { |
| 15 | let c1 = LoroDoc::new(); |
| 16 | c1.set_peer_id(1).unwrap(); |
| 17 | let c2 = LoroDoc::new(); |
| 18 | c2.set_peer_id(2).unwrap(); |
| 19 | let t1 = c1.get_text("text"); |
| 20 | let t2 = c2.get_text("text"); |
| 21 | for (i, action) in actions.iter().enumerate() { |
| 22 | if i > 2000 { |
| 23 | break; |
| 24 | } |
| 25 | let TextAction { pos, ins, del } = action; |
| 26 | if i % 2 == 0 { |
| 27 | let mut txn = c1.txn().unwrap(); |
| 28 | t1.delete_with_txn(&mut txn, *pos, *del, PosType::Unicode) |
| 29 | .unwrap(); |
| 30 | t1.insert_with_txn(&mut txn, *pos, ins, PosType::Unicode) |
| 31 | .unwrap(); |
| 32 | txn.commit().unwrap(); |
| 33 | |
| 34 | let update = c1.export(ExportMode::updates(&c2.oplog_vv())).unwrap(); |
| 35 | c2.import(&update).unwrap(); |
| 36 | } else { |
| 37 | let mut txn = c2.txn().unwrap(); |
| 38 | t2.delete_with_txn(&mut txn, *pos, *del, PosType::Unicode) |
| 39 | .unwrap(); |
| 40 | t2.insert_with_txn(&mut txn, *pos, ins, PosType::Unicode) |
| 41 | .unwrap(); |
| 42 | txn.commit().unwrap(); |
| 43 | let update = c2.export(ExportMode::updates(&c1.oplog_vv())).unwrap(); |
| 44 | c1.import(&update).unwrap(); |
| 45 | } |
| 46 | } |
| 47 | }) |
| 48 | }); |
| 49 | } |
| 50 | } |
| 51 | #[cfg(feature = "test_utils")] |
| 52 | mod run { |
| 53 | use super::*; |
| 54 | use bench_utils::TextAction; |
| 55 | use loro_internal::{cursor::PosType, encoding::ExportMode, LoroDoc}; |
| 56 | use std::hint::black_box; |
| 57 | |
| 58 | pub fn b4(c: &mut Criterion) { |
| 59 | let loro = LoroDoc::default(); |
| 60 | let mut ran = false; |
| 61 | let mut ensure_ran = || { |
| 62 | if !ran { |
| 63 | let actions = bench_utils::get_automerge_actions(); |
| 64 | let text = loro.get_text("text"); |
| 65 | for TextAction { pos, ins, del } in actions.iter() { |
| 66 | let mut txn = loro.txn().unwrap(); |
| 67 | text.delete_with_txn(&mut txn, *pos, *del, PosType::Unicode) |
| 68 | .unwrap(); |
| 69 | text.insert_with_txn(&mut txn, *pos, ins, PosType::Unicode) |
| 70 | .unwrap(); |
| 71 | } |
| 72 | ran = true; |
| 73 | } |
| 74 | }; |
| 75 | |
| 76 | let mut b = c.benchmark_group("encode"); |
| 77 | b.sample_size(10); |
| 78 | b.bench_function("B4_encode_updates", |b| { |
| 79 | ensure_ran(); |
| 80 | b.iter(|| { |
| 81 | let _ = loro.export(ExportMode::all_updates()).unwrap(); |
| 82 | }) |
| 83 | }); |
| 84 | b.bench_function("B4_decode_updates", |b| { |
| 85 | ensure_ran(); |
| 86 | let buf = loro.export(ExportMode::all_updates()).unwrap(); |
| 87 | |
| 88 | b.iter(|| { |
| 89 | let store2 = LoroDoc::default(); |
| 90 | store2.import(&buf).unwrap(); |
| 91 | }) |
| 92 | }); |
| 93 | b.bench_function("B4_decode_updates detached mode", |b| { |
| 94 | ensure_ran(); |
| 95 | let buf = loro.export(ExportMode::all_updates()).unwrap(); |
| 96 | |
| 97 | b.iter(|| { |
| 98 | let store2 = LoroDoc::default(); |
| 99 | store2.detach(); |
| 100 | store2.import(&buf).unwrap(); |
| 101 | }) |
| 102 | }); |
| 103 | b.bench_function("B4_encode_snapshot", |b| { |
| 104 | ensure_ran(); |
| 105 | b.iter(|| { |
| 106 | let _ = loro.export(ExportMode::Snapshot).unwrap(); |
| 107 | }) |
| 108 | }); |
| 109 | b.bench_function("B4_decode_snapshot", |b| { |
| 110 | ensure_ran(); |
| 111 | let buf = loro.export(ExportMode::Snapshot).unwrap(); |
| 112 | b.iter(|| { |
| 113 | let store2 = LoroDoc::default(); |
| 114 | store2.import(&buf).unwrap(); |
| 115 | }) |
| 116 | }); |
| 117 | |
| 118 | b.bench_function("B4_encode_json_update", |b| { |
| 119 | ensure_ran(); |
| 120 | b.iter(|| { |
| 121 | let _ = loro |
| 122 | .export_json_updates(&Default::default(), &loro.oplog_vv(), true) |
| 123 | .unwrap(); |
| 124 | }) |
| 125 | }); |
| 126 | b.bench_function("B4_decode_json_update", |b| { |
| 127 | ensure_ran(); |
| 128 | let json = loro |
| 129 | .export_json_updates(&Default::default(), &loro.oplog_vv(), true) |
| 130 | .unwrap(); |
| 131 | b.iter(|| { |
| 132 | let store2 = LoroDoc::default(); |
| 133 | store2.import_json_updates(json.clone()).unwrap(); |
| 134 | }) |
| 135 | }); |
| 136 | } |
| 137 | |
| 138 | fn build_issue_992_source_doc(paragraphs: usize) -> LoroDoc { |
| 139 | let doc = LoroDoc::new(); |
| 140 | doc.set_peer_id(1).unwrap(); |
| 141 | let text = doc.get_text("codemirror"); |
| 142 | let mut chunks = Vec::with_capacity(paragraphs); |
| 143 | for i in 0..paragraphs { |
| 144 | chunks.push(format!( |
| 145 | "# Section {i}\nParagraph {i} line A with repeated markdown content.\nParagraph {i} line B with **bold** and _italic_ markers.\n\n" |
| 146 | )); |
| 147 | } |
| 148 | text.insert(0, &chunks.concat(), PosType::Unicode).unwrap(); |
| 149 | doc.commit_then_renew(); |
| 150 | |
| 151 | for i in 0..6 { |
| 152 | text.insert( |
| 153 | text.len_unicode(), |
| 154 | &format!("Tail block {i}: {}\n", "x".repeat(2048)), |
| 155 | PosType::Unicode, |
| 156 | ) |
| 157 | .unwrap(); |
| 158 | doc.commit_then_renew(); |
| 159 | } |
| 160 | |
| 161 | for i in 0..3 { |
| 162 | let current = text.to_string(); |
| 163 | let next = current.replace(&format!("Section {i}"), &format!("Section {i} updated")) |
| 164 | + &format!("\nreplace-round-{i}:{}", "y".repeat(1024)); |
| 165 | text.delete(0, text.len_unicode(), PosType::Unicode) |
| 166 | .unwrap(); |
| 167 | text.insert(0, &next, PosType::Unicode).unwrap(); |
| 168 | doc.commit_then_renew(); |
| 169 | } |
| 170 | |
| 171 | doc |
| 172 | } |
| 173 | |
| 174 | fn clone_doc(doc: &LoroDoc) -> LoroDoc { |
| 175 | LoroDoc::from_snapshot(&doc.export(ExportMode::Snapshot).unwrap()).unwrap() |
| 176 | } |
| 177 | |
| 178 | fn build_issue_992_shallow_tail_doc(paragraphs: usize, tail_rounds: usize) -> LoroDoc { |
| 179 | let source = build_issue_992_source_doc(paragraphs); |
| 180 | let source = clone_doc(&source); |
| 181 | let shallow_bytes = source |
| 182 | .export(ExportMode::shallow_snapshot(&source.oplog_frontiers())) |
| 183 | .unwrap(); |
| 184 | let shallow_doc = LoroDoc::from_snapshot(&shallow_bytes).unwrap(); |
| 185 | let text = shallow_doc.get_text("codemirror"); |
| 186 | |
| 187 | for i in 0..tail_rounds { |
| 188 | let current = text.to_string(); |
| 189 | let next = current.replace(&format!("Section {i}"), &format!("Section {i} tail-{i}")) |
| 190 | + &format!("\npost-shallow-tail-{i}:{}", "z".repeat(4096)); |
| 191 | text.delete(0, text.len_unicode(), PosType::Unicode) |
| 192 | .unwrap(); |
| 193 | text.insert(0, &next, PosType::Unicode).unwrap(); |
| 194 | shallow_doc.commit_then_renew(); |
| 195 | } |
| 196 | |
| 197 | shallow_doc |
| 198 | } |
| 199 | |
| 200 | pub fn issue_992_shallow_snapshot_tail(c: &mut Criterion) { |
| 201 | let mut b = c.benchmark_group("encode_regression"); |
| 202 | b.sample_size(10); |
| 203 | |
| 204 | // This mirrors https://github.com/loro-dev/loro/issues/992: import a shallow |
| 205 | // snapshot, perform a few full-document text replacements, then export snapshot. |
| 206 | // The fixture is intentionally below the original report size so a regression is |
| 207 | // visible without making the default benchmark prohibitively slow. |
| 208 | let shallow_tail_doc = build_issue_992_shallow_tail_doc(1000, 2); |
| 209 | b.bench_function("issue_992_shallow_tail_snapshot_export", |b| { |
| 210 | b.iter(|| { |
| 211 | black_box(shallow_tail_doc.export(ExportMode::Snapshot).unwrap().len()); |
| 212 | }) |
| 213 | }); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | mod import { |
| 218 | use criterion::{BatchSize, Criterion}; |
| 219 | use loro_common::LoroValue; |
| 220 | use loro_internal::{cursor::PosType, encoding::ExportMode, LoroDoc}; |
| 221 | |
| 222 | #[allow(dead_code)] |
| 223 | pub fn causal_iter(c: &mut Criterion) { |
| 224 | let mut b = c.benchmark_group("causal_iter"); |
| 225 | b.sample_size(10); |
| 226 | b.bench_function("parallel_500", |b| { |
| 227 | b.iter(|| { |
| 228 | let c1 = LoroDoc::new(); |
| 229 | c1.set_peer_id(1).unwrap(); |
| 230 | let c2 = LoroDoc::new(); |
| 231 | c1.set_peer_id(2).unwrap(); |
| 232 | |
| 233 | let text1 = c1.get_text("text"); |
| 234 | let text2 = c2.get_text("text"); |
| 235 | for _ in 0..500 { |
| 236 | text1 |
| 237 | .insert_with_txn(&mut c1.txn().unwrap(), 0, "1", PosType::Unicode) |
| 238 | .unwrap(); |
| 239 | text2 |
| 240 | .insert_with_txn(&mut c2.txn().unwrap(), 0, "2", PosType::Unicode) |
| 241 | .unwrap(); |
| 242 | } |
| 243 | |
| 244 | let updates = c2.export(ExportMode::updates(&c1.oplog_vv())).unwrap(); |
| 245 | c1.import(&updates).unwrap() |
| 246 | }) |
| 247 | }); |
| 248 | } |
| 249 | |
| 250 | struct BinaryImportFixture { |
| 251 | base_update: Vec<u8>, |
| 252 | incremental_update: Vec<u8>, |
| 253 | } |
| 254 | |
| 255 | fn text_split_fixture(fragments: usize) -> BinaryImportFixture { |
| 256 | const CHUNK_LEN: usize = 256; |
| 257 | const PEER_A: u64 = 1; |
| 258 | const PEER_B: u64 = 2; |
| 259 | |
| 260 | let doc_len = CHUNK_LEN * fragments; |
| 261 | let doc_a = LoroDoc::new(); |
| 262 | doc_a.set_peer_id(PEER_A).unwrap(); |
| 263 | let text_a = doc_a.get_text("text"); |
| 264 | let mut txn = doc_a.txn().unwrap(); |
| 265 | text_a |
| 266 | .insert_with_txn(&mut txn, 0, &"a".repeat(doc_len), PosType::Unicode) |
| 267 | .unwrap(); |
| 268 | txn.commit().unwrap(); |
| 269 | let base_update = doc_a.export(ExportMode::all_updates()).unwrap(); |
| 270 | |
| 271 | let doc_b = LoroDoc::new(); |
| 272 | doc_b.set_peer_id(PEER_B).unwrap(); |
| 273 | let text_b = doc_b.get_text("text"); |
| 274 | doc_b.import(&base_update).unwrap(); |
| 275 | let base_vv = doc_b.oplog_vv(); |
| 276 | let mut txn = doc_b.txn().unwrap(); |
| 277 | for i in 0..(fragments - 1) { |
| 278 | let pos = (i + 1) * CHUNK_LEN + i; |
| 279 | text_b |
| 280 | .insert_with_txn(&mut txn, pos, "x", PosType::Unicode) |
| 281 | .unwrap(); |
| 282 | } |
| 283 | txn.commit().unwrap(); |
| 284 | let incremental_update = doc_b.export(ExportMode::updates(&base_vv)).unwrap(); |
| 285 | |
| 286 | BinaryImportFixture { |
| 287 | base_update, |
| 288 | incremental_update, |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | fn list_diff_fixture(items: usize, inserts: usize) -> BinaryImportFixture { |
| 293 | const PEER_A: u64 = 11; |
| 294 | const PEER_B: u64 = 12; |
| 295 | |
| 296 | let doc_a = LoroDoc::new(); |
| 297 | doc_a.set_peer_id(PEER_A).unwrap(); |
| 298 | let list_a = doc_a.get_list("list"); |
| 299 | let mut txn = doc_a.txn().unwrap(); |
| 300 | for i in 0..items { |
| 301 | list_a |
| 302 | .insert_with_txn(&mut txn, i, LoroValue::I64(i as i64)) |
| 303 | .unwrap(); |
| 304 | } |
| 305 | txn.commit().unwrap(); |
| 306 | let base_update = doc_a.export(ExportMode::all_updates()).unwrap(); |
| 307 | |
| 308 | let doc_b = LoroDoc::new(); |
| 309 | doc_b.set_peer_id(PEER_B).unwrap(); |
| 310 | let list_b = doc_b.get_list("list"); |
| 311 | doc_b.import(&base_update).unwrap(); |
| 312 | let base_vv = doc_b.oplog_vv(); |
| 313 | let mut txn = doc_b.txn().unwrap(); |
| 314 | for i in 0..inserts { |
| 315 | let len = items + i; |
| 316 | let pos = (i * 7) % len; |
| 317 | list_b |
| 318 | .insert_with_txn(&mut txn, pos, LoroValue::I64(-((i as i64) + 1))) |
| 319 | .unwrap(); |
| 320 | } |
| 321 | txn.commit().unwrap(); |
| 322 | let incremental_update = doc_b.export(ExportMode::updates(&base_vv)).unwrap(); |
| 323 | |
| 324 | BinaryImportFixture { |
| 325 | base_update, |
| 326 | incremental_update, |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | fn import_attached(fixture: &BinaryImportFixture) { |
| 331 | let doc = LoroDoc::new(); |
| 332 | doc.import(&fixture.base_update).unwrap(); |
| 333 | doc.import(&fixture.incremental_update).unwrap(); |
| 334 | } |
| 335 | |
| 336 | fn import_detached(fixture: &BinaryImportFixture) { |
| 337 | let doc = LoroDoc::new(); |
| 338 | doc.import(&fixture.base_update).unwrap(); |
| 339 | doc.detach(); |
| 340 | doc.import(&fixture.incremental_update).unwrap(); |
| 341 | } |
| 342 | |
| 343 | fn checkout_after_detached_import(fixture: &BinaryImportFixture) { |
| 344 | let doc = LoroDoc::new(); |
| 345 | doc.import(&fixture.base_update).unwrap(); |
| 346 | doc.detach(); |
| 347 | doc.import(&fixture.incremental_update).unwrap(); |
| 348 | doc.checkout_to_latest(); |
| 349 | } |
| 350 | |
| 351 | #[allow(dead_code)] |
| 352 | pub fn import_regression(c: &mut Criterion) { |
| 353 | let text_fixture = text_split_fixture(1024); |
| 354 | let list_fixture = list_diff_fixture(4096, 1024); |
| 355 | |
| 356 | let mut b = c.benchmark_group("import_regression"); |
| 357 | b.sample_size(10); |
| 358 | |
| 359 | b.bench_function("text_split_attached_import_1024", |b| { |
| 360 | b.iter_batched(|| &text_fixture, import_attached, BatchSize::SmallInput) |
| 361 | }); |
| 362 | b.bench_function("text_split_detached_import_1024", |b| { |
| 363 | b.iter_batched(|| &text_fixture, import_detached, BatchSize::SmallInput) |
| 364 | }); |
| 365 | b.bench_function("text_split_checkout_1024", |b| { |
| 366 | b.iter_batched( |
| 367 | || &text_fixture, |
| 368 | checkout_after_detached_import, |
| 369 | BatchSize::SmallInput, |
| 370 | ) |
| 371 | }); |
| 372 | b.bench_function("list_attached_import_4096x1024", |b| { |
| 373 | b.iter_batched(|| &list_fixture, import_attached, BatchSize::SmallInput) |
| 374 | }); |
| 375 | b.bench_function("list_checkout_4096x1024", |b| { |
| 376 | b.iter_batched( |
| 377 | || &list_fixture, |
| 378 | checkout_after_detached_import, |
| 379 | BatchSize::SmallInput, |
| 380 | ) |
| 381 | }); |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | pub fn dumb(_c: &mut Criterion) {} |
| 386 | |
| 387 | #[cfg(feature = "test_utils")] |
| 388 | criterion_group!( |
| 389 | benches, |
| 390 | run::b4, |
| 391 | run::issue_992_shallow_snapshot_tail, |
| 392 | sync::b4, |
| 393 | import::causal_iter, |
| 394 | import::import_regression |
| 395 | ); |
| 396 | #[cfg(not(feature = "test_utils"))] |
| 397 | criterion_group!(benches, dumb); |
| 398 | criterion_main!(benches); |