forge
Explore
Log in

Don't you dare go Hollow

project_note

Clone
★ 0

0 watching0 forksRust3.6 MiBc49f5e7Tahinli1 month ago2 commits

state_size.rs127 lines, 4.0 KiBHistoryRawDownload
1use loro_common::{LoroResult, LoroValue};
2use loro_internal::{loro::ExportMode, LoroDoc};
3use rand::{thread_rng, Rng};
4
5#[derive(Debug, Clone, Copy)]
6enum Kind {
7 Map,
8 List,
9 MovableList,
10 Text,
11 Tree,
12 Counter,
13}
14
15fn empty_state() -> Vec<u8> {
16 let doc = LoroDoc::new_auto_commit();
17 doc.export(ExportMode::StateOnly(None)).unwrap()
18}
19
20fn create_elem_sequentially(n: usize, kind: Kind) -> LoroResult<Vec<u8>> {
21 let doc = LoroDoc::new_auto_commit();
22 for i in 0..n {
23 match kind {
24 Kind::Map => {
25 doc.get_map("map").insert(&i.to_string(), i as u32)?;
26 }
27 Kind::List => {
28 doc.get_list("list").push(i as u32)?;
29 }
30 Kind::MovableList => {
31 doc.get_movable_list("movable_list")
32 .push(LoroValue::from(i as u32))?;
33 }
34 Kind::Text => {
35 let text = doc.get_text("text");
36 text.insert_unicode(text.len_unicode(), &i.to_string())?;
37 }
38 Kind::Tree => {
39 doc.get_tree("tree").enable_fractional_index(0);
40 doc.get_tree("tree")
41 .create(loro_internal::TreeParentId::Root)?;
42 }
43 Kind::Counter => {
44 #[cfg(feature = "counter")]
45 {
46 doc.get_counter("counter").increment(i as f64)?;
47 }
48 }
49 }
50 }
51 let bytes = doc.export(ExportMode::StateOnly(None)).unwrap();
52 Ok(bytes)
53}
54
55fn create_elem_randomly(n: usize, kind: Kind) -> LoroResult<Vec<u8>> {
56 let doc = LoroDoc::new_auto_commit();
57 let mut rng = thread_rng();
58 for _ in 0..n / 5 {
59 for _ in 0..5 {
60 match kind {
61 Kind::Map => {
62 let i = rng.gen::<usize>() % 1000;
63 doc.get_map("map").insert(&i.to_string(), i as u32)?;
64 }
65 Kind::List => {
66 let list = doc.get_list("list");
67 let i = rng.gen::<usize>() % (list.len() + 1);
68 list.insert(i, i as u32)?;
69 }
70 Kind::MovableList => {
71 let list = doc.get_movable_list("movable_list");
72 let i = rng.gen::<usize>() % (list.len() + 1);
73 list.insert(i, i as u32)?;
74 }
75 Kind::Text => {
76 let text = doc.get_text("text");
77 let i = rng.gen::<usize>() % (text.len_unicode() + 1);
78 text.insert_unicode(i, &i.to_string())?;
79 }
80 Kind::Tree => {
81 doc.get_tree("tree").enable_fractional_index(0);
82 doc.get_tree("tree")
83 .create(loro_internal::TreeParentId::Root)?;
84 }
85 Kind::Counter => {
86 #[cfg(feature = "counter")]
87 {
88 doc.get_counter("counter").increment(rng.gen::<f64>())?;
89 }
90 }
91 }
92 }
93 doc.set_peer_id(rng.gen::<u64>()).unwrap();
94 }
95 let bytes = doc.export(ExportMode::StateOnly(None)).unwrap();
96 Ok(bytes)
97}
98
99fn main() -> LoroResult<()> {
100 for kind in [
101 Kind::Map,
102 Kind::List,
103 Kind::MovableList,
104 Kind::Text,
105 #[allow(unused)]
106 Kind::Tree,
107 #[cfg(feature = "counter")]
108 Kind::Counter,
109 ] {
110 println!("=============================");
111 println!("{kind:?}");
112 println!("=============================");
113
114 for (title, bytes) in [
115 ("empty", empty_state()),
116 ("10 items", create_elem_sequentially(10, kind)?),
117 (
118 "1000 items sequentially",
119 create_elem_sequentially(1000, kind)?,
120 ),
121 ("1000 items randomly", create_elem_randomly(1000, kind)?),
122 ] {
123 println!("{}: {} bytes", title, bytes.len())
124 }
125 }
126 Ok(())
127}