Merge pull request #1276 from ludovic111/feat/telemetry-live
🤖 Agent: merged after verify passed. telemetry.playhorizon.dev is live on Railway (service telemetry in horizon-site, volume /data): /healthz, /v1/live (a test ping counted) and /v1/benchmarks/summary (CORS for playhorizon.dev) answer. The PR's local end-to-end runs are in the description.
This commit is contained in:
@@ -2,8 +2,8 @@
|
||||
//! launcher's "N playing now". While the game runs, a thread of its own sends a random session
|
||||
//! ID made here and kept only in memory, the version, the platform and whether a LAN or
|
||||
//! play-with-friends world is open, every minute and at once when that last one changes.
|
||||
//! It sends nothing when there is no server (`HORIZON_TELEMETRY_URL`, none by default) or
|
||||
//! with `HORIZON_NO_HEARTBEAT=1` (automated runs).
|
||||
//! It sends nothing when sending is turned off (`HORIZON_TELEMETRY_URL=off`) or with
|
||||
//! `HORIZON_NO_HEARTBEAT=1` (automated runs).
|
||||
use std::sync::OnceLock;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::thread::Thread;
|
||||
|
||||
@@ -5,7 +5,7 @@ This server receives what players choose to send (performance summaries, crash a
|
||||
cargo run --release # in this folder: its own workspace, like the launcher's
|
||||
PORT=8080 HORIZON_TELEMETRY_DB=/data/telemetry.sqlite3 horizon-telemetry-server
|
||||
|
||||
The launcher sends to it when `HORIZON_TELEMETRY_URL` is set (and to `DEFAULT_ENDPOINT` in `crates/horizon-telemetry` once there is one).
|
||||
It runs at **https://telemetry.playhorizon.dev** (`DEFAULT_ENDPOINT` in `crates/horizon-telemetry`). The game and the launcher use it unless `HORIZON_TELEMETRY_URL` names another server, or is `off` (tests and automated runs send nothing).
|
||||
|
||||
## Endpoints
|
||||
|
||||
@@ -14,11 +14,34 @@ The launcher sends to it when `HORIZON_TELEMETRY_URL` is set (and to `DEFAULT_EN
|
||||
| `POST /v1/batch` | A zstd batch (`Content-Encoding: zstd`, at most 256 KiB, 1 MiB inflated, 64 items). Returns 204. Returns 400 for a bad batch, 413 for one too large, 415 if it is not zstd, 429 when over a limit. |
|
||||
| `DELETE /v1/installs/<id>` | Deletes everything that install sent. Its share of the daily totals is rebuilt at once. |
|
||||
| `GET /v1/aggregate?days=30` | The totals the website reads. Cached 10 minutes, with `Access-Control-Allow-Origin` set to the website. |
|
||||
| `GET /v1/benchmarks/summary` | Players' benchmark uploads grouped by GPU, CPU, render distance and game, for the website's Benchmarks page (below). Cached 10 minutes, same `Access-Control-Allow-Origin`. |
|
||||
| `POST /v1/live` | A game's "I'm playing" ping (JSON, at most 512 bytes). Returns 204. Returns 400 for a bad ping, 413 for one too large, 429 over the address's limit, 503 when it holds `LIVE_MAX_SESSIONS` already. |
|
||||
| `GET /v1/live` | How many play now, rounded: `playing`, `lan`, `versions`, `platforms`. Made again at most every 15 seconds, with the same `Access-Control-Allow-Origin`. |
|
||||
| `GET /healthz` | `ok` |
|
||||
|
||||
The totals are grouped per version, OS, GPU vendor and render distance. Each group has its sessions, install-days, hours played, the 14 frame-time buckets summed, and the mean p50 and p99 frame times. Groups with fewer than 5 install-days are left out. The totals also list crash counts per version and the newest 50 benchmark uploads.
|
||||
The totals are grouped per version, OS, GPU vendor and render distance. Each group has its sessions, install-days, hours played, the 14 frame-time buckets summed, and the mean p50 and p99 frame times. Groups with fewer than 5 install-days are left out. The totals also list crash counts per version. Benchmark uploads are published only through the summary below, never one by one.
|
||||
|
||||
## Benchmarks summary
|
||||
|
||||
`src/summary.rs` makes it with the same rules as `website/scripts/benchmarks.mjs`, which bakes the page's fallback copy; both are tested against the same fixtures (`website/scripts/fixtures/benchmarks/`).
|
||||
|
||||
- Only reports that compare: a version 1 `horizon-benchmark-report` of the standard flight over the four scenes, VSync off, no frame cap, on screen, from Horizon or plain vanilla, naming its GPU and CPU.
|
||||
- A group (GPU, CPU, render distance, game) is published only with **at least 3 different installs**. Each install counts once: the median of its runs, then the median across installs.
|
||||
- Out go the names, the counts, the newest three versions and the medians. Never an install ID, a day, an OS version, a driver, a memory size or a screen.
|
||||
|
||||
```json
|
||||
{
|
||||
"format": "horizon-benchmark-summary", "version": 1, "generated": "2026-10-22T20:00:00Z", "min_installs": 3,
|
||||
"groups": [{
|
||||
"gpu": "NVIDIA GeForce RTX 4080", "cpu": "13th Gen Intel Core i7-13700KF", "render_distance": 12,
|
||||
"game": "horizon", "source": "players", "installs": 3, "reports": 4, "versions": ["v0.2.1", "v0.2.0"],
|
||||
"medians": { "avg_fps": 515, "low_1pct_fps": 120, "p95_ms": 3.5, "world_ms": 4500,
|
||||
"chunk_load_ms": 1400, "rss_peak_mb": 1450, "rss_steady_mb": 1250 }
|
||||
}]
|
||||
}
|
||||
```
|
||||
|
||||
Frame rates are whole numbers, milliseconds have one decimal under 10, megabytes are whole. Groups come most installs first. A deleted install leaves its groups at once.
|
||||
|
||||
## Storage
|
||||
|
||||
@@ -45,7 +68,7 @@ All are environment variables:
|
||||
- `HORIZON_TELEMETRY_MIN_INSTALL_DAYS` (5)
|
||||
- `HORIZON_TELEMETRY_REQUESTS_PER_HOUR` per address (60, bursts of 6)
|
||||
- `HORIZON_TELEMETRY_ITEMS_PER_DAY` per install (100)
|
||||
- `HORIZON_TELEMETRY_TRUST_PROXY=1` behind a proxy (the client's address is then the first of `X-Forwarded-For`)
|
||||
- `HORIZON_TELEMETRY_TRUST_PROXY=1` behind a proxy (the client's address is then the proxy's `X-Real-IP`, else the last `X-Forwarded-For` entry; it is used only for the rate limits, in memory, and never stored or logged)
|
||||
- `HORIZON_TELEMETRY_ALLOW_ORIGIN` (`https://playhorizon.dev`)
|
||||
- `HORIZON_TELEMETRY_LIVE_PER_HOUR` per address, pings and reads (600, bursts of 60: ten players behind one address)
|
||||
- `HORIZON_TELEMETRY_LIVE_WINDOW_SECONDS` (120)
|
||||
@@ -55,6 +78,14 @@ All are environment variables:
|
||||
|
||||
`Dockerfile` builds it from the repository root (`docker build -f crates/horizon-telemetry-server/Dockerfile .`). The database goes on a volume mounted at `/data`.
|
||||
|
||||
On Railway it is a service of the same project as the website:
|
||||
|
||||
- Source: this repository, root directory `/` (the Dockerfile needs the shared crate), config file `/crates/horizon-telemetry-server/railway.json` (Dockerfile build, health check `/healthz`, one replica in `europe-west4`, Amsterdam: one SQLite file can't be shared).
|
||||
- A volume mounted at `/data`. The Dockerfile already sets `HORIZON_TELEMETRY_DB=/data/telemetry.sqlite3` and `HORIZON_TELEMETRY_TRUST_PROXY=1`; Railway sets `PORT`.
|
||||
- The custom domain `telemetry.playhorizon.dev`: a `CNAME` at Porkbun to the target Railway gives.
|
||||
|
||||
Check it with `curl https://telemetry.playhorizon.dev/healthz`.
|
||||
|
||||
At Railway's prices (RAM $10/GB-month, CPU $20/vCPU-month, volumes $0.15/GB-month, egress $0.05/GB), one more service with a 1 GB volume uses about $0.35 a month:
|
||||
|
||||
- RAM about 15 MB: $0.15
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"$schema": "https://railway.com/railway.schema.json",
|
||||
"build": {
|
||||
"builder": "DOCKERFILE",
|
||||
"dockerfilePath": "crates/horizon-telemetry-server/Dockerfile",
|
||||
"watchPatterns": [
|
||||
"/crates/horizon-telemetry/**",
|
||||
"/crates/horizon-telemetry-server/**",
|
||||
"/Cargo.toml"
|
||||
]
|
||||
},
|
||||
"deploy": {
|
||||
"healthcheckPath": "/healthz",
|
||||
"healthcheckTimeout": 60,
|
||||
"restartPolicyType": "ON_FAILURE",
|
||||
"restartPolicyMaxRetries": 5,
|
||||
"multiRegionConfig": {
|
||||
"europe-west4-drams3a": { "numReplicas": 1 }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,8 @@
|
||||
//! - `POST /v1/batch`: a zstd batch from the launcher, checked by `horizon_telemetry::batch`.
|
||||
//! - `DELETE /v1/installs/<id>`: everything an install sent goes.
|
||||
//! - `GET /v1/aggregate?days=30`: the anonymous totals the website reads, cached.
|
||||
//! - `GET /v1/benchmarks/summary`: players' benchmark results grouped by GPU and CPU, groups of at
|
||||
//! least 3 installs only (`summary`), cached; the website's Benchmarks page reads it.
|
||||
//! - `POST /v1/live`: a game's anonymous "I'm playing" ping (`live`, in memory only).
|
||||
//! - `GET /v1/live`: how many play now, rounded, cached briefly.
|
||||
//! - `GET /healthz`.
|
||||
@@ -26,6 +28,7 @@ use horizon_telemetry::batch;
|
||||
|
||||
pub mod live;
|
||||
pub mod store;
|
||||
pub mod summary;
|
||||
|
||||
use horizon_telemetry::live::{MAX_PING_BYTES, Ping};
|
||||
use live::Live;
|
||||
@@ -40,7 +43,8 @@ pub struct Config {
|
||||
pub requests_per_hour: f64,
|
||||
/// Items one install may send a day.
|
||||
pub items_per_day: u32,
|
||||
/// Behind a proxy (Railway, Fly…): the client's address is the first of X-Forwarded-For.
|
||||
/// Behind a proxy (Railway, Fly…): the client's address is the proxy's `X-Real-IP`, else the
|
||||
/// last `X-Forwarded-For` entry.
|
||||
pub trust_proxy: bool,
|
||||
/// The website allowed to read the totals from a browser.
|
||||
pub allow_origin: String,
|
||||
@@ -117,6 +121,8 @@ pub struct App {
|
||||
live_cache: Mutex<Option<(Instant, String)>>,
|
||||
/// `/v1/aggregate` answers by `days`, with when they were made.
|
||||
cache: Mutex<HashMap<u32, (Instant, String)>>,
|
||||
/// `/v1/benchmarks/summary`'s answer and when it was made.
|
||||
summary_cache: Mutex<Option<(Instant, String)>>,
|
||||
/// Today, in days since 1970 (a test moves it).
|
||||
today: Box<dyn Fn() -> u32 + Send + Sync>,
|
||||
}
|
||||
@@ -148,6 +154,7 @@ impl App {
|
||||
live: Mutex::new(live),
|
||||
live_cache: Mutex::default(),
|
||||
cache: Mutex::default(),
|
||||
summary_cache: Mutex::default(),
|
||||
today,
|
||||
})
|
||||
}
|
||||
@@ -170,20 +177,26 @@ impl App {
|
||||
self.live.lock().unwrap_or_else(|e| e.into_inner())
|
||||
}
|
||||
|
||||
/// The address requests are counted under, for the rate limits only: kept in memory, never
|
||||
/// stored or logged. Behind a proxy (Railway) the TCP peer is the proxy, so it is the proxy's
|
||||
/// `X-Real-IP`, else the last `X-Forwarded-For` entry: the one the proxy added, which a client
|
||||
/// can't choose (the first one it can).
|
||||
fn client(&self, headers: &HeaderMap, peer: Option<SocketAddr>) -> IpAddr {
|
||||
let header = |name: &str| headers.get(name)?.to_str().ok();
|
||||
let forwarded = self
|
||||
.config
|
||||
.trust_proxy
|
||||
.then(|| {
|
||||
headers
|
||||
.get("x-forwarded-for")?
|
||||
.to_str()
|
||||
.ok()?
|
||||
.split(',')
|
||||
.next()?
|
||||
.trim()
|
||||
.parse()
|
||||
.ok()
|
||||
header("x-real-ip")
|
||||
.and_then(|ip| ip.trim().parse().ok())
|
||||
.or_else(|| {
|
||||
header("x-forwarded-for")?
|
||||
.rsplit(',')
|
||||
.next()?
|
||||
.trim()
|
||||
.parse()
|
||||
.ok()
|
||||
})
|
||||
})
|
||||
.flatten();
|
||||
forwarded
|
||||
@@ -196,6 +209,12 @@ impl App {
|
||||
self.store().newest_day(install).ok().flatten()
|
||||
}
|
||||
|
||||
/// Drops the cached public answers (after a deletion or expiry).
|
||||
fn forget_cached(&self) {
|
||||
self.cache.lock().unwrap_or_else(|e| e.into_inner()).clear();
|
||||
*self.summary_cache.lock().unwrap_or_else(|e| e.into_inner()) = None;
|
||||
}
|
||||
|
||||
/// Daily upkeep: totals rebuilt, expired rows removed, idle rate-limit entries and live
|
||||
/// sessions forgotten.
|
||||
pub fn maintain(&self) -> rusqlite::Result<usize> {
|
||||
@@ -209,7 +228,7 @@ impl App {
|
||||
}
|
||||
self.live().forget_expired(Instant::now());
|
||||
if removed > 0 {
|
||||
self.cache.lock().unwrap_or_else(|e| e.into_inner()).clear();
|
||||
self.forget_cached();
|
||||
}
|
||||
Ok(removed)
|
||||
}
|
||||
@@ -288,7 +307,7 @@ async fn forget(State(app): State<Arc<App>>, Path(id): Path<String>, req: Reques
|
||||
};
|
||||
match app.store().delete_install(&install) {
|
||||
Ok(_) => {
|
||||
app.cache.lock().unwrap_or_else(|e| e.into_inner()).clear();
|
||||
app.forget_cached();
|
||||
StatusCode::NO_CONTENT.into_response()
|
||||
}
|
||||
Err(e) => {
|
||||
@@ -338,6 +357,60 @@ async fn aggregate(State(app): State<Arc<App>>, Query(window): Query<Window>) ->
|
||||
json_response(&app, json, CACHE_FOR.as_secs())
|
||||
}
|
||||
|
||||
/// `GET /v1/benchmarks/summary`: players' benchmark groups (`summary`), made again at most every
|
||||
/// ten minutes.
|
||||
async fn benchmarks(State(app): State<Arc<App>>) -> Response {
|
||||
let cached = app
|
||||
.summary_cache
|
||||
.lock()
|
||||
.unwrap_or_else(|e| e.into_inner())
|
||||
.as_ref()
|
||||
.filter(|(at, _)| at.elapsed() < CACHE_FOR)
|
||||
.map(|(_, json)| json.clone());
|
||||
let json = match cached {
|
||||
Some(json) => json,
|
||||
None => {
|
||||
let oldest = (app.today)().saturating_sub(app.config.retention_days);
|
||||
let uploads = match app.store().benchmark_uploads(oldest) {
|
||||
Ok(uploads) => uploads,
|
||||
Err(e) => {
|
||||
eprintln!("horizon-telemetry-server: could not read the benchmarks: {e}");
|
||||
return status(StatusCode::SERVICE_UNAVAILABLE, "try again later");
|
||||
}
|
||||
};
|
||||
let json = summary::summary(&uploads, summary::MIN_INSTALLS, &utc_now()).to_string();
|
||||
*app.summary_cache.lock().unwrap_or_else(|e| e.into_inner()) =
|
||||
Some((Instant::now(), json.clone()));
|
||||
json
|
||||
}
|
||||
};
|
||||
json_response(&app, json, CACHE_FOR.as_secs())
|
||||
}
|
||||
|
||||
/// Now as `YYYY-MM-DDTHH:MM:SSZ`.
|
||||
fn utc_now() -> String {
|
||||
let secs = std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.map_or(0, |d| d.as_secs());
|
||||
let (days, rest) = (secs / 86_400, secs % 86_400);
|
||||
// Days since 1970 to a civil date (Howard Hinnant's algorithm).
|
||||
let z = days as i64 + 719_468;
|
||||
let era = z.div_euclid(146_097);
|
||||
let doe = z - era * 146_097;
|
||||
let yoe = (doe - doe / 1460 + doe / 36_524 - doe / 146_096) / 365;
|
||||
let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
|
||||
let mp = (5 * doy + 2) / 153;
|
||||
let day = doy - (153 * mp + 2) / 5 + 1;
|
||||
let month = if mp < 10 { mp + 3 } else { mp - 9 };
|
||||
let year = yoe + era * 400 + i64::from(month <= 2);
|
||||
format!(
|
||||
"{year:04}-{month:02}-{day:02}T{:02}:{:02}:{:02}Z",
|
||||
rest / 3600,
|
||||
rest / 60 % 60,
|
||||
rest % 60
|
||||
)
|
||||
}
|
||||
|
||||
/// `POST /v1/live`: one "I'm playing". 204, or 400 for a bad ping, 413 for one too large, 429
|
||||
/// over the address's limit, 503 when the table is full.
|
||||
async fn ping(State(app): State<Arc<App>>, req: Request) -> Response {
|
||||
@@ -407,6 +480,7 @@ pub fn router(app: Arc<App>) -> Router {
|
||||
.route("/v1/live", post(ping).get(playing))
|
||||
.route("/v1/installs/{id}", delete(forget))
|
||||
.route("/v1/aggregate", get(aggregate))
|
||||
.route("/v1/benchmarks/summary", get(benchmarks))
|
||||
.route("/healthz", get(|| async { "ok" }))
|
||||
.with_state(app)
|
||||
}
|
||||
|
||||
@@ -357,27 +357,33 @@ impl Store {
|
||||
})?
|
||||
.collect::<Result<_, _>>()?
|
||||
};
|
||||
let benchmarks: Vec<serde_json::Value> = {
|
||||
let mut q = self.db.prepare(
|
||||
"SELECT day, version, body FROM reports WHERE kind = ?1 AND day BETWEEN ?2 AND ?3
|
||||
ORDER BY rowid DESC LIMIT 50",
|
||||
)?;
|
||||
q.query_map(params![kind_code(Kind::Benchmark), from, to], |r| {
|
||||
let body: Vec<u8> = r.get(2)?;
|
||||
Ok(serde_json::json!({
|
||||
"day": r.get::<_, u32>(0)?, "version": r.get::<_, String>(1)?, "report": inflate(&body),
|
||||
}))
|
||||
})?
|
||||
.collect::<Result<_, _>>()?
|
||||
};
|
||||
Ok(serde_json::json!({
|
||||
"from_day": from, "to_day": to, "min_install_days": k,
|
||||
"bucket_edges_ms": BUCKET_EDGES_MS.map(|e| (f64::from(e) * 10.0).round() / 10.0),
|
||||
"totals": { "sessions": sessions, "hours": (hours * 10.0).round() / 10.0 },
|
||||
"groups": out, "crashes_by_version": crashes, "benchmarks": benchmarks,
|
||||
"groups": out, "crashes_by_version": crashes,
|
||||
}))
|
||||
}
|
||||
|
||||
/// Every benchmark upload received since `from_day`, with its install (to count installs, never shown)
|
||||
/// and release, for `summary`.
|
||||
pub fn benchmark_uploads(
|
||||
&self,
|
||||
from_day: u32,
|
||||
) -> rusqlite::Result<Vec<crate::summary::Upload>> {
|
||||
let mut q = self
|
||||
.db
|
||||
.prepare("SELECT install, version, body FROM reports WHERE kind = ?1 AND day >= ?2")?;
|
||||
q.query_map(params![kind_code(Kind::Benchmark), from_day], |r| {
|
||||
Ok(crate::summary::Upload {
|
||||
install: r.get(0)?,
|
||||
version: r.get(1)?,
|
||||
report: inflate(&r.get::<_, Vec<u8>>(2)?),
|
||||
})
|
||||
})?
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// The day an install's newest row came in, if any (tests, and checking a deletion).
|
||||
pub fn newest_day(&self, install: &[u8]) -> rusqlite::Result<Option<u32>> {
|
||||
self.db
|
||||
|
||||
@@ -0,0 +1,266 @@
|
||||
//! `GET /v1/benchmarks/summary`: players' benchmark uploads, grouped for the website's Benchmarks page.
|
||||
//!
|
||||
//! The same rules as `website/scripts/benchmarks.mjs` (which builds the page's baked copy), checked against the
|
||||
//! same fixtures (`website/scripts/fixtures/benchmarks/`):
|
||||
//!
|
||||
//! - Only reports that compare: `horizon-benchmark-report` version 1, kind `game`, Horizon or plain vanilla, the
|
||||
//! standard flight over the four scenes, VSync off, no frame cap, drawn on a screen, with a GPU and CPU name.
|
||||
//! - Groups by GPU, CPU, render distance and game. A group is published only with at least `min_installs`
|
||||
//! different installs, so no one can be picked out.
|
||||
//! - Each install counts once: the median of its runs first, then the median across installs.
|
||||
//! - Out go the group's names, its counts, the newest three versions and the medians. Never the install ID, the
|
||||
//! day, the OS version, the driver, the memory size or the screen.
|
||||
use std::collections::{BTreeMap, BTreeSet, HashMap};
|
||||
|
||||
use serde_json::{Value, json};
|
||||
|
||||
pub const FORMAT: &str = "horizon-benchmark-summary";
|
||||
pub const MIN_INSTALLS: usize = 3;
|
||||
pub const SCENES: [&str; 4] = ["village", "forest", "ocean", "mountain"];
|
||||
pub const FIELDS: [&str; 7] = [
|
||||
"avg_fps",
|
||||
"low_1pct_fps",
|
||||
"p95_ms",
|
||||
"world_ms",
|
||||
"chunk_load_ms",
|
||||
"rss_peak_mb",
|
||||
"rss_steady_mb",
|
||||
];
|
||||
|
||||
/// One stored upload: who sent it (only to count installs), the release, and the report.
|
||||
pub struct Upload {
|
||||
pub install: Vec<u8>,
|
||||
pub version: String,
|
||||
pub report: Value,
|
||||
}
|
||||
|
||||
/// "Intel(R) Core(TM) i7" -> "Intel Core i7", spaces collapsed.
|
||||
fn trim(s: &str) -> String {
|
||||
s.replace("(R)", "")
|
||||
.replace("(TM)", "")
|
||||
.replace("(tm)", "")
|
||||
.split_whitespace()
|
||||
.collect::<Vec<_>>()
|
||||
.join(" ")
|
||||
}
|
||||
|
||||
fn game(app: &Value) -> Option<&'static str> {
|
||||
match app.get("name").and_then(Value::as_str) {
|
||||
Some("horizon-client") => Some("horizon"),
|
||||
Some("minecraft") => {
|
||||
let mods = app.get("mods").and_then(Value::as_array);
|
||||
let plain = mods.is_none_or(|m| {
|
||||
m.iter()
|
||||
.all(|m| m.get("name").and_then(Value::as_str) == Some("fabric-loader"))
|
||||
});
|
||||
plain.then_some("vanilla")
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Why a report can't be compared with the others, or `None` when it can.
|
||||
pub fn rejection(report: &Value) -> Option<&'static str> {
|
||||
if report.get("format").and_then(Value::as_str) != Some("horizon-benchmark-report")
|
||||
|| report.get("version").and_then(Value::as_u64) != Some(1)
|
||||
{
|
||||
return Some("not a version 1 benchmark report");
|
||||
}
|
||||
if report.get("kind").and_then(Value::as_str) != Some("game") {
|
||||
return Some("not a game run");
|
||||
}
|
||||
game(&report["app"])?;
|
||||
let c = &report["conditions"];
|
||||
let scenario = c.get("scenario").and_then(Value::as_str).unwrap_or("");
|
||||
if !(scenario == "standard" || scenario == "flight" || scenario.starts_with("perf_bench:")) {
|
||||
return Some("not the standard flight");
|
||||
}
|
||||
if c.get("vsync") != Some(&Value::Bool(false)) {
|
||||
return Some("VSync on or unknown");
|
||||
}
|
||||
if c.get("fps_cap").is_some_and(|v| !v.is_null()) {
|
||||
return Some("frame rate capped");
|
||||
}
|
||||
if c.get("offscreen") == Some(&Value::Bool(true)) {
|
||||
return Some("offscreen");
|
||||
}
|
||||
if !c.get("render_distance").is_some_and(Value::is_number) {
|
||||
return Some("no render distance");
|
||||
}
|
||||
let phases: BTreeSet<&str> = report["frames"]
|
||||
.as_array()
|
||||
.map(|f| f.iter().filter_map(|f| f["phase"].as_str()).collect())
|
||||
.unwrap_or_default();
|
||||
if !SCENES.iter().all(|s| phases.contains(s)) {
|
||||
return Some("not every scene");
|
||||
}
|
||||
let named = |v: &Value| v.as_str().is_some_and(|s| !trim(s).is_empty());
|
||||
if !named(&report["hardware"]["gpu"]["name"]) || !named(&report["hardware"]["cpu"]) {
|
||||
return Some("no GPU or CPU name");
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn mean(xs: &[f64]) -> Option<f64> {
|
||||
(!xs.is_empty()).then(|| xs.iter().sum::<f64>() / xs.len() as f64)
|
||||
}
|
||||
|
||||
pub fn median(xs: &[f64]) -> Option<f64> {
|
||||
let mut v: Vec<f64> = xs.iter().copied().filter(|x| x.is_finite()).collect();
|
||||
if v.is_empty() {
|
||||
return None;
|
||||
}
|
||||
v.sort_by(f64::total_cmp);
|
||||
let m = v.len() / 2;
|
||||
Some(if v.len() % 2 == 1 {
|
||||
v[m]
|
||||
} else {
|
||||
(v[m - 1] + v[m]) / 2.0
|
||||
})
|
||||
}
|
||||
|
||||
/// fps to integers, milliseconds to a tenth under 10 and to integers above, megabytes to integers.
|
||||
pub fn round(field: &str, x: f64) -> f64 {
|
||||
if field.ends_with("_ms") && x < 10.0 {
|
||||
(x * 10.0).round() / 10.0
|
||||
} else {
|
||||
x.round()
|
||||
}
|
||||
}
|
||||
|
||||
/// Whole numbers as integers (`515`, not `515.0`), as JavaScript writes them.
|
||||
fn number(x: f64) -> Value {
|
||||
if x.fract() == 0.0 && x.abs() < 9e15 {
|
||||
json!(x as i64)
|
||||
} else {
|
||||
json!(x)
|
||||
}
|
||||
}
|
||||
|
||||
/// A report's figures: frame figures averaged over every flight, the rest from its metrics.
|
||||
pub fn figures(report: &Value) -> [Option<f64>; 7] {
|
||||
let frames = report["frames"]
|
||||
.as_array()
|
||||
.map(Vec::as_slice)
|
||||
.unwrap_or(&[]);
|
||||
let avg = |key: &str| {
|
||||
mean(
|
||||
&frames
|
||||
.iter()
|
||||
.filter_map(|f| f[key].as_f64())
|
||||
.collect::<Vec<_>>(),
|
||||
)
|
||||
};
|
||||
let metrics = report["metrics"].as_object();
|
||||
let metric = |name: &str| metrics.and_then(|m| m.get(name)?.get("mean")?.as_f64());
|
||||
let loads: Vec<f64> = metrics
|
||||
.map(|m| {
|
||||
m.iter()
|
||||
.filter(|(k, _)| {
|
||||
k.strip_prefix("chunks.")
|
||||
.and_then(|k| k.strip_suffix(".load_ms"))
|
||||
.is_some_and(|scene| !scene.contains('.'))
|
||||
})
|
||||
.filter_map(|(_, v)| v.get("mean")?.as_f64())
|
||||
.collect()
|
||||
})
|
||||
.unwrap_or_default();
|
||||
[
|
||||
avg("avg_fps"),
|
||||
avg("low_1pct_fps"),
|
||||
avg("p95_ms"),
|
||||
metric("startup.world_ms"),
|
||||
mean(&loads),
|
||||
metric("memory.rss_peak_mb"),
|
||||
metric("memory.rss_steady_mb"),
|
||||
]
|
||||
}
|
||||
|
||||
/// "v0.2.10" after "v0.2.9", dated tags by their numbers: newest first.
|
||||
fn newest_first(a: &str, b: &str) -> std::cmp::Ordering {
|
||||
let n = |v: &str| -> Vec<u64> {
|
||||
v.split(|c: char| !c.is_ascii_digit())
|
||||
.filter(|p| !p.is_empty())
|
||||
.filter_map(|p| p.parse().ok())
|
||||
.collect()
|
||||
};
|
||||
n(b).cmp(&n(a))
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct Group {
|
||||
installs: HashMap<Vec<u8>, Vec<[Option<f64>; 7]>>,
|
||||
reports: usize,
|
||||
versions: BTreeSet<String>,
|
||||
}
|
||||
|
||||
/// Players' groups of at least `min_installs` installs, as `website/scripts/benchmarks.mjs` makes them.
|
||||
pub fn groups(uploads: &[Upload], min_installs: usize) -> Vec<Value> {
|
||||
let mut by: BTreeMap<(String, String, i64, &str), Group> = BTreeMap::new();
|
||||
for u in uploads {
|
||||
if u.install.is_empty() || rejection(&u.report).is_some() {
|
||||
continue;
|
||||
}
|
||||
let r = &u.report;
|
||||
let key = (
|
||||
trim(r["hardware"]["gpu"]["name"].as_str().unwrap_or_default()),
|
||||
trim(r["hardware"]["cpu"].as_str().unwrap_or_default()),
|
||||
r["conditions"]["render_distance"]
|
||||
.as_f64()
|
||||
.unwrap_or_default() as i64,
|
||||
game(&r["app"]).unwrap_or_default(),
|
||||
);
|
||||
let g = by.entry(key).or_default();
|
||||
g.reports += 1;
|
||||
if !u.version.is_empty() {
|
||||
g.versions.insert(u.version.clone());
|
||||
}
|
||||
g.installs
|
||||
.entry(u.install.clone())
|
||||
.or_default()
|
||||
.push(figures(r));
|
||||
}
|
||||
let mut out: Vec<(usize, Value)> = by
|
||||
.into_iter()
|
||||
.filter(|(_, g)| g.installs.len() >= min_installs)
|
||||
.map(|((gpu, cpu, rd, game), g)| {
|
||||
let per_install: Vec<[Option<f64>; 7]> = g
|
||||
.installs
|
||||
.values()
|
||||
.map(|runs| {
|
||||
std::array::from_fn(|i| median(&runs.iter().filter_map(|r| r[i]).collect::<Vec<_>>()))
|
||||
})
|
||||
.collect();
|
||||
let medians: serde_json::Map<String, Value> = FIELDS
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, f)| {
|
||||
let m = median(&per_install.iter().filter_map(|r| r[i]).collect::<Vec<_>>());
|
||||
(f.to_string(), m.map_or(Value::Null, |m| number(round(f, m))))
|
||||
})
|
||||
.collect();
|
||||
let mut versions: Vec<String> = g.versions.into_iter().collect();
|
||||
versions.sort_by(|a, b| newest_first(a, b));
|
||||
versions.truncate(3);
|
||||
let installs = g.installs.len();
|
||||
(
|
||||
installs,
|
||||
json!({
|
||||
"gpu": gpu, "cpu": cpu, "render_distance": rd, "game": game, "source": "players",
|
||||
"installs": installs, "reports": g.reports, "versions": versions, "medians": medians,
|
||||
}),
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
// Most installs first, then by GPU and render distance (the map's order).
|
||||
out.sort_by_key(|g| std::cmp::Reverse(g.0));
|
||||
out.into_iter().map(|(_, v)| v).collect()
|
||||
}
|
||||
|
||||
/// The whole answer; `generated` is an RFC 3339 UTC time.
|
||||
pub fn summary(uploads: &[Upload], min_installs: usize, generated: &str) -> Value {
|
||||
json!({
|
||||
"format": FORMAT, "version": 1, "generated": generated, "min_installs": min_installs,
|
||||
"groups": groups(uploads, min_installs),
|
||||
})
|
||||
}
|
||||
@@ -198,14 +198,30 @@ async fn an_address_is_rate_limited() {
|
||||
s.post(&install(9), vec![perf("x", 8)]).await,
|
||||
StatusCode::TOO_MANY_REQUESTS
|
||||
);
|
||||
let from = |forwarded: &str, real: Option<&str>| {
|
||||
let body = batch::encode(&Batch::new(&install(9), vec![perf("x", 8)]));
|
||||
let mut req = Request::post("/v1/batch")
|
||||
.header("x-forwarded-for", forwarded)
|
||||
.header("content-encoding", "zstd");
|
||||
if let Some(ip) = real {
|
||||
req = req.header("x-real-ip", ip);
|
||||
}
|
||||
req.body(Body::from(body)).unwrap()
|
||||
};
|
||||
// A client can't pick its address: the proxy adds the real one last, or sets X-Real-IP.
|
||||
assert_eq!(
|
||||
s.send(from("198.51.100.7, 203.0.113.9", None)).await.0,
|
||||
StatusCode::TOO_MANY_REQUESTS
|
||||
);
|
||||
assert_eq!(
|
||||
s.send(from("198.51.100.7", Some("203.0.113.9"))).await.0,
|
||||
StatusCode::TOO_MANY_REQUESTS
|
||||
);
|
||||
// Another address has its own allowance.
|
||||
let body = batch::encode(&Batch::new(&install(9), vec![perf("x", 8)]));
|
||||
let req = Request::post("/v1/batch")
|
||||
.header("x-forwarded-for", "198.51.100.7, 10.0.0.1")
|
||||
.header("content-encoding", "zstd")
|
||||
.body(Body::from(body))
|
||||
.unwrap();
|
||||
assert_eq!(s.send(req).await.0, StatusCode::NO_CONTENT);
|
||||
assert_eq!(
|
||||
s.send(from("203.0.113.9, 198.51.100.7", None)).await.0,
|
||||
StatusCode::NO_CONTENT
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
|
||||
@@ -0,0 +1,180 @@
|
||||
//! `GET /v1/benchmarks/summary` against the website's fixtures (website/scripts/fixtures/benchmarks/): the same
|
||||
//! uploads must give the groups `website/scripts/benchmarks.mjs` gives.
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::AtomicU32;
|
||||
|
||||
use axum::body::Body;
|
||||
use axum::http::{Request, StatusCode};
|
||||
use horizon_telemetry::Item;
|
||||
use horizon_telemetry::batch::{self, Batch};
|
||||
use horizon_telemetry::payload::{Payload, Source};
|
||||
use horizon_telemetry_server::store::Store;
|
||||
use horizon_telemetry_server::summary::{self, Upload};
|
||||
use horizon_telemetry_server::{App, Config, router};
|
||||
use http_body_util::BodyExt;
|
||||
use serde_json::Value;
|
||||
use tower::ServiceExt;
|
||||
|
||||
const FIXTURES: &str = concat!(
|
||||
env!("CARGO_MANIFEST_DIR"),
|
||||
"/../../website/scripts/fixtures/benchmarks"
|
||||
);
|
||||
|
||||
fn items() -> Vec<Value> {
|
||||
std::fs::read_to_string(format!("{FIXTURES}/items.jsonl"))
|
||||
.unwrap()
|
||||
.lines()
|
||||
.filter(|l| !l.trim().is_empty())
|
||||
.map(|l| serde_json::from_str(l).unwrap())
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn expected() -> Value {
|
||||
serde_json::from_str(&std::fs::read_to_string(format!("{FIXTURES}/expected.json")).unwrap())
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
/// The fixtures' short install names as the 32 hex digits real installs have.
|
||||
fn install_id(name: &str) -> String {
|
||||
format!(
|
||||
"{:032x}",
|
||||
name.bytes().fold(0u128, |n, b| n * 257 + u128::from(b))
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_fixtures_give_the_websites_groups() {
|
||||
let uploads: Vec<Upload> = items()
|
||||
.into_iter()
|
||||
.map(|i| Upload {
|
||||
install: i["install_id"]
|
||||
.as_str()
|
||||
.unwrap_or_default()
|
||||
.as_bytes()
|
||||
.to_vec(),
|
||||
version: i["version"].as_str().unwrap_or_default().to_owned(),
|
||||
report: i["data"].clone(),
|
||||
})
|
||||
.collect();
|
||||
let s = summary::summary(&uploads, summary::MIN_INSTALLS, "2026-11-03T00:00:00Z");
|
||||
let want = expected();
|
||||
assert_eq!(s["format"], "horizon-benchmark-summary");
|
||||
assert_eq!(s["version"], 1);
|
||||
assert_eq!(s["min_installs"], want["min_installs"]);
|
||||
assert_eq!(s["groups"], want["groups"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn only_comparable_runs_count() {
|
||||
let reasons: Vec<&str> = items()
|
||||
.iter()
|
||||
.filter_map(|i| summary::rejection(&i["data"]))
|
||||
.collect();
|
||||
assert_eq!(
|
||||
reasons,
|
||||
[
|
||||
"VSync on or unknown",
|
||||
"offscreen",
|
||||
"frame rate capped",
|
||||
"not every scene",
|
||||
"not the standard flight",
|
||||
"not a version 1 benchmark report"
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn medians_and_rounding() {
|
||||
assert_eq!(summary::median(&[3.0, 1.0, 2.0]), Some(2.0));
|
||||
assert_eq!(summary::median(&[4.0, 1.0, 2.0, 3.0]), Some(2.5));
|
||||
assert_eq!(summary::median(&[f64::NAN]), None);
|
||||
assert_eq!(summary::round("p95_ms", 3.46), 3.5);
|
||||
assert_eq!(summary::round("world_ms", 4512.4), 4512.0);
|
||||
assert_eq!(summary::round("avg_fps", 99.5), 100.0);
|
||||
}
|
||||
|
||||
async fn get(app: &Arc<App>, uri: &str) -> (StatusCode, String, Option<String>) {
|
||||
let res = router(app.clone())
|
||||
.oneshot(Request::get(uri).body(Body::empty()).unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
let code = res.status();
|
||||
let origin = res
|
||||
.headers()
|
||||
.get("access-control-allow-origin")
|
||||
.map(|v| v.to_str().unwrap().to_owned());
|
||||
let body = res.into_body().collect().await.unwrap().to_bytes();
|
||||
(code, String::from_utf8_lossy(&body).into_owned(), origin)
|
||||
}
|
||||
|
||||
/// Uploaded through `POST /v1/batch` as the launcher sends them, then read back from the endpoint.
|
||||
#[tokio::test]
|
||||
async fn uploads_come_back_grouped_and_anonymous() {
|
||||
let day = Arc::new(AtomicU32::new(20_396));
|
||||
let clock = day.clone();
|
||||
let config = Config {
|
||||
requests_per_hour: 1e6,
|
||||
..Config::default()
|
||||
};
|
||||
let app = App::with_clock(
|
||||
Store::open(":memory:").unwrap(),
|
||||
config,
|
||||
Box::new(move || clock.load(std::sync::atomic::Ordering::SeqCst)),
|
||||
);
|
||||
for i in items() {
|
||||
let Some(name) = i["install_id"].as_str() else {
|
||||
continue; // an item without an install can't be sent
|
||||
};
|
||||
let item = Item::new(
|
||||
Source::Game,
|
||||
i["version"].as_str().unwrap(),
|
||||
Payload::Benchmark(i["data"].clone()),
|
||||
);
|
||||
let body = batch::encode(&Batch::new(&install_id(name), vec![item]));
|
||||
let res = router(app.clone())
|
||||
.oneshot(
|
||||
Request::post("/v1/batch")
|
||||
.header("content-encoding", "zstd")
|
||||
.body(Body::from(body))
|
||||
.unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(res.status(), StatusCode::NO_CONTENT, "{name}");
|
||||
}
|
||||
|
||||
let (code, body, origin) = get(&app, "/v1/benchmarks/summary").await;
|
||||
assert_eq!(code, StatusCode::OK);
|
||||
assert_eq!(origin.as_deref(), Some("https://playhorizon.dev"));
|
||||
let s: Value = serde_json::from_str(&body).unwrap();
|
||||
assert_eq!(s["groups"], expected()["groups"]);
|
||||
let generated = s["generated"].as_str().unwrap();
|
||||
assert!(
|
||||
generated.len() == 20 && generated.ends_with('Z'),
|
||||
"{generated}"
|
||||
);
|
||||
for secret in [
|
||||
install_id("a1").as_str(),
|
||||
"install_id",
|
||||
"secret-os-version",
|
||||
"secret-driver",
|
||||
"20394",
|
||||
"ram_mb",
|
||||
] {
|
||||
assert!(!body.contains(secret), "{secret} in {body}");
|
||||
}
|
||||
|
||||
// Deleting one of the three installs takes the group below three: it goes at once.
|
||||
let res = router(app.clone())
|
||||
.oneshot(
|
||||
Request::delete(format!("/v1/installs/{}", install_id("a3")))
|
||||
.body(Body::empty())
|
||||
.unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(res.status(), StatusCode::NO_CONTENT);
|
||||
let (_, body, _) = get(&app, "/v1/benchmarks/summary").await;
|
||||
let s: Value = serde_json::from_str(&body).unwrap();
|
||||
assert_eq!(s["groups"], serde_json::json!([]));
|
||||
}
|
||||
@@ -33,16 +33,21 @@ pub const SCHEMA: u32 = 1;
|
||||
/// and until the player answers nothing new is kept or sent.
|
||||
pub const CONSENT_VERSION: u32 = 1;
|
||||
|
||||
/// The ingest server, or none while there isn't one: then nothing is ever sent (no ping either)
|
||||
/// and the outbox stays within its caps. `HORIZON_TELEMETRY_URL` overrides it (tests, a local
|
||||
/// server).
|
||||
pub const DEFAULT_ENDPOINT: Option<&str> = None;
|
||||
/// The ingest server (`crates/horizon-telemetry-server`, on Railway). `HORIZON_TELEMETRY_URL`
|
||||
/// overrides it (a local server); `HORIZON_TELEMETRY_URL=off` sends nothing at all, no ping
|
||||
/// either (tests and automated runs). What is sent still depends on the player's switches.
|
||||
pub const DEFAULT_ENDPOINT: &str = "https://telemetry.playhorizon.dev";
|
||||
|
||||
/// Where items are sent, if anywhere.
|
||||
pub fn endpoint() -> Option<String> {
|
||||
match std::env::var("HORIZON_TELEMETRY_URL") {
|
||||
Ok(url) if !url.trim().is_empty() => Some(url.trim().trim_end_matches('/').to_owned()),
|
||||
_ => DEFAULT_ENDPOINT.map(str::to_owned),
|
||||
resolve(std::env::var("HORIZON_TELEMETRY_URL").ok().as_deref())
|
||||
}
|
||||
|
||||
fn resolve(var: Option<&str>) -> Option<String> {
|
||||
match var.map(str::trim) {
|
||||
Some("off" | "none" | "0") => None,
|
||||
Some(url) if !url.is_empty() => Some(url.trim_end_matches('/').to_owned()),
|
||||
_ => Some(DEFAULT_ENDPOINT.to_owned()),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -100,3 +105,20 @@ pub(crate) mod test_home {
|
||||
dir
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn the_endpoint_defaults_to_the_server_and_can_be_moved_or_turned_off() {
|
||||
assert_eq!(resolve(None).as_deref(), Some(DEFAULT_ENDPOINT));
|
||||
assert_eq!(resolve(Some(" ")).as_deref(), Some(DEFAULT_ENDPOINT));
|
||||
assert_eq!(
|
||||
resolve(Some("http://127.0.0.1:8080/")).as_deref(),
|
||||
Some("http://127.0.0.1:8080")
|
||||
);
|
||||
assert_eq!(resolve(Some("off")), None);
|
||||
assert!(DEFAULT_ENDPOINT.starts_with("https://") && !DEFAULT_ENDPOINT.ends_with('/'));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user