Skip to content

Commit

Permalink
perf(search): benchmark smart sort (#2202)
Browse files Browse the repository at this point in the history
  • Loading branch information
ellie authored Jun 26, 2024
1 parent fd3aca7 commit 1201cae
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 3 deletions.
51 changes: 51 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions crates/atuin-history/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,12 @@ tracing = "0.1"
uuid = { workspace = true }
unicode-segmentation = "1.11.0"
sysinfo = "0.30.7"

[dev-dependencies]
tracing-tree = "0.3"
divan = "0.1.14"
rand = { workspace = true }

[[bench]]
name = "smart_sort"
harness = false
35 changes: 35 additions & 0 deletions crates/atuin-history/benches/smart_sort.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use atuin_client::history::History;
use atuin_history::sort::sort;

use rand::Rng;

fn main() {
// Run registered benchmarks.
divan::main();
}

// Smart sort usually runs on 200 entries, test on a few sizes
#[divan::bench(args=[100, 200, 400, 800, 1600, 10000])]
fn smart_sort(lines: usize) {
// benchmark a few different sizes of "history"
// first we need to generate some history. This will use a whole bunch of memory, sorry
let mut rng = rand::thread_rng();
let now = time::OffsetDateTime::now_utc().unix_timestamp();

let possible_commands = ["echo", "ls", "cd", "grep", "atuin", "curl"];
let mut commands = Vec::<History>::with_capacity(lines);

for _ in 0..lines {
let command = possible_commands[rng.gen_range(0..possible_commands.len())];

let command = History::import()
.command(command)
.timestamp(time::OffsetDateTime::from_unix_timestamp(rng.gen_range(0..now)).unwrap())
.build()
.into();

commands.push(command);
}

let _ = sort("curl", commands);
}
1 change: 1 addition & 0 deletions crates/atuin-history/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod sort;
pub mod stats;
File renamed without changes.
1 change: 0 additions & 1 deletion crates/atuin/src/command/client/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ mod engines;
mod history_list;
mod inspector;
mod interactive;
mod sort;

pub use duration::format_duration_into;

Expand Down
6 changes: 4 additions & 2 deletions crates/atuin/src/command/client/search/interactive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ use super::{
cursor::Cursor,
engines::{SearchEngine, SearchState},
history_list::{HistoryList, ListState, PREFIX_LENGTH},
sort,
};

use crate::{command::client::search::engines, VERSION};
Expand Down Expand Up @@ -96,7 +95,10 @@ impl State {
self.results_len = results.len();

if smart_sort {
Ok(sort::sort(self.search.input.as_str(), results))
Ok(atuin_history::sort::sort(
self.search.input.as_str(),
results,
))
} else {
Ok(results)
}
Expand Down

0 comments on commit 1201cae

Please sign in to comment.