325 lines
15 KiB
Rust
325 lines
15 KiB
Rust
//! What a scan says it spent its time on (`docs/features/perf5-p4/README.md`).
|
|
//!
|
|
//! Three claims, one test each: the lines exist only under `--verbose`, the
|
|
//! counts on them are the work the scan actually did, and the stages add up
|
|
//! to the scan they describe.
|
|
|
|
use super::*;
|
|
|
|
/// A tree of `files` one-package files.
|
|
pub(super) fn trace_tree(tag: &str, files: usize) -> Tree {
|
|
let tree = Tree::new(tag);
|
|
for n in 0..files {
|
|
tree.package(&format!("t{n:03}.sv"), &format!("t{n:03}"));
|
|
}
|
|
tree
|
|
}
|
|
|
|
/// Every `timing: scan …` line the server has said since the last drain.
|
|
pub(super) fn scan_lines(session: &Session) -> Vec<String> {
|
|
session
|
|
.logged()
|
|
.into_iter()
|
|
.filter(|(typ, text)| *typ == MessageType::LOG && is_trace_line(text))
|
|
.map(|(_, text)| text)
|
|
.collect()
|
|
}
|
|
|
|
/// Whether a log line is one of a scan's three trace lines — `timing: scan
|
|
/// <number> …` — rather than the `scan started`/`scan finished` pair the
|
|
/// verbose-timing feature already had.
|
|
pub(super) fn is_trace_line(text: &str) -> bool {
|
|
let Some(rest) = text.strip_prefix("timing: scan ") else { return false };
|
|
rest.split_whitespace().next().is_some_and(|word| word.parse::<u32>().is_ok())
|
|
}
|
|
|
|
/// The one line of `lines` whose prefix says it is that kind.
|
|
pub(super) fn line_of(lines: &[String], kind: &str) -> String {
|
|
lines
|
|
.iter()
|
|
.find(|line| line.contains(&format!(" {kind} (")))
|
|
.unwrap_or_else(|| panic!("no {kind} line among {lines:#?}"))
|
|
.clone()
|
|
}
|
|
|
|
/// The value of `key=` on a `key=value` line, as text.
|
|
pub(super) fn field<'a>(line: &'a str, key: &str) -> &'a str {
|
|
let at = line.find(&format!("{key}=")).unwrap_or_else(|| panic!("no {key} on {line}"));
|
|
let rest = &line[at + key.len() + 1..];
|
|
rest.split_whitespace().next().unwrap_or("")
|
|
}
|
|
|
|
/// The value of `key=` as a number.
|
|
fn number(line: &str, key: &str) -> f64 {
|
|
let text = field(line, key);
|
|
text.parse().unwrap_or_else(|_| panic!("{key}={text} is not a number on {line}"))
|
|
}
|
|
|
|
#[test]
|
|
fn without_the_flag_a_scan_says_nothing_about_its_stages() {
|
|
// The shipped state. `Session::wide` is every other test in this module,
|
|
// and none of them may start seeing three extra log lines per scan.
|
|
let tree = trace_tree("tracequiet", 12);
|
|
let session = Session::wide(vec![tree.path.clone()], false);
|
|
assert!(scan_lines(&session).is_empty(), "verbose is off: {:#?}", session.logged());
|
|
}
|
|
|
|
#[test]
|
|
fn the_flag_makes_a_scan_say_what_it_spent_and_what_it_left() {
|
|
let tree = trace_tree("traceverbose", 12);
|
|
let session = Session::verbose_wide(vec![tree.path.clone()], None);
|
|
let lines = scan_lines(&session);
|
|
// Three lines, and they name the same scan.
|
|
let stages = line_of(&lines, "stages");
|
|
let counts = line_of(&lines, "counts");
|
|
let memory = line_of(&lines, "memory");
|
|
for line in [&stages, &counts, &memory] {
|
|
assert!(line.starts_with("timing: scan 1 "), "{line}");
|
|
assert!(line.contains("whole"), "the startup scan is a whole scan: {line}");
|
|
}
|
|
// The counts are this fixture's, exactly: twelve files walked and read,
|
|
// none skipped, none open, and no store to hit.
|
|
assert_eq!(field(&counts, "read"), "12");
|
|
assert_eq!(field(&counts, "skipped"), "0");
|
|
assert_eq!(field(&counts, "hits"), "0");
|
|
assert_eq!(field(&counts, "misses"), "12");
|
|
assert_eq!(field(&counts, "buffers"), "0");
|
|
// Twelve files is under `INTERLEAVE_ABOVE`, so the scan is one shot and
|
|
// nothing is published mid-way.
|
|
assert_eq!(field(&counts, "publishes"), "0");
|
|
assert_eq!(field(&stages, "since_start"), field(&stages, "since_start"));
|
|
// And the index it left is those twelve packages.
|
|
assert_eq!(field(&memory, "index_files"), "12");
|
|
assert_eq!(field(&memory, "cache_entries"), "0", "no storage path, so no store");
|
|
}
|
|
|
|
#[test]
|
|
fn a_warm_second_session_reports_every_file_as_a_hit() {
|
|
// The one claim the whole warm-scan column rests on: `hits` counts
|
|
// records served for exactly these bytes, and `misses` counts the parses.
|
|
let tree = trace_tree("tracewarm", 9);
|
|
let storage = std::env::temp_dir().join(format!("lucisv_trace_store_{}", std::process::id()));
|
|
let _ = std::fs::remove_dir_all(&storage);
|
|
let cold = Session::verbose_wide(vec![tree.path.clone()], Some(storage.clone()));
|
|
let cold_lines = scan_lines(&cold);
|
|
let cold_counts = line_of(&cold_lines, "counts");
|
|
assert_eq!(field(&cold_counts, "hits"), "0");
|
|
assert_eq!(field(&cold_counts, "misses"), "9");
|
|
assert_eq!(field(&cold_counts, "records"), "9", "a cold scan writes every record through");
|
|
drop(cold);
|
|
|
|
let warm = Session::verbose_wide(vec![tree.path.clone()], Some(storage.clone()));
|
|
let warm_lines = scan_lines(&warm);
|
|
let warm_counts = line_of(&warm_lines, "counts");
|
|
assert_eq!(field(&warm_counts, "hits"), "9", "the same bytes, so every lookup hits");
|
|
assert_eq!(field(&warm_counts, "misses"), "0");
|
|
assert_eq!(field(&warm_counts, "records"), "0", "a hit writes nothing back");
|
|
let warm_memory = line_of(&warm_lines, "memory");
|
|
assert_eq!(field(&warm_memory, "cache_entries"), "9");
|
|
assert!(number(&warm_counts, "decoded_kib") >= 0.0);
|
|
drop(warm);
|
|
let _ = std::fs::remove_dir_all(&storage);
|
|
}
|
|
|
|
#[test]
|
|
fn the_stages_add_up_to_the_scan_they_describe() {
|
|
// The tolerance claim: what the trace attributes must be most of the scan
|
|
// and can never be more than all of it. The slack is the loop itself —
|
|
// the progress callbacks, the per-file word `Vec`, the staging pushes —
|
|
// which is deliberately unattributed rather than silently folded into a
|
|
// neighbouring stage.
|
|
let tree = trace_tree("tracesums", 40);
|
|
let session = Session::verbose_wide(vec![tree.path.clone()], None);
|
|
let stages = line_of(&scan_lines(&session), "stages");
|
|
let total = number(&stages, "total");
|
|
let named = [
|
|
"discovery",
|
|
"order",
|
|
// Since 2026-09-08 a scan reads a file's text at the step that indexes
|
|
// it, so what `discovery` used to spend on reading the whole scope is
|
|
// this stage instead — and a list without it accounts for a fifth of
|
|
// the scan (`docs/features/perf5-p4/README.md` §P4b3).
|
|
"read",
|
|
"cache_open",
|
|
"lookup",
|
|
"decode",
|
|
"parse",
|
|
"encode",
|
|
"store_write",
|
|
"publish_absorb",
|
|
"publish_candidates",
|
|
"publish_includes",
|
|
"publish_overlay",
|
|
"gone",
|
|
"absorb",
|
|
"compact",
|
|
"candidates",
|
|
"includes",
|
|
"overlay",
|
|
];
|
|
let sum: f64 = named.iter().map(|key| number(&stages, key)).sum();
|
|
assert!(total > 0.0, "a scan of 40 files takes measurable time: {stages}");
|
|
assert!(sum <= total * 1.02, "the stages cannot exceed the scan: {sum} of {total}");
|
|
assert!(sum >= total * 0.5, "the stages must account for most of it: {sum} of {total}");
|
|
}
|
|
|
|
#[test]
|
|
fn a_rescan_is_a_second_scan_with_its_own_three_lines() {
|
|
// A session pays for more than one scan — the startup scan runs at the
|
|
// default settings and whatever the client then says can ask for another
|
|
// (`docs/features/perf5-p4/README.md`, the two-scan startup). Each gets
|
|
// its own numbered triple so no reader adds two scans together.
|
|
let tree = trace_tree("tracerescan", 8);
|
|
let mut session = Session::verbose_wide(vec![tree.path.clone()], None);
|
|
let first = scan_lines(&session);
|
|
assert_eq!(first.len(), 3, "one scan, three lines: {first:#?}");
|
|
|
|
session.notify(
|
|
DidChangeConfiguration::METHOD,
|
|
serde_json::json!({ "settings": { "lucisv": {
|
|
"index": { "scope": "workspace" },
|
|
"exclude": ["**/t000.sv"],
|
|
} } }),
|
|
);
|
|
let second = scan_lines(&session);
|
|
assert_eq!(second.len(), 3, "the rescan says the same three things: {second:#?}");
|
|
for line in &second {
|
|
assert!(line.starts_with("timing: scan 2 "), "{line}");
|
|
}
|
|
let counts = line_of(&second, "counts");
|
|
assert_eq!(field(&counts, "read"), "7", "the exclusion is one file fewer");
|
|
}
|
|
|
|
/// A `--verbose` session at the **default** settings, against a client that
|
|
/// cannot be asked for its settings at all — the shape every pre-2026-08-27
|
|
/// editor starts in, and the one the startup staging leaves untouched
|
|
/// (`server/startup.rs`). There is nothing to wait for, so the startup scan
|
|
/// runs immediately at the defaults: with no buffer open its folder scope is
|
|
/// empty and so is its work list.
|
|
fn verbose_default(roots: Vec<PathBuf>, storage: Option<PathBuf>) -> Session {
|
|
let (server_end, client) = Connection::memory();
|
|
let startup = Startup {
|
|
abilities: ClientAbilities::default(),
|
|
client: ClientSettings::default(),
|
|
storage,
|
|
verbose: true,
|
|
queued: Vec::new(),
|
|
wait: Duration::ZERO,
|
|
};
|
|
let server = Server::started(server_end, roots, startup);
|
|
Session { server, client }
|
|
}
|
|
|
|
/// Ask for `lucisv.index.scope: "workspace"`, which is the rescan the pulled
|
|
/// configuration asks for in a real session.
|
|
fn widen_to_the_workspace(session: &mut Session) {
|
|
session.notify(
|
|
DidChangeConfiguration::METHOD,
|
|
serde_json::json!({ "settings": { "lucisv": { "index": { "scope": "workspace" } } } }),
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn a_startup_scan_with_no_files_opens_no_store_and_a_warm_rescan_rewrites_none() {
|
|
// §P4b, both halves, over the real dispatch.
|
|
//
|
|
// **(b1)** the startup scan indexes nothing, so it opens nothing: warm it
|
|
// used to read a 44 MiB store and rewrite it byte-identically, 184 ms of
|
|
// a 1.33 s start, for a scan with no files in it. **(a)** the configured
|
|
// rescan behind it hits every record, appends nothing, and so would
|
|
// rewrite the store it just read into itself — which it no longer does.
|
|
let tree = trace_tree("traceemptyscan", 9);
|
|
let storage = std::env::temp_dir().join(format!("lucisv_trace_empty_{}", std::process::id()));
|
|
let _ = std::fs::remove_dir_all(&storage);
|
|
let store = storage.join("index-cache");
|
|
|
|
// Cold: scan 1 is empty and touches nothing, scan 2 fills the store.
|
|
let mut cold = verbose_default(vec![tree.path.clone()], Some(storage.clone()));
|
|
let first = line_of(&scan_lines(&cold), "counts");
|
|
assert_eq!(field(&first, "read"), "0", "premise: the startup scan indexes nothing");
|
|
assert_eq!(field(&first, "opens"), "0", "and so reads no store");
|
|
assert_eq!(field(&first, "compactions"), "0", "and rewrites none");
|
|
assert!(!store.exists(), "a scan with no files does not even create one");
|
|
|
|
widen_to_the_workspace(&mut cold);
|
|
let filled = line_of(&scan_lines(&cold), "counts");
|
|
assert_eq!(field(&filled, "read"), "9", "the configured rescan indexes the tree");
|
|
assert_eq!(field(&filled, "opens"), "1", "which does open the store");
|
|
assert_eq!(field(&filled, "records"), "9");
|
|
assert_eq!(field(&filled, "compactions"), "1", "a session that appended must rewrite");
|
|
drop(cold);
|
|
|
|
let bytes = std::fs::read(&store).expect("a store");
|
|
let stamp = std::fs::metadata(&store).expect("meta").modified().expect("mtime");
|
|
|
|
// Warm: the same two scans, and neither writes a byte — with a buffer
|
|
// open, which is the shape a real session has. An open buffer is indexed
|
|
// from its own analysis and writes its record through on every scan, and
|
|
// that write-through is what would otherwise put a duplicate line in the
|
|
// store and owe the rewrite back.
|
|
let mut warm = verbose_default(vec![tree.path.clone()], Some(storage.clone()));
|
|
let empty = line_of(&scan_lines(&warm), "counts");
|
|
assert_eq!(field(&empty, "read"), "0");
|
|
assert_eq!(field(&empty, "opens"), "0", "still no store read for a scan with no files");
|
|
assert_eq!(field(&empty, "compactions"), "0");
|
|
|
|
let opened = tree.path.join("t000.sv");
|
|
let text = std::fs::read_to_string(&opened).expect("read");
|
|
warm.open(&opened, &text);
|
|
let _ = scan_lines(&warm);
|
|
|
|
widen_to_the_workspace(&mut warm);
|
|
let warmed = line_of(&scan_lines(&warm), "counts");
|
|
assert_eq!(field(&warmed, "hits"), "8", "every file but the buffer came out of the store");
|
|
assert_eq!(field(&warmed, "buffers"), "1", "premise: the buffer indexes itself");
|
|
assert_eq!(field(&warmed, "repeats"), "1", "and its record was already in the store");
|
|
assert_eq!(field(&warmed, "records"), "0", "so nothing was appended");
|
|
assert_eq!(field(&warmed, "opens"), "1", "the store was read once, not twice");
|
|
assert_eq!(field(&warmed, "compactions"), "0", "and the rewrite would have been identical");
|
|
drop(warm);
|
|
|
|
assert_eq!(std::fs::read(&store).expect("a store"), bytes, "not one byte of the store moved");
|
|
assert_eq!(
|
|
std::fs::metadata(&store).expect("meta").modified().expect("mtime"),
|
|
stamp,
|
|
"and the file was not touched at all"
|
|
);
|
|
let _ = std::fs::remove_dir_all(&storage);
|
|
}
|
|
|
|
#[test]
|
|
fn the_discovery_split_is_a_split_of_discovery_and_not_stages_beside_it() {
|
|
// §61.3. `config`, `walk`, `filelist`, `sort`, `cap` and `buffers` are
|
|
// disjoint parts of `discovery`, so they may never add up to more than it
|
|
// — and anything that sums the scan (`the_stages_add_up_to_the_scan_they
|
|
// _describe`) must not sum these as well, which is why they are absent
|
|
// from that test's list. The slack is `collect_sources`'s own bookkeeping:
|
|
// the `spoken_for` set, the `Vec` pushes, the `Scope` moves.
|
|
let tree = trace_tree("tracediscovery", 30);
|
|
let session = Session::verbose_wide(vec![tree.path.clone()], None);
|
|
let lines = scan_lines(&session);
|
|
let stages = line_of(&lines, "stages");
|
|
let discovery = number(&stages, "discovery");
|
|
let parts: f64 = ["config", "walk", "filelist", "sort", "cap", "buffers"]
|
|
.iter()
|
|
.map(|key| number(&stages, key))
|
|
.sum();
|
|
assert!(discovery > 0.0, "a walk of 30 files takes measurable time: {stages}");
|
|
assert!(parts <= discovery * 1.02, "the parts cannot exceed the whole: {parts} of {discovery}");
|
|
assert!(parts > 0.0, "and at least one of them was timed: {stages}");
|
|
// Not asserted: that the parts account for *most* of discovery. On a
|
|
// fixture this small the whole is well under a millisecond and the
|
|
// bookkeeping slack — the `spoken_for` set, the `Vec` pushes — rivals the
|
|
// walk, so a ratio bound is a stopwatch under the workspace suite's
|
|
// parallel load (it tripped once at 50 % in the lead's gate run and
|
|
// passed alone three times). On opentitan the walk is 95 % of discovery
|
|
// (`docs/features/perf5r-first-answer/README.md`); that ratio is the
|
|
// README's measurement, not this test's claim.
|
|
assert_eq!(number(&stages, "filelist"), 0.0, "no filelist is in force: {stages}");
|
|
// Nothing was walked twice: one root, and the cap read the walk's own
|
|
// lengths rather than asking the filesystem again.
|
|
let counts = line_of(&lines, "counts");
|
|
assert_eq!(field(&counts, "walk_dirs"), "1", "one root, walked once: {counts}");
|
|
assert_eq!(field(&counts, "stats"), "0", "the cap stats nothing the walk already read");
|
|
}
|