Skip to content

Commit

Permalink
refactor: clean up things and add first benchmarking check
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianWoelki committed Aug 15, 2024
1 parent 6b8e629 commit e841bc9
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 162 deletions.
98 changes: 44 additions & 54 deletions src/benchmark/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use serde::Serialize;

use crate::{
data::SparseVector,
index::{IndexType, SparseIndex},
index::{DistanceMetric, IndexType, SparseIndex},
};

pub mod logger;
Expand Down Expand Up @@ -41,13 +41,17 @@ pub struct BenchmarkConfig {
/// any single element in the data vectors can take. This is crucial for
/// generating test data with realistic variability.
pub value_range: (f32, f32),
pub sparsity: f32,
pub distance_metric: DistanceMetric,
}

impl BenchmarkConfig {
pub fn new(
dimensions_range: (usize, usize, usize),
num_images_range: (usize, usize, usize),
value_range: (f32, f32),
sparsity: f32,
distance_metric: DistanceMetric,
) -> Self {
BenchmarkConfig {
start_dimensions: dimensions_range.0,
Expand All @@ -57,6 +61,8 @@ impl BenchmarkConfig {
end_num_images: num_images_range.1,
step_num_images: num_images_range.2,
value_range,
sparsity,
distance_metric,
}
}

Expand All @@ -82,58 +88,42 @@ pub struct BenchmarkResult {
pub scalability_factor: Option<f32>, // Optional because the first benchmark doesn't have a previous result to compare to.
}

pub struct Benchmark {
index_type: Box<IndexType>,
query_vector: SparseVector,
previous_benchmark_result: Option<BenchmarkResult>,
}

impl Benchmark {
pub fn new(
index_type: Box<IndexType>,
query_vector: SparseVector,
previous_benchmark_result: Option<BenchmarkResult>,
) -> Self {
Benchmark {
index_type,
query_vector,
previous_benchmark_result,
}
}

pub fn run(&mut self, dataset_size: usize, dimensions: usize, k: usize) -> BenchmarkResult {
let start_time = Instant::now();

// Builds the index.
self.index_type.build();
let index_execution_time = start_time.elapsed();

// Perform the query.
let _query_results = self.index_type.search(&self.query_vector, k);
let query_execution_time = start_time.elapsed() - index_execution_time;

let total_execution_time = start_time.elapsed();

let queries_per_second = metrics::calculate_queries_per_second(query_execution_time);

let scalability_factor = self
.previous_benchmark_result
.as_ref()
.map(|previous_result| {
metrics::calculate_scalability_factor(
(queries_per_second, dataset_size, dimensions),
previous_result,
)
});

BenchmarkResult {
total_execution_time,
index_execution_time,
query_execution_time,
queries_per_second,
scalability_factor,
dataset_size,
dataset_dimensionality: dimensions,
}
pub fn measure_benchmark(
index_type: &mut IndexType,
query_vector: &SparseVector,
previous_result: Option<BenchmarkResult>,
dataset_size: usize,
dimensions: usize,
k: usize,
) -> BenchmarkResult {
let start_time = Instant::now();

// Builds the index.
index_type.build();
let index_execution_time = start_time.elapsed();

// Perform the query.
let _query_results = index_type.search(&query_vector, k);
let query_execution_time = start_time.elapsed() - index_execution_time;

let total_execution_time = start_time.elapsed();

let queries_per_second = metrics::calculate_queries_per_second(query_execution_time);

let scalability_factor = previous_result.as_ref().map(|previous_result| {
metrics::calculate_scalability_factor(
(queries_per_second, dataset_size, dimensions),
previous_result,
)
});

BenchmarkResult {
total_execution_time,
index_execution_time,
query_execution_time,
queries_per_second,
scalability_factor,
dataset_size,
dataset_dimensionality: dimensions,
}
}
2 changes: 1 addition & 1 deletion src/index/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub mod neighbor;
pub mod nsw;
pub mod pq;

#[derive(PartialEq, Serialize, Deserialize)]
#[derive(PartialEq, Serialize, Deserialize, Copy, Clone)]
pub enum DistanceMetric {
Euclidean,
Cosine,
Expand Down
Loading

0 comments on commit e841bc9

Please sign in to comment.