use std::path::PathBuf;
use std::time::{Duration, Instant};
use cpp_code_analysis::{DiskFiles, OpenDocuments, Session, SessionFiles, WatchFilter};
fn project() -> PathBuf {
PathBuf::from(std::env::var("CPPLS_REAL_PROJECT").unwrap_or_else(|_| r"E:\clice".to_string()))
}
fn entry(root: &std::path::Path) -> PathBuf {
std::env::var("CPPLS_KEYSTROKE_FILE")
.map(PathBuf::from)
.map(|file| {
if file.is_absolute() {
file
} else {
root.join(file)
}
})
.unwrap_or_else(|_| root.join("src/AST/Selection.cpp"))
}
fn summary(times: &[Duration]) -> String {
let mut millis: Vec<u128> = times.iter().map(Duration::as_millis).collect();
if millis.is_empty() {
return "no steps".to_string();
}
millis.sort_unstable();
format!(
"min {} / median {} / max {} ms",
millis[0],
millis[millis.len() / 2],
millis[millis.len() - 1]
)
}
#[test]
#[ignore = "a probe: opens a real project, settles its index, and prints instead of asserting"]
fn what_a_keystroke_costs() {
let root = project();
let file = entry(&root);
let original = std::fs::read_to_string(&file).expect("the file reads");
let mut session = Session::open(
root.clone(),
SessionFiles::new(OpenDocuments::new(), DiskFiles),
WatchFilter::new(&root),
);
session.add_project_files([file.clone()]);
let indexing = Instant::now();
session.index_everything();
let mut slices = 0;
while session.pending_work() > 0 && slices < 10_000 {
session.advance(64);
slices += 1;
}
println!(
"settled in {:?} after {slices} extra slice(s); units {:?}",
indexing.elapsed(),
session.unit_stats()
);
session.did_open(&file, &original);
let opened = Instant::now();
let steps = session.advance(1);
let open_step = steps.iter().find(|step| step.path == file);
println!(
"didOpen + re-read: {:?} | environment {} | units {:?}",
opened.elapsed(),
open_step.is_some_and(|step| step.with_the_environment),
session.unit_stats()
);
let mut body_times = Vec::new();
let mut body_rendered = 0usize;
let mut text = original.clone();
for round in 0..10 {
text.push_str(&format!("// typed {round}\n"));
session.did_change(&file, &text);
let started = Instant::now();
let steps = session.advance(1);
body_times.push(started.elapsed());
let step = steps.iter().find(|step| step.path == file);
if step.is_some_and(|step| step.with_the_environment) {
body_rendered += 1;
}
}
println!("body keystrokes, 10x: {}", summary(&body_times));
println!(
"{body_rendered} of 10 read as the program; units {:?}",
session.unit_stats()
);
let mut directive_times = Vec::new();
let mut directive_rendered = 0usize;
for round in 0..10 {
let edited = format!("{text}#define PAD_{round} 1\n");
session.did_change(&file, &edited);
let started = Instant::now();
let steps = session.advance(1);
directive_times.push(started.elapsed());
if steps
.iter()
.find(|step| step.path == file)
.is_some_and(|step| step.with_the_environment)
{
directive_rendered += 1;
}
text = edited;
}
println!("directive keystrokes, 10x: {}", summary(&directive_times));
println!(
"{directive_rendered} of 10 read as the program; units {:?}",
session.unit_stats()
);
let sizes = session.cache_sizes();
println!(
"cache afterwards: {} summary(ies), {} unit(s), {} fact(s)",
sizes.summaries, sizes.units, sizes.facts
);
std::thread::sleep(std::time::Duration::from_millis(1_200));
let before = session.cache_sizes().cooked_declarations;
let started = Instant::now();
let read = session.read_a_program_now();
println!(
"a paused reader: {:?} for {:?} file(s), +{} cooked declaration(s) now queryable",
started.elapsed(),
read.as_ref().map(|reading| reading.files),
session
.cache_sizes()
.cooked_declarations
.saturating_sub(before)
);
if let Some(reading) = read {
println!(
" {} token(s), {} error(s), {} unplaced, {} unbalanced",
reading.tokens,
reading.errors,
reading.unplaced,
reading.unbalanced.len()
);
}
}