|
| 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