Skip to content

Commit dec8af0

Browse files
authored
chore: add save and get collection bench (#92)
2 parents c147dc8 + 0db9fef commit dec8af0

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

bench/main.rs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,40 @@ criterion_group!(
7070
bench_insert_to_collection
7171
);
7272

73-
criterion_main!(collection);
73+
fn bench_save_collection_to_database(criterion: &mut Criterion) {
74+
let id = "save collection to database";
75+
76+
// Setup the database and collection.
77+
let collection = build_test_collection(DIMENSION, COLLECTION_SIZE);
78+
let mut db = create_test_database(DIMENSION, COLLECTION_SIZE);
79+
80+
// Benchmark the save speed.
81+
criterion.bench_function(id, |bencher| {
82+
bencher.iter(|| {
83+
black_box(db.save_collection("bench", &collection).unwrap());
84+
})
85+
});
86+
}
87+
88+
fn bench_get_collection_from_database(criterion: &mut Criterion) {
89+
let id = "get collection from database";
90+
let db = create_test_database(DIMENSION, COLLECTION_SIZE);
91+
92+
// Benchmark the get speed.
93+
// This is the operation that loads the collection into memory.
94+
let routine = || {
95+
black_box(db.get_collection("bench").unwrap());
96+
};
97+
98+
criterion.bench_function(id, |b| b.iter(routine));
99+
}
100+
101+
criterion_group! {
102+
name = database;
103+
config = Criterion::default().sample_size(10);
104+
targets =
105+
bench_save_collection_to_database,
106+
bench_get_collection_from_database
107+
}
108+
109+
criterion_main!(collection, database);

bench/utils.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,13 @@ pub fn build_test_collection(dimension: usize, len: usize) -> Collection {
88
let config = Config::default();
99
Collection::build(&config, &records).unwrap()
1010
}
11+
12+
/// Creates a pre-populated database with a collection for testing.
13+
/// * `dimension`: Dimensionality of the vector embeddings
14+
/// * `size`: Number of records in the collection
15+
pub fn create_test_database(dimension: usize, size: usize) -> Database {
16+
let collection = build_test_collection(dimension, size);
17+
let mut db = Database::new("data/bench").unwrap();
18+
db.save_collection("bench", &collection).unwrap();
19+
db
20+
}

0 commit comments

Comments
 (0)