Skip to content

Commit af0d4e6

Browse files
committed
Support automated UX testing
1 parent 5dd7e0e commit af0d4e6

File tree

4 files changed

+419
-3
lines changed

4 files changed

+419
-3
lines changed

Cargo.lock

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

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ unicode-segmentation = "1.9.0"
3636
unicode-width = "0.1.9"
3737

3838
[dev-dependencies]
39+
alacritty-test = { git = "https://github.com/YizhePKU/alacritty-test.git", rev = "2f63ebe" }
3940
gethostname = "0.4.0"
4041
pretty_assertions = "1.4.0"
4142
rstest = { version = "0.18.0", default-features = false }

examples/typing_latency.rs

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
//! Measure the typing latency of Reedline with default configurations.
2+
3+
use alacritty_test::{pty_spawn, EventedReadWrite, Terminal};
4+
use reedline::{DefaultPrompt, Reedline, Signal};
5+
use std::{
6+
io::Write,
7+
time::{Duration, Instant},
8+
};
9+
10+
fn child() -> ! {
11+
let mut line_editor = Reedline::create();
12+
let prompt = DefaultPrompt::default();
13+
14+
loop {
15+
let sig = line_editor.read_line(&prompt).unwrap();
16+
match sig {
17+
Signal::Success(buffer) => {
18+
println!("We processed: {buffer}");
19+
}
20+
_ => std::process::exit(-1),
21+
}
22+
}
23+
}
24+
25+
fn main() -> std::io::Result<()> {
26+
if let Some(arg) = std::env::args().nth(1) {
27+
if arg == "--child" {
28+
child();
29+
}
30+
}
31+
32+
let mut pty = pty_spawn(
33+
"target/debug/examples/typing_latency",
34+
vec!["--child"],
35+
None,
36+
)?;
37+
let mut terminal = Terminal::new();
38+
terminal.read_from_pty(&mut pty, Some(Duration::from_millis(50)))?;
39+
40+
// Test latency of a single keystroke.
41+
let mut total_latency = Duration::from_millis(0);
42+
for loop_cnt in 1.. {
43+
let old_cursor = terminal.inner().grid().cursor.point;
44+
45+
// input a single keystroke
46+
pty.writer().write_all(b"A").unwrap();
47+
48+
let start_time = Instant::now();
49+
loop {
50+
// measure with 10us accuracy
51+
terminal.read_from_pty(&mut pty, Some(Duration::from_micros(10)))?;
52+
53+
let new_cursor = terminal.inner().grid().cursor.point;
54+
if new_cursor.column > old_cursor.column {
55+
break;
56+
}
57+
}
58+
total_latency += start_time.elapsed();
59+
60+
println!(
61+
"single keystroke latency = {:.2}ms, averaging over {loop_cnt} iterations",
62+
(total_latency.as_millis() as f64) / (loop_cnt as f64),
63+
);
64+
65+
// delete the keystroke
66+
pty.writer().write_all(b"\x7f\x7f\x7f\x7f").unwrap();
67+
terminal.read_from_pty(&mut pty, Some(Duration::from_millis(50)))?;
68+
}
69+
70+
Ok(())
71+
}

0 commit comments

Comments
 (0)