Bucket in SQLite, draw the build-time bootstrap once, start sccache's server

Speed: the chart's counts come from GROUP BY over the bucket number, so
about 120 rows per table per range reach Rust instead of every saved row;
a range total is one indexed SUM. History counts the rows it hands out.

Build times: crates/stats gains Resampled, which draws the bootstrap once
for a set of languages and applies it to any number of hit sets; the
server keeps it and redraws only when an observation was added. The
views fill build_times, time saved and the 90% interval from the stats
crate, and the time-saved arithmetic of the server is gone.

Starting sccache: when nothing listens where the client looks
(SCCACHE_SERVER_UDS, or 127.0.0.1 at SCCACHE_SERVER_PORT), sccache
--start-server is run once and the counters read again; a failure is
reported as NotRunning and not retried for a minute.

Uses the api's Range::ALL, parse and label.
This commit is contained in:
swaits committed 2026-11-28 11:57:10 -07:00
1 parent 90a6302149
commit 16ba94517b
7 files changed
+1523 -473

No files matched your search

+152 -33
View File
@@ -7,10 +7,10 @@ use std::path::PathBuf;
use std::sync::Arc;
use std::thread;
use sccache_dash::app::{unix_now, App, Config};
use sccache_dash::app::{unix_now, App, Config, Probe, Prober, Task};
use sccache_dash::history::History;
use sccache_dash::stats;
use sccache_dash_api::{Range, Status, Totals, View};
use sccache_dash_api::{Range, Severity, Status, Totals, View};
const LIVE: &str = include_str!("../../../fixtures/live-0.18.0.json");
const ZEROED: &str = "{\"stats\":{\"compile_requests\":0,\"cache_hits\":{\"counts\":{},\"adv_counts\":{}},\"cache_misses\":{\"counts\":{},\"adv_counts\":{}},\"cache_errors\":{\"counts\":{},\"adv_counts\":{}}},\"version\":\"0.18.0\"}";
@@ -36,6 +36,7 @@ printf '%s\n' "$*" >> "$0.log"
case "$1" in
--version) echo 'sccache 0.18.0' ;;
--zero-stats) cp "$0.zeroed" "$0.out" ;;
--start-server) if [ -e "$0.no-start" ]; then echo "Server startup failed: Address in use" >&2; exit 1; fi; cp "$0.live" "$0.out"; touch "$0.running"; echo started ;;
*) cat "$0.out" ;;
esac
"#,
@@ -43,6 +44,8 @@ esac
.unwrap();
std::fs::set_permissions(&script, std::fs::Permissions::from_mode(0o755)).unwrap();
std::fs::write(dir.path().join("sccache.out"), LIVE).unwrap();
std::fs::write(dir.path().join("sccache.live"), LIVE).unwrap();
std::fs::write(dir.path().join("sccache.running"), "").unwrap();
std::fs::write(dir.path().join("sccache.zeroed"), ZEROED).unwrap();
Fake {
log: dir.path().join("sccache.log"),
@@ -66,6 +69,24 @@ esac
.collect()
}
/// No server running: `--show-stats` then answers with empty counters, as the real one does.
fn down(&self) {
std::fs::remove_file(self.dir.path().join("sccache.running")).unwrap();
self.answers(ZEROED);
}
/// The stand-in for the check on whether sccache's server is listening.
fn probe(&self) -> Prober {
let marker = self.dir.path().join("sccache.running");
Arc::new(move || {
if marker.exists() {
Probe::Running
} else {
Probe::NotRunning
}
})
}
fn config(&self) -> Config {
Config {
sccache: self.script.clone().into(),
@@ -76,15 +97,15 @@ esac
/// A server on a free port next to an empty history, polled once, answering in the background.
/// The test stands in for the polling task: it holds the history and polls by hand.
fn start(fake: &Fake) -> (Arc<App>, History, SocketAddr) {
let app = Arc::new(App::new(fake.config()));
let history = History::memory().unwrap();
app.poll_at(&history, NOW);
fn start(fake: &Fake) -> (Arc<App>, Task, SocketAddr) {
let app = Arc::new(App::with_probe(fake.config(), fake.probe()));
let mut task = Task::memory();
task.poll(&app, NOW);
let server = tiny_http::Server::http("127.0.0.1:0").unwrap();
let addr = server.server_addr().to_ip().unwrap();
let answering = Arc::clone(&app);
thread::spawn(move || sccache_dash::http::answer(server, answering));
(app, history, addr)
(app, task, addr)
}
/// Sends one raw request (`{port}` stands for the server's port) and returns the whole response.
@@ -119,7 +140,7 @@ fn zero(addr: SocketAddr, host: Option<&str>, origin: Option<&str>) -> (u16, Str
#[test]
fn zero_is_refused_for_a_foreign_origin_or_host() {
let fake = Fake::new();
let (app, history, addr) = start(&fake);
let (app, task, addr) = start(&fake);
// A page anywhere else, in any shape: a bare host, another port, or "null".
for origin in [
@@ -154,7 +175,7 @@ fn zero_is_refused_for_a_foreign_origin_or_host() {
fake.calls()
);
assert_eq!(
history.row_count().unwrap(),
task.history.row_count().unwrap(),
0,
"and nothing was written to the history"
);
@@ -167,7 +188,7 @@ fn zero_is_refused_for_a_foreign_origin_or_host() {
#[test]
fn zero_from_the_dashboard_zeroes_sccache_and_keeps_the_history() {
let fake = Fake::new();
let (app, history, addr) = start(&fake);
let (app, mut task, addr) = start(&fake);
let (code, response) = zero(
addr,
@@ -186,24 +207,24 @@ fn zero_from_the_dashboard_zeroes_sccache_and_keeps_the_history() {
);
// What the polling task then does, woken by the request: it reads the counters again.
app.poll_at(&history, unix_now());
task.poll(&app, unix_now());
let calls = fake.calls();
assert_eq!(calls.iter().filter(|c| *c == "--zero-stats").count(), 2);
assert_eq!(calls.last().unwrap(), "--show-stats --stats-format json");
assert_eq!(
history.row_count().unwrap(),
task.history.row_count().unwrap(),
1,
"the history stays, and the zeroing is one more row"
);
let row = &history.rows(0, i64::MAX).unwrap()[0];
let row = &task.history.rows(0, i64::MAX).unwrap()[0];
assert!(row.reset, "the zeroing shows in the history as a reset");
assert_eq!(
row.delta,
stats::Counters::default(),
"nothing has been compiled since"
);
assert_eq!(history.last_reset().unwrap(), Some(row.t));
assert_eq!(task.history.last_reset().unwrap(), Some(row.t));
let v = app.view(Range::SinceReset);
assert_eq!(
@@ -221,7 +242,7 @@ fn zero_from_the_dashboard_zeroes_sccache_and_keeps_the_history() {
#[test]
fn view_export_and_the_page_answer() {
let fake = Fake::new();
let (_app, _history, addr) = start(&fake);
let (_app, _task, addr) = start(&fake);
let response = send(addr, &format!("GET /api/view?range=reset{GET}"));
assert!(
@@ -243,9 +264,7 @@ fn view_export_and_the_page_answer() {
),
(Some("0.18.0"), Some("0.18.0"))
);
assert!(v
.summary_markdown
.starts_with("**sccache, since last reset**\n"));
assert!(v.summary_markdown.starts_with("**sccache, since reset**\n"));
for range in ["1h", "24h", "7d", "30d"] {
let response = send(addr, &format!("GET /api/view?range={range}{GET}"));
let v: View = serde_json::from_str(response.split("\r\n\r\n").nth(1).unwrap()).unwrap();
@@ -287,38 +306,45 @@ fn view_export_and_the_page_answer() {
#[test]
fn rows_are_saved_when_counters_change_and_at_most_once_a_minute() {
let fake = Fake::new();
let app = App::new(fake.config());
let history = History::memory().unwrap();
let rows = || history.row_count().unwrap();
let app = App::with_probe(fake.config(), fake.probe());
let mut task = Task::memory();
app.poll_at(&history, NOW);
assert_eq!(rows(), 0, "the first reading is a baseline, not a row");
app.poll_at(&history, NOW + 10);
assert_eq!(rows(), 0, "nothing changed");
task.poll(&app, NOW);
assert_eq!(
task.history.row_count().unwrap(),
0,
"the first reading is a baseline, not a row"
);
task.poll(&app, NOW + 10);
assert_eq!(task.history.row_count().unwrap(), 0, "nothing changed");
fake.answers(&LIVE.replace("\"compile_requests\":7351", "\"compile_requests\":7352"));
app.poll_at(&history, NOW + 30);
task.poll(&app, NOW + 30);
assert_eq!(
rows(),
task.history.row_count().unwrap(),
0,
"changed, but less than a minute after the baseline"
);
app.poll_at(&history, NOW + 60);
assert_eq!(rows(), 1, "a minute on, the change is saved");
let row = &history.rows(0, i64::MAX).unwrap()[0];
task.poll(&app, NOW + 60);
assert_eq!(
task.history.row_count().unwrap(),
1,
"a minute on, the change is saved"
);
let row = &task.history.rows(0, i64::MAX).unwrap()[0];
assert_eq!(
(row.t, row.delta.totals.requests, row.reset),
(NOW + 60, 1, false)
);
app.poll_at(&history, NOW + 700);
task.poll(&app, NOW + 700);
assert_eq!(
rows(),
task.history.row_count().unwrap(),
1,
"unchanged: nothing more is saved, however long the quiet lasts"
);
assert!(
history.build_observations().unwrap().is_empty(),
task.history.build_observations().unwrap().is_empty(),
"and with no misses there is no build observation"
);
assert_eq!(app.lock().views.len(), 5, "every poll makes the five views");
@@ -406,3 +432,96 @@ fn old_rows_are_deleted_at_startup() {
"WAL mode is on"
);
}
#[test]
fn a_missing_sccache_server_is_started_once_then_read() {
let fake = Fake::new();
fake.down();
let app = App::with_probe(fake.config(), fake.probe());
let mut task = Task::memory();
task.poll(&app, NOW);
let calls = fake.calls();
assert_eq!(
calls.iter().filter(|c| *c == "--start-server").count(),
1,
"{calls:?}"
);
assert_eq!(
calls.last().unwrap(),
"--show-stats --stats-format json",
"the counters are read again after starting"
);
let v = app.view(Range::SinceReset);
assert_eq!(v.server.status, Status::Idle, "{:?}", v.server.error);
assert_eq!(
v.totals,
stats::parse(LIVE).unwrap().counters.totals,
"the started server's counters, not the empty ones"
);
task.poll(&app, NOW + 10);
assert_eq!(
fake.calls()
.iter()
.filter(|c| *c == "--start-server")
.count(),
1,
"not started again while it is running"
);
}
#[test]
fn a_server_that_will_not_start_is_reported_and_not_retried_for_a_minute() {
let fake = Fake::new();
fake.down();
std::fs::write(fake.dir.path().join("sccache.no-start"), "").unwrap();
let app = App::with_probe(fake.config(), fake.probe());
let mut task = Task::memory();
for at in [NOW, NOW + 10, NOW + 50] {
task.poll(&app, at);
let v = app.view(Range::Day);
assert_eq!(v.server.status, Status::NotRunning, "polled at {at}");
assert!(
v.server
.error
.as_deref()
.is_some_and(|e| e.contains("`sccache --start-server` failed")),
"{:?}",
v.server.error
);
assert!(
v.problems
.iter()
.any(|p| p.severity == Severity::Error && p.title == "sccache is not running"),
"{:?}",
v.problems
);
}
assert_eq!(
fake.calls()
.iter()
.filter(|c| *c == "--start-server")
.count(),
1,
"tried once"
);
task.poll(&app, NOW + 60);
assert_eq!(
fake.calls()
.iter()
.filter(|c| *c == "--start-server")
.count(),
2,
"and again a minute later"
);
// Meanwhile the server came up by itself (a compile started it): no more error. Its counters
// differ from the empty ones, so that counts as compiles having arrived.
std::fs::write(fake.dir.path().join("sccache.running"), "").unwrap();
fake.answers(LIVE);
task.poll(&app, NOW + 70);
assert_eq!(app.view(Range::Day).server.status, Status::Running);
assert_eq!(app.view(Range::Day).server.error, None);
}