forked from lance-format/lance-graph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph_execution.rs
More file actions
340 lines (308 loc) · 11.8 KB
/
Copy pathgraph_execution.rs
File metadata and controls
340 lines (308 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright The Lance Authors
//! Graph query execution benchmarks with actual data processing
//!
//! This benchmark measures ACTUAL query execution performance:
//! - Creates Arrow datasets
//! - Executes Cypher queries
//! - Processes query results
//! - Full end-to-end execution!
//!
//! Run with:
//! ```
//! cargo bench --bench graph_execution
//! ```
use std::collections::HashMap;
use std::sync::Arc;
use arrow_array::{Int32Array, RecordBatch, RecordBatchIterator, StringArray};
use arrow_schema::{DataType, Field, Schema as ArrowSchema};
use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
use futures::TryStreamExt;
use lance::dataset::{Dataset, WriteMode, WriteParams};
use lance_graph::{CypherQuery, GraphConfig};
use tempfile::TempDir;
fn create_people_batch() -> RecordBatch {
let schema = Arc::new(ArrowSchema::new(vec![
Field::new("person_id", DataType::Int32, false),
Field::new("name", DataType::Utf8, false),
Field::new("age", DataType::Int32, false),
]));
RecordBatch::try_new(
schema,
vec![
Arc::new(Int32Array::from(vec![1, 2, 3, 4, 5])),
Arc::new(StringArray::from(vec![
"Alice", "Bob", "Carol", "David", "Eve",
])),
Arc::new(Int32Array::from(vec![28, 34, 29, 42, 31])),
],
)
.unwrap()
}
fn create_friendship_batch() -> RecordBatch {
let schema = Arc::new(ArrowSchema::new(vec![
Field::new("person1_id", DataType::Int32, false),
Field::new("person2_id", DataType::Int32, false),
Field::new("friendship_type", DataType::Utf8, false),
]));
RecordBatch::try_new(
schema,
vec![
Arc::new(Int32Array::from(vec![1, 1, 2, 3, 4])),
Arc::new(Int32Array::from(vec![2, 3, 4, 4, 5])),
Arc::new(StringArray::from(vec![
"close", "casual", "close", "casual", "close",
])),
],
)
.unwrap()
}
// Execute query using CypherQuery::execute against in-memory batches
fn execute_cypher_query(
rt: &tokio::runtime::Runtime,
q: &CypherQuery,
datasets: HashMap<String, RecordBatch>,
) -> RecordBatch {
rt.block_on(async move { q.execute(datasets, None).await.unwrap() })
}
fn make_people_batch(n: usize) -> RecordBatch {
if n == 5 {
return create_people_batch();
}
let schema = Arc::new(ArrowSchema::new(vec![
Field::new("person_id", DataType::Int32, false),
Field::new("name", DataType::Utf8, false),
Field::new("age", DataType::Int32, false),
]));
let ids: Vec<i32> = (0..n as i32).collect();
let names: Vec<String> = (0..n).map(|i| format!("name_{}", i)).collect();
let ages: Vec<i32> = (0..n as i32).map(|i| 20 + (i % 60)).collect();
RecordBatch::try_new(
schema,
vec![
Arc::new(Int32Array::from(ids)),
Arc::new(StringArray::from(names)),
Arc::new(Int32Array::from(ages)),
],
)
.unwrap()
}
fn make_friendship_batch(n: usize) -> RecordBatch {
if n == 5 {
return create_friendship_batch();
}
let schema = Arc::new(ArrowSchema::new(vec![
Field::new("person1_id", DataType::Int32, false),
Field::new("person2_id", DataType::Int32, false),
Field::new("friendship_type", DataType::Utf8, false),
]));
let src: Vec<i32> = (0..n as i32).collect();
let dst: Vec<i32> = (0..n as i32).map(|i| (i + 1) % n as i32).collect();
let ftype: Vec<&str> = std::iter::repeat_n("friend", n).collect();
RecordBatch::try_new(
schema,
vec![
Arc::new(Int32Array::from(src)),
Arc::new(Int32Array::from(dst)),
Arc::new(StringArray::from(ftype)),
],
)
.unwrap()
}
fn bench_cypher_execution(c: &mut Criterion) {
let mut group = c.benchmark_group("cypher_execution");
let sizes = [100usize, 10_000usize, 1_000_000usize];
// Global runtime reused across iterations
let rt = tokio::runtime::Runtime::new().unwrap();
// Helper function to create graph config
let make_config = || {
GraphConfig::builder()
.with_node_label("Person", "person_id")
.with_relationship("FRIEND_OF", "person1_id", "person2_id")
.build()
.unwrap()
};
// Prebuild queries (reuse per iteration)
let q_basic = CypherQuery::new("MATCH (n:Person) WHERE n.age > 50 RETURN n.name")
.unwrap()
.with_config(make_config());
let q_single_hop = CypherQuery::new("MATCH (a:Person)-[:FRIEND_OF]->(b:Person) RETURN b.name")
.unwrap()
.with_config(make_config());
let q_two_hop = CypherQuery::new(
"MATCH (a:Person)-[:FRIEND_OF]->(b:Person)-[:FRIEND_OF]->(c:Person) RETURN c.name",
)
.unwrap()
.with_config(make_config());
// Prebuild small (100) datasets once and reuse
let person_small = make_people_batch(100);
let friendship_small = make_friendship_batch(100);
// Persist medium datasets once and reuse
let (_medium_tmpdir, person_medium, friendship_medium): (TempDir, RecordBatch, RecordBatch) = {
let tmpdir = tempfile::tempdir().unwrap();
let person_path = tmpdir.path().join("person.lance");
let friend_path = tmpdir.path().join("friendship.lance");
let person_b = make_people_batch(10_000);
let friend_b = make_friendship_batch(10_000);
rt.block_on(async {
Dataset::write(
RecordBatchIterator::new(vec![Ok(person_b.clone())].into_iter(), person_b.schema()),
person_path.to_str().unwrap(),
Some(WriteParams {
mode: WriteMode::Create,
..Default::default()
}),
)
.await
.unwrap();
Dataset::write(
RecordBatchIterator::new(vec![Ok(friend_b.clone())].into_iter(), friend_b.schema()),
friend_path.to_str().unwrap(),
Some(WriteParams {
mode: WriteMode::Create,
..Default::default()
}),
)
.await
.unwrap();
let person_ds = Dataset::open(person_path.to_str().unwrap()).await.unwrap();
let p_batches = person_ds
.scan()
.try_into_stream()
.await
.unwrap()
.try_collect::<Vec<_>>()
.await
.unwrap();
let person_one = p_batches.into_iter().next().unwrap();
let friend_ds = Dataset::open(friend_path.to_str().unwrap()).await.unwrap();
let f_batches = friend_ds
.scan()
.try_into_stream()
.await
.unwrap()
.try_collect::<Vec<_>>()
.await
.unwrap();
let friend_one = f_batches.into_iter().next().unwrap();
(tmpdir, person_one, friend_one)
})
};
// Persist large (1_000_000) datasets once and reuse
let (_large_tmpdir, person_large, friendship_large): (TempDir, RecordBatch, RecordBatch) = {
let tmpdir = tempfile::tempdir().unwrap();
let person_path = tmpdir.path().join("person.lance");
let friend_path = tmpdir.path().join("friendship.lance");
let person_b = make_people_batch(1_000_000);
let friend_b = make_friendship_batch(1_000_000);
rt.block_on(async {
Dataset::write(
RecordBatchIterator::new(vec![Ok(person_b.clone())].into_iter(), person_b.schema()),
person_path.to_str().unwrap(),
Some(WriteParams {
mode: WriteMode::Create,
..Default::default()
}),
)
.await
.unwrap();
Dataset::write(
RecordBatchIterator::new(vec![Ok(friend_b.clone())].into_iter(), friend_b.schema()),
friend_path.to_str().unwrap(),
Some(WriteParams {
mode: WriteMode::Create,
..Default::default()
}),
)
.await
.unwrap();
let person_ds = Dataset::open(person_path.to_str().unwrap()).await.unwrap();
let p_batches = person_ds
.scan()
.try_into_stream()
.await
.unwrap()
.try_collect::<Vec<_>>()
.await
.unwrap();
let person_one = p_batches.into_iter().next().unwrap();
let friend_ds = Dataset::open(friend_path.to_str().unwrap()).await.unwrap();
let f_batches = friend_ds
.scan()
.try_into_stream()
.await
.unwrap()
.try_collect::<Vec<_>>()
.await
.unwrap();
let friend_one = f_batches.into_iter().next().unwrap();
(tmpdir, person_one, friend_one)
})
};
// 1) Basic node filter + projection
for &n in &sizes {
group.throughput(Throughput::Elements(n as u64));
group.bench_with_input(BenchmarkId::new("basic_node_filter", n), &n, |b, &n| {
b.iter(|| {
let people_batch = match n {
100 => person_small.clone(),
10_000 => person_medium.clone(),
_ => person_large.clone(),
};
let mut ds = HashMap::new();
ds.insert("Person".to_string(), people_batch);
let out = execute_cypher_query(&rt, &q_basic, ds);
black_box(out.num_rows());
})
});
}
// 2) Single-hop relationship expansion
for &n in &sizes {
group.throughput(Throughput::Elements(n as u64));
group.bench_with_input(BenchmarkId::new("single_hop_expand", n), &n, |b, &n| {
b.iter(|| {
let people_batch = match n {
100 => person_small.clone(),
10_000 => person_medium.clone(),
_ => person_large.clone(),
};
let friendship = match n {
100 => friendship_small.clone(),
10_000 => friendship_medium.clone(),
_ => friendship_large.clone(),
};
let mut ds = HashMap::new();
ds.insert("Person".to_string(), people_batch);
ds.insert("FRIEND_OF".to_string(), friendship);
let out = execute_cypher_query(&rt, &q_single_hop, ds);
black_box(out.num_rows());
})
});
}
// 3) Two-hop relationship expansion
for &n in &sizes {
group.throughput(Throughput::Elements(n as u64));
group.bench_with_input(BenchmarkId::new("two_hop_expand", n), &n, |b, &n| {
b.iter(|| {
let people_batch = match n {
100 => person_small.clone(),
10_000 => person_medium.clone(),
_ => person_large.clone(),
};
let friendship = match n {
100 => friendship_small.clone(),
10_000 => friendship_medium.clone(),
_ => friendship_large.clone(),
};
let mut ds = HashMap::new();
ds.insert("Person".to_string(), people_batch);
ds.insert("FRIEND_OF".to_string(), friendship);
let out = execute_cypher_query(&rt, &q_two_hop, ds);
black_box(out.num_rows());
})
});
}
group.finish();
}
criterion_group!(benches, bench_cypher_execution);
criterion_main!(benches);