Skip to content

Commit

Permalink
refactor: adjust measure time macro
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianWoelki committed Aug 19, 2024
1 parent fdc34a5 commit 3099fa2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
10 changes: 5 additions & 5 deletions src/benchmark/macros/measure_time.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#[macro_export]
macro_rules! measure_time {
($func:expr) => {{
($block:block) => {{
let start = Instant::now();
let result = $func;
let end = Instant::now();
let duration = end.duration_since(start);
let result = $block;
let duration = start.elapsed();
(result, duration)
}};
}
Expand All @@ -24,7 +24,7 @@ mod tests {

#[test]
fn test_measure_time_zero_duration() {
let (result, duration) = measure_time!(42);
let (result, duration) = measure_time!({ 42 });
assert_eq!(result, 42);
assert!(duration < Duration::from_millis(1));
}
Expand Down
25 changes: 13 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ use std::{
};

use benchmark::{
logger::BenchmarkLogger, macros::measure_system::ResourceReport, BenchmarkConfig,
GenericBenchmarkResult, IndexBenchmarkResult,
logger::BenchmarkLogger,
macros::{measure_system::ResourceReport, measure_time},
BenchmarkConfig, GenericBenchmarkResult, IndexBenchmarkResult,
};
use chrono::Local;
use data::{
Expand Down Expand Up @@ -248,13 +249,13 @@ async fn main() {
// TODO: Add benchmark for measuring application of dimensionality reduction techniques to data.

// Benchmark for saving to disk.
let save_time_start = Instant::now();
let saved_file = save_index(
&dir_path,
format!("annoy_serial_{}", amount), // TODO: Modify to support parallel
IndexType::Annoy(index),
);
let total_save_duration = save_time_start.elapsed();
let (saved_file, total_save_duration) = measure_time!({
save_index(
&dir_path,
format!("annoy_serial_{}", amount), // TODO: Modify to support parallel
IndexType::Annoy(index),
)
});

let index_disk_space = saved_file
.metadata()
Expand All @@ -263,9 +264,9 @@ async fn main() {
/ (1024.0 * 1024.0); // in mb

// Benchmark loading time for index.
let load_time_start = Instant::now();
AnnoyIndex::load_index(&saved_file);
let total_load_duration = load_time_start.elapsed();
let (_, total_load_duration) = measure_time!({
AnnoyIndex::load_index(&saved_file);
});

index_logger.add_record(IndexBenchmarkResult {
execution_time: total_index_duration.as_secs_f32(),
Expand Down

0 comments on commit 3099fa2

Please sign in to comment.