1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
use std::{
collections::BTreeMap,
env,
fmt::{self, Write as _},
io::Write as _,
path::Path,
time::{Instant, SystemTime, UNIX_EPOCH},
};
use anyhow::{bail, format_err, Result};
use crate::not_bash::{fs2, pushd, pushenv, rm_rf, run};
type Unit = String;
pub struct MetricsCmd {
pub dry_run: bool,
}
impl MetricsCmd {
pub fn run(self) -> Result<()> {
let mut metrics = Metrics::new()?;
if !self.dry_run {
rm_rf("./target/release")?;
}
if !Path::new("./target/rustc-perf").exists() {
fs2::create_dir_all("./target/rustc-perf")?;
run!("git clone https://github.com/rust-lang/rustc-perf.git ./target/rustc-perf")?;
}
{
let _d = pushd("./target/rustc-perf");
run!("git reset --hard 1d9288b0da7febf2599917da1b57dc241a1af033")?;
}
let _env = pushenv("RA_METRICS", "1");
metrics.measure_build()?;
metrics.measure_analysis_stats_self()?;
metrics.measure_analysis_stats("ripgrep")?;
metrics.measure_analysis_stats("webrender")?;
if !self.dry_run {
let _d = pushd("target");
let metrics_token = env::var("METRICS_TOKEN").unwrap();
let repo = format!("https://{}@github.com/rust-analyzer/metrics.git", metrics_token);
run!("git clone --depth 1 {}", repo)?;
let _d = pushd("metrics");
let mut file = std::fs::OpenOptions::new().append(true).open("metrics.json")?;
writeln!(file, "{}", metrics.json())?;
run!("git add .")?;
run!("git -c user.name=Bot -c [email protected] commit --message 📈")?;
run!("git push origin master")?;
}
eprintln!("{:#?}", metrics);
Ok(())
}
}
impl Metrics {
fn measure_build(&mut self) -> Result<()> {
eprintln!("\nMeasuring build");
run!("cargo fetch")?;
let time = Instant::now();
run!("cargo build --release --package rust-analyzer --bin rust-analyzer")?;
let time = time.elapsed();
self.report("build", time.as_millis() as u64, "ms".into());
Ok(())
}
fn measure_analysis_stats_self(&mut self) -> Result<()> {
self.measure_analysis_stats_path("self", &".")
}
fn measure_analysis_stats(&mut self, bench: &str) -> Result<()> {
self.measure_analysis_stats_path(
bench,
&format!("./target/rustc-perf/collector/benchmarks/{}", bench),
)
}
fn measure_analysis_stats_path(&mut self, name: &str, path: &str) -> Result<()> {
eprintln!("\nMeasuring analysis-stats/{}", name);
let output = run!("./target/release/rust-analyzer analysis-stats --quiet {}", path)?;
for (metric, value, unit) in parse_metrics(&output) {
self.report(&format!("analysis-stats/{}/{}", name, metric), value, unit.into());
}
Ok(())
}
}
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()
}
#[derive(Debug)]
struct Metrics {
host: Host,
timestamp: SystemTime,
revision: String,
metrics: BTreeMap<String, (u64, Unit)>,
}
#[derive(Debug)]
struct Host {
os: String,
cpu: String,
mem: String,
}
impl Metrics {
fn new() -> Result<Metrics> {
let host = Host::new()?;
let timestamp = SystemTime::now();
let revision = run!("git rev-parse HEAD")?;
Ok(Metrics { host, timestamp, revision, metrics: BTreeMap::new() })
}
fn report(&mut self, name: &str, value: u64, unit: Unit) {
self.metrics.insert(name.into(), (value, unit));
}
fn json(&self) -> Json {
let mut json = Json::default();
self.to_json(&mut json);
json
}
fn to_json(&self, json: &mut Json) {
json.begin_object();
{
json.field("host");
self.host.to_json(json);
json.field("timestamp");
let timestamp = self.timestamp.duration_since(UNIX_EPOCH).unwrap();
json.number(timestamp.as_secs() as f64);
json.field("revision");
json.string(&self.revision);
json.field("metrics");
json.begin_object();
{
for (k, (value, unit)) in &self.metrics {
json.field(k);
json.begin_array();
{
json.number(*value as f64);
json.string(unit);
}
json.end_array();
}
}
json.end_object()
}
json.end_object();
}
}
impl Host {
fn new() -> Result<Host> {
if cfg!(not(target_os = "linux")) {
bail!("can only collect metrics on Linux ");
}
let os = read_field("/etc/os-release", "PRETTY_NAME=")?.trim_matches('"').to_string();
let cpu =
read_field("/proc/cpuinfo", "model name")?.trim_start_matches(':').trim().to_string();
let mem = read_field("/proc/meminfo", "MemTotal:")?;
return Ok(Host { os, cpu, mem });
fn read_field<'a>(path: &str, field: &str) -> Result<String> {
let text = fs2::read_to_string(path)?;
let line = text
.lines()
.find(|it| it.starts_with(field))
.ok_or_else(|| format_err!("can't parse {}", path))?;
Ok(line[field.len()..].trim().to_string())
}
}
fn to_json(&self, json: &mut Json) {
json.begin_object();
{
json.field("os");
json.string(&self.os);
json.field("cpu");
json.string(&self.cpu);
json.field("mem");
json.string(&self.mem);
}
json.end_object();
}
}
struct State {
obj: bool,
first: bool,
}
#[derive(Default)]
struct Json {
stack: Vec<State>,
buf: String,
}
impl Json {
fn begin_object(&mut self) {
self.stack.push(State { obj: true, first: true });
self.buf.push('{');
}
fn end_object(&mut self) {
self.stack.pop();
self.buf.push('}')
}
fn begin_array(&mut self) {
self.stack.push(State { obj: false, first: true });
self.buf.push('[');
}
fn end_array(&mut self) {
self.stack.pop();
self.buf.push(']')
}
fn field(&mut self, name: &str) {
self.object_comma();
self.string_token(name);
self.buf.push(':');
}
fn string(&mut self, value: &str) {
self.array_comma();
self.string_token(value);
}
fn string_token(&mut self, value: &str) {
self.buf.push('"');
self.buf.extend(value.escape_default());
self.buf.push('"');
}
fn number(&mut self, value: f64) {
self.array_comma();
write!(self.buf, "{}", value).unwrap();
}
fn array_comma(&mut self) {
let state = self.stack.last_mut().unwrap();
if state.obj {
return;
}
if !state.first {
self.buf.push(',');
}
state.first = false;
}
fn object_comma(&mut self) {
let state = self.stack.last_mut().unwrap();
if !state.first {
self.buf.push(',');
}
state.first = false;
}
}
impl fmt::Display for Json {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.buf)
}
}
|