Skip to content

Commit 1201cae

Browse files
authored
perf(search): benchmark smart sort (#2202)
1 parent fd3aca7 commit 1201cae

File tree

7 files changed

+100
-3
lines changed

7 files changed

+100
-3
lines changed

Cargo.lock

+51
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/atuin-history/Cargo.toml

+9
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,12 @@ tracing = "0.1"
3939
uuid = { workspace = true }
4040
unicode-segmentation = "1.11.0"
4141
sysinfo = "0.30.7"
42+
43+
[dev-dependencies]
44+
tracing-tree = "0.3"
45+
divan = "0.1.14"
46+
rand = { workspace = true }
47+
48+
[[bench]]
49+
name = "smart_sort"
50+
harness = false
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use atuin_client::history::History;
2+
use atuin_history::sort::sort;
3+
4+
use rand::Rng;
5+
6+
fn main() {
7+
// Run registered benchmarks.
8+
divan::main();
9+
}
10+
11+
// Smart sort usually runs on 200 entries, test on a few sizes
12+
#[divan::bench(args=[100, 200, 400, 800, 1600, 10000])]
13+
fn smart_sort(lines: usize) {
14+
// benchmark a few different sizes of "history"
15+
// first we need to generate some history. This will use a whole bunch of memory, sorry
16+
let mut rng = rand::thread_rng();
17+
let now = time::OffsetDateTime::now_utc().unix_timestamp();
18+
19+
let possible_commands = ["echo", "ls", "cd", "grep", "atuin", "curl"];
20+
let mut commands = Vec::<History>::with_capacity(lines);
21+
22+
for _ in 0..lines {
23+
let command = possible_commands[rng.gen_range(0..possible_commands.len())];
24+
25+
let command = History::import()
26+
.command(command)
27+
.timestamp(time::OffsetDateTime::from_unix_timestamp(rng.gen_range(0..now)).unwrap())
28+
.build()
29+
.into();
30+
31+
commands.push(command);
32+
}
33+
34+
let _ = sort("curl", commands);
35+
}

crates/atuin-history/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
pub mod sort;
12
pub mod stats;

crates/atuin/src/command/client/search.rs

-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ mod engines;
2121
mod history_list;
2222
mod inspector;
2323
mod interactive;
24-
mod sort;
2524

2625
pub use duration::format_duration_into;
2726

crates/atuin/src/command/client/search/interactive.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ use super::{
3131
cursor::Cursor,
3232
engines::{SearchEngine, SearchState},
3333
history_list::{HistoryList, ListState, PREFIX_LENGTH},
34-
sort,
3534
};
3635

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

9897
if smart_sort {
99-
Ok(sort::sort(self.search.input.as_str(), results))
98+
Ok(atuin_history::sort::sort(
99+
self.search.input.as_str(),
100+
results,
101+
))
100102
} else {
101103
Ok(results)
102104
}

0 commit comments

Comments
 (0)