rust/xtask/src/metrics.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

201 lines
6.3 KiB
Rust
Raw Normal View History

2020-07-24 16:28:07 +02:00
use std::{
collections::BTreeMap,
2022-03-13 21:20:51 +00:00
env, fs,
2020-07-24 16:28:07 +02:00
io::Write as _,
2020-07-25 10:35:45 +02:00
path::Path,
2020-07-24 16:28:07 +02:00
time::{Instant, SystemTime, UNIX_EPOCH},
};
2022-03-13 21:20:51 +00:00
use anyhow::{bail, format_err};
use xshell::{cmd, Shell};
2020-07-24 16:28:07 +02:00
use crate::flags;
2020-07-24 16:28:07 +02:00
type Unit = String;
2020-07-25 00:16:21 +02:00
impl flags::Metrics {
2022-03-13 21:20:51 +00:00
pub(crate) fn run(self, sh: &Shell) -> anyhow::Result<()> {
let mut metrics = Metrics::new(sh)?;
2020-07-25 00:16:21 +02:00
if !self.dry_run {
2022-03-17 08:53:33 +02:00
sh.remove_path("./target/release")?;
2020-07-25 00:16:21 +02:00
}
2020-07-25 10:35:45 +02:00
if !Path::new("./target/rustc-perf").exists() {
2022-03-13 21:20:51 +00:00
sh.create_dir("./target/rustc-perf")?;
cmd!(sh, "git clone https://github.com/rust-lang/rustc-perf.git ./target/rustc-perf")
2020-10-16 19:46:03 +02:00
.run()?;
2020-07-25 10:35:45 +02:00
}
{
2022-03-13 21:20:51 +00:00
let _d = sh.push_dir("./target/rustc-perf");
2021-03-15 22:21:02 +01:00
let revision = &metrics.perf_revision;
2022-03-13 21:20:51 +00:00
cmd!(sh, "git reset --hard {revision}").run()?;
2020-07-25 10:35:45 +02:00
}
2022-03-13 21:20:51 +00:00
let _env = sh.push_env("RA_METRICS", "1");
2020-07-25 00:16:21 +02:00
2022-02-26 11:56:57 +02:00
{
2022-07-08 15:44:49 +02:00
// https://github.com/rust-lang/rust-analyzer/issues/9997
2022-03-13 21:20:51 +00:00
let _d = sh.push_dir("target/rustc-perf/collector/benchmarks/webrender");
cmd!(sh, "cargo update -p url --precise 1.6.1").run()?;
2022-02-26 11:56:57 +02:00
}
2022-03-13 21:20:51 +00:00
metrics.measure_build(sh)?;
metrics.measure_analysis_stats_self(sh)?;
metrics.measure_analysis_stats(sh, "ripgrep")?;
metrics.measure_analysis_stats(sh, "webrender")?;
metrics.measure_analysis_stats(sh, "diesel/diesel")?;
2020-07-24 16:28:07 +02:00
2020-07-25 00:16:21 +02:00
if !self.dry_run {
2022-03-13 21:20:51 +00:00
let _d = sh.push_dir("target");
2020-07-25 00:16:21 +02:00
let metrics_token = env::var("METRICS_TOKEN").unwrap();
2020-10-16 19:46:03 +02:00
cmd!(
2022-03-13 21:20:51 +00:00
sh,
2020-10-16 19:46:03 +02:00
"git clone --depth 1 https://{metrics_token}@github.com/rust-analyzer/metrics.git"
)
.run()?;
2020-07-24 20:53:08 +02:00
2022-03-16 18:15:44 +02:00
{
let mut file =
fs::File::options().append(true).open("target/metrics/metrics.json")?;
writeln!(file, "{}", metrics.json())?;
}
let _d = sh.push_dir("metrics");
2022-03-13 21:20:51 +00:00
cmd!(sh, "git add .").run()?;
cmd!(sh, "git -c user.name=Bot -c user.email=dummy@example.com commit --message 📈")
2020-10-16 19:46:03 +02:00
.run()?;
2022-03-13 21:20:51 +00:00
cmd!(sh, "git push origin master").run()?;
2020-07-25 00:16:21 +02:00
}
2022-03-13 21:20:51 +00:00
eprintln!("{metrics:#?}");
2020-07-25 00:16:21 +02:00
Ok(())
2020-07-24 16:28:07 +02:00
}
}
impl Metrics {
2022-03-13 21:20:51 +00:00
fn measure_build(&mut self, sh: &Shell) -> anyhow::Result<()> {
2020-07-25 10:35:45 +02:00
eprintln!("\nMeasuring build");
2022-03-13 21:20:51 +00:00
cmd!(sh, "cargo fetch").run()?;
2020-07-24 16:28:07 +02:00
2020-07-25 00:16:21 +02:00
let time = Instant::now();
2022-03-13 21:20:51 +00:00
cmd!(sh, "cargo build --release --package rust-analyzer --bin rust-analyzer").run()?;
2020-07-25 00:16:21 +02:00
let time = time.elapsed();
2020-07-25 10:35:45 +02:00
self.report("build", time.as_millis() as u64, "ms".into());
2020-07-25 00:16:21 +02:00
Ok(())
}
2022-03-13 21:20:51 +00:00
fn measure_analysis_stats_self(&mut self, sh: &Shell) -> anyhow::Result<()> {
self.measure_analysis_stats_path(sh, "self", ".")
2020-07-25 10:35:45 +02:00
}
2022-03-13 21:20:51 +00:00
fn measure_analysis_stats(&mut self, sh: &Shell, bench: &str) -> anyhow::Result<()> {
2020-07-25 10:35:45 +02:00
self.measure_analysis_stats_path(
2022-03-13 21:20:51 +00:00
sh,
2020-07-25 10:35:45 +02:00
bench,
&format!("./target/rustc-perf/collector/benchmarks/{}", bench),
)
}
2022-03-13 21:20:51 +00:00
fn measure_analysis_stats_path(
&mut self,
sh: &Shell,
name: &str,
path: &str,
) -> anyhow::Result<()> {
eprintln!("\nMeasuring analysis-stats/{name}");
let output =
cmd!(sh, "./target/release/rust-analyzer -q analysis-stats --memory-usage {path}")
.read()?;
2020-07-25 10:35:45 +02:00
for (metric, value, unit) in parse_metrics(&output) {
2022-03-13 21:20:51 +00:00
self.report(&format!("analysis-stats/{name}/{metric}"), value, unit.into());
2020-07-25 10:35:45 +02:00
}
2020-07-24 16:28:07 +02:00
Ok(())
}
}
2020-07-25 10:35:45 +02:00
fn parse_metrics(output: &str) -> Vec<(&str, u64, &str)> {
output
.lines()
.filter_map(|it| {
let entry = it.split(':').collect::<Vec<_>>();
match entry.as_slice() {
["METRIC", name, value, unit] => Some((*name, value.parse().unwrap(), *unit)),
_ => None,
}
})
.collect()
}
2020-07-24 16:28:07 +02:00
#[derive(Debug)]
struct Metrics {
host: Host,
timestamp: SystemTime,
revision: String,
2021-03-15 22:21:02 +01:00
perf_revision: String,
2020-07-24 16:28:07 +02:00
metrics: BTreeMap<String, (u64, Unit)>,
}
#[derive(Debug)]
struct Host {
os: String,
cpu: String,
mem: String,
}
impl Metrics {
2022-03-13 21:20:51 +00:00
fn new(sh: &Shell) -> anyhow::Result<Metrics> {
let host = Host::new(sh)?;
2020-07-24 16:28:07 +02:00
let timestamp = SystemTime::now();
2022-03-13 21:20:51 +00:00
let revision = cmd!(sh, "git rev-parse HEAD").read()?;
2021-03-15 22:21:02 +01:00
let perf_revision = "c52ee623e231e7690a93be88d943016968c1036b".into();
Ok(Metrics { host, timestamp, revision, perf_revision, metrics: BTreeMap::new() })
2020-07-24 16:28:07 +02:00
}
fn report(&mut self, name: &str, value: u64, unit: Unit) {
self.metrics.insert(name.into(), (value, unit));
}
2020-08-01 04:09:52 +02:00
fn json(&self) -> String {
let mut buf = String::new();
self.to_json(write_json::object(&mut buf));
buf
2020-07-24 16:28:07 +02:00
}
2020-08-01 04:09:52 +02:00
fn to_json(&self, mut obj: write_json::Object<'_>) {
self.host.to_json(obj.object("host"));
let timestamp = self.timestamp.duration_since(UNIX_EPOCH).unwrap();
obj.number("timestamp", timestamp.as_secs() as f64);
obj.string("revision", &self.revision);
2021-03-15 22:21:02 +01:00
obj.string("perf_revision", &self.perf_revision);
2020-08-01 04:09:52 +02:00
let mut metrics = obj.object("metrics");
for (k, (value, unit)) in &self.metrics {
metrics.array(k).number(*value as f64).string(unit);
2020-07-24 16:28:07 +02:00
}
}
}
impl Host {
2022-03-13 21:20:51 +00:00
fn new(sh: &Shell) -> anyhow::Result<Host> {
2020-07-24 16:28:07 +02:00
if cfg!(not(target_os = "linux")) {
bail!("can only collect metrics on Linux ");
}
2022-03-13 21:20:51 +00:00
let os = read_field(sh, "/etc/os-release", "PRETTY_NAME=")?.trim_matches('"').to_string();
2020-07-24 16:28:07 +02:00
2022-03-13 21:20:51 +00:00
let cpu = read_field(sh, "/proc/cpuinfo", "model name")?
.trim_start_matches(':')
.trim()
.to_string();
2020-07-24 16:28:07 +02:00
2022-03-13 21:20:51 +00:00
let mem = read_field(sh, "/proc/meminfo", "MemTotal:")?;
2020-07-24 16:28:07 +02:00
return Ok(Host { os, cpu, mem });
2022-03-13 21:20:51 +00:00
fn read_field(sh: &Shell, path: &str, field: &str) -> anyhow::Result<String> {
let text = sh.read_file(path)?;
2020-07-24 16:28:07 +02:00
2022-03-13 21:20:51 +00:00
text.lines()
.find_map(|it| it.strip_prefix(field))
.map(|it| it.trim().to_string())
.ok_or_else(|| format_err!("can't parse {}", path))
2020-07-24 16:28:07 +02:00
}
}
2020-08-01 04:09:52 +02:00
fn to_json(&self, mut obj: write_json::Object<'_>) {
obj.string("os", &self.os).string("cpu", &self.cpu).string("mem", &self.mem);
2020-07-24 16:28:07 +02:00
}
}