forge
Explore
Log in

Don't you dare go Hollow

project_note

Clone
★ 0

0 watching0 forksRust3.6 MiBc49f5e7Tahinli1 month ago2 commits

jsonpath.rs317 lines, 9.7 KiBHistoryRawDownload
1use criterion::{criterion_group, criterion_main, Criterion};
2
3#[cfg(feature = "test_utils")]
4mod jsonpath {
5 use criterion::Criterion;
6 use loro_internal::{ListHandler, LoroDoc, MapHandler};
7 use std::hint::black_box;
8
9 // Sizes to test: small, medium, large
10 const SIZES: [usize; 3] = [100, 500, 2000];
11
12 /// Setup a large document with:
13 /// - books[0..size] with title, author, price, available
14 /// - '1984' by George Orwell every ~100 items
15 /// - Nested structure under $.catalog.fiction.books for recursive tests
16 fn setup_large_doc(size: usize) -> LoroDoc {
17 let doc = LoroDoc::new_auto_commit();
18 let store = doc.get_map("store");
19 let books = store
20 .insert_container("books", ListHandler::new_detached())
21 .unwrap();
22
23 for i in 0..size {
24 let book = books
25 .insert_container(i, MapHandler::new_detached())
26 .unwrap();
27 let title = if i % 100 == 42 {
28 "1984"
29 } else {
30 &format!("Book {}", i)
31 };
32 let author = if i % 100 == 42 {
33 "George Orwell"
34 } else {
35 &format!("Author {}", i % 10)
36 };
37 let price = 5.0 + (i % 25) as f64;
38 let available = i % 4 != 0;
39
40 book.insert("title", title).unwrap();
41 book.insert("author", author).unwrap();
42 book.insert("price", price).unwrap();
43 book.insert("available", available).unwrap();
44 }
45
46 // Add nested structure for $..title and recursive filters
47 let catalog = doc.get_map("catalog");
48 let fiction = catalog
49 .insert_container("fiction", MapHandler::new_detached())
50 .unwrap();
51 let nested_books = fiction
52 .insert_container("books", ListHandler::new_detached())
53 .unwrap();
54 for i in 0..10 {
55 let b = nested_books
56 .insert_container(i, MapHandler::new_detached())
57 .unwrap();
58 b.insert("title", format!("Nested {}", i)).unwrap();
59 b.insert("price", 12.0 + i as f64).unwrap();
60 }
61
62 doc
63 }
64
65 /// Benchmark a single JSONPath query across all sizes
66 fn bench_pattern<F>(c: &mut Criterion, group_name: &str, query: &str, setup: F)
67 where
68 F: Fn(usize) -> LoroDoc,
69 {
70 let mut group = c.benchmark_group(group_name);
71 group.sample_size(if group_name.contains("large") { 20 } else { 50 });
72
73 for &size in &SIZES {
74 let doc = setup(size);
75 let name = format!("{} (size={})", query, size);
76 group.bench_function(&name, |b| {
77 b.iter(|| {
78 let result = doc.jsonpath(query).unwrap();
79 black_box(result);
80 })
81 });
82 }
83 group.finish();
84 }
85
86 pub fn child_selector(c: &mut Criterion) {
87 bench_pattern(
88 c,
89 "Child Selector",
90 "$.store.books[0].title",
91 setup_large_doc,
92 );
93 }
94
95 pub fn wildcard(c: &mut Criterion) {
96 bench_pattern(c, "Wildcard", "$.store.books[*].title", setup_large_doc);
97 }
98
99 pub fn recursive_descent(c: &mut Criterion) {
100 bench_pattern(c, "Recursive Descent", "$..title", setup_large_doc);
101 }
102
103 pub fn quoted_keys(c: &mut Criterion) {
104 bench_pattern(
105 c,
106 "Quoted Keys",
107 "$.store['books'][0]['title']",
108 setup_large_doc,
109 );
110 }
111
112 pub fn string_filter(c: &mut Criterion) {
113 bench_pattern(
114 c,
115 "String Filter",
116 "$.store.books[?(@.title == '1984')].title",
117 setup_large_doc,
118 );
119 }
120
121 pub fn logical_operator(c: &mut Criterion) {
122 bench_pattern(
123 c,
124 "Logical Operator",
125 "$.store.books[?(@.author == 'George Orwell' && @.price < 10)].title",
126 setup_large_doc,
127 );
128 }
129
130 pub fn union_operation(c: &mut Criterion) {
131 bench_pattern(
132 c,
133 "Union Operation",
134 "$.store.books[0,2].title",
135 setup_large_doc,
136 );
137 }
138
139 pub fn slice_operation(c: &mut Criterion) {
140 let mut group = c.benchmark_group("Slice Operation");
141
142 for &size in &SIZES {
143 let doc = setup_large_doc(size);
144
145 // Small slice
146 group.bench_function(format!("$.store.books[0:3].title (size={})", size), |b| {
147 b.iter(|| {
148 let result = doc.jsonpath("$.store.books[0:3].title").unwrap();
149 black_box(result);
150 })
151 });
152
153 // Large slice (half)
154 let half = size / 2;
155 group.bench_function(
156 format!("$.store.books[0:{}].title (size={})", half, size),
157 |b| {
158 b.iter(|| {
159 let result = doc
160 .jsonpath(&format!("$.store.books[0:{}].title", half))
161 .unwrap();
162 black_box(result);
163 })
164 },
165 );
166
167 // Negative slice
168 group.bench_function(format!("$.store.books[-10:].title (size={})", size), |b| {
169 b.iter(|| {
170 let result = doc.jsonpath("$.store.books[-10:].title").unwrap();
171 black_box(result);
172 })
173 });
174 }
175 group.finish();
176 }
177
178 pub fn complex_filter(c: &mut Criterion) {
179 bench_pattern(
180 c,
181 "Complex Filter",
182 "$.store.books[?(@.price >= 10 && @.available == true && @.title contains '1984')].title",
183 setup_large_doc,
184 );
185 }
186
187 pub fn recursive_mapped_filter(c: &mut Criterion) {
188 bench_pattern(
189 c,
190 "Recursive Mapped Filter",
191 "$.store.books[?(@.price > 10)].title",
192 setup_large_doc,
193 );
194 }
195
196 pub fn recursive_filter(c: &mut Criterion) {
197 bench_pattern(
198 c,
199 "Recursive Filter",
200 "$..[?(@.price > 10)].title",
201 setup_large_doc,
202 );
203 }
204
205 // === Legacy / Extra: Keep your original ones (now size-scaled) ===
206
207 pub fn index_selector(c: &mut Criterion) {
208 let mut group = c.benchmark_group("Index Selector");
209 group.sample_size(50);
210
211 for &size in &SIZES {
212 let doc = setup_large_doc(size);
213 let idx = (size as i64 / 2).min(42); // Safe index
214 group.bench_function(format!("positive index {} (size={})", idx, size), |b| {
215 b.iter(|| {
216 let result = doc
217 .jsonpath(&format!("$.store.books[{}].title", idx))
218 .unwrap();
219 black_box(result);
220 })
221 });
222
223 group.bench_function(format!("negative index -10 (size={})", size), |b| {
224 b.iter(|| {
225 let result = doc.jsonpath("$.store.books[-10].title").unwrap();
226 black_box(result);
227 })
228 });
229 }
230 group.finish();
231 }
232
233 pub fn union_key_selector(c: &mut Criterion) {
234 let mut group = c.benchmark_group("Union Key Selector");
235 group.sample_size(50);
236
237 for &size in &SIZES {
238 let doc = setup_large_doc(size);
239 group.bench_function(format!("two keys single item (size={})", size), |b| {
240 b.iter(|| {
241 let result = doc.jsonpath("$.store.books[42]['title','author']").unwrap();
242 black_box(result);
243 })
244 });
245
246 group.bench_function(format!("three keys slice (size={})", size), |b| {
247 b.iter(|| {
248 let result = doc
249 .jsonpath("$.store.books[0:10]['title','author','price']")
250 .unwrap();
251 black_box(result);
252 })
253 });
254 }
255 group.finish();
256 }
257
258 pub fn mixed_selectors(c: &mut Criterion) {
259 bench_pattern(
260 c,
261 "Mixed Selectors",
262 "$.store.books[0,5:15,20].title",
263 setup_large_doc,
264 );
265 }
266
267 pub fn real_world_patterns(c: &mut Criterion) {
268 let mut group = c.benchmark_group("Real World Patterns");
269 group.sample_size(50);
270
271 for &size in &SIZES {
272 let doc = setup_large_doc(size);
273 group.bench_function(format!("last 10 items (size={})", size), |b| {
274 b.iter(|| {
275 let result = doc.jsonpath("$.store.books[-10:].title").unwrap();
276 black_box(result);
277 })
278 });
279
280 group.bench_function(format!("pagination page 1 (size={})", size), |b| {
281 b.iter(|| {
282 let result = doc.jsonpath("$.store.books[0:20].title").unwrap();
283 black_box(result);
284 })
285 });
286 }
287 group.finish();
288 }
289
290 // === Master Group ===
291 pub fn all_benches(c: &mut Criterion) {
292 child_selector(c);
293 wildcard(c);
294 recursive_descent(c);
295 quoted_keys(c);
296 string_filter(c);
297 logical_operator(c);
298 union_operation(c);
299 slice_operation(c);
300 complex_filter(c);
301 recursive_mapped_filter(c);
302 recursive_filter(c);
303 index_selector(c);
304 union_key_selector(c);
305 mixed_selectors(c);
306 real_world_patterns(c);
307 }
308}
309
310pub fn dumb(_c: &mut Criterion) {}
311
312#[cfg(feature = "test_utils")]
313criterion_group!(benches, jsonpath::all_benches);
314#[cfg(not(feature = "test_utils"))]
315criterion_group!(benches, dumb);
316
317criterion_main!(benches);