Skip to content

Commit 5af32ae

Browse files
bors[bot]matklad
andauthored
Merge #5635
5635: Better JSON serialization in metrics r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
2 parents 2346a28 + a015714 commit 5af32ae

File tree

3 files changed

+22
-118
lines changed

3 files changed

+22
-118
lines changed

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

xtask/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ proc-macro2 = "1.0.8"
1717
quote = "1.0.2"
1818
ungrammar = "0.1.0"
1919
walkdir = "2.3.1"
20+
write-json = "0.1.0"

xtask/src/metrics.rs

Lines changed: 14 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use std::{
22
collections::BTreeMap,
33
env,
4-
fmt::{self, Write as _},
54
io::Write as _,
65
path::Path,
76
time::{Instant, SystemTime, UNIX_EPOCH},
@@ -127,40 +126,21 @@ impl Metrics {
127126
self.metrics.insert(name.into(), (value, unit));
128127
}
129128

130-
fn json(&self) -> Json {
131-
let mut json = Json::default();
132-
self.to_json(&mut json);
133-
json
129+
fn json(&self) -> String {
130+
let mut buf = String::new();
131+
self.to_json(write_json::object(&mut buf));
132+
buf
134133
}
135-
fn to_json(&self, json: &mut Json) {
136-
json.begin_object();
137-
{
138-
json.field("host");
139-
self.host.to_json(json);
140-
141-
json.field("timestamp");
142-
let timestamp = self.timestamp.duration_since(UNIX_EPOCH).unwrap();
143-
json.number(timestamp.as_secs() as f64);
144134

145-
json.field("revision");
146-
json.string(&self.revision);
147-
148-
json.field("metrics");
149-
json.begin_object();
150-
{
151-
for (k, (value, unit)) in &self.metrics {
152-
json.field(k);
153-
json.begin_array();
154-
{
155-
json.number(*value as f64);
156-
json.string(unit);
157-
}
158-
json.end_array();
159-
}
160-
}
161-
json.end_object()
135+
fn to_json(&self, mut obj: write_json::Object<'_>) {
136+
self.host.to_json(obj.object("host"));
137+
let timestamp = self.timestamp.duration_since(UNIX_EPOCH).unwrap();
138+
obj.number("timestamp", timestamp.as_secs() as f64);
139+
obj.string("revision", &self.revision);
140+
let mut metrics = obj.object("metrics");
141+
for (k, (value, unit)) in &self.metrics {
142+
metrics.array(k).number(*value as f64).string(unit);
162143
}
163-
json.end_object();
164144
}
165145
}
166146

@@ -189,91 +169,7 @@ impl Host {
189169
Ok(line[field.len()..].trim().to_string())
190170
}
191171
}
192-
fn to_json(&self, json: &mut Json) {
193-
json.begin_object();
194-
{
195-
json.field("os");
196-
json.string(&self.os);
197-
198-
json.field("cpu");
199-
json.string(&self.cpu);
200-
201-
json.field("mem");
202-
json.string(&self.mem);
203-
}
204-
json.end_object();
205-
}
206-
}
207-
208-
struct State {
209-
obj: bool,
210-
first: bool,
211-
}
212-
213-
#[derive(Default)]
214-
struct Json {
215-
stack: Vec<State>,
216-
buf: String,
217-
}
218-
219-
impl Json {
220-
fn begin_object(&mut self) {
221-
self.stack.push(State { obj: true, first: true });
222-
self.buf.push('{');
223-
}
224-
fn end_object(&mut self) {
225-
self.stack.pop();
226-
self.buf.push('}')
227-
}
228-
fn begin_array(&mut self) {
229-
self.stack.push(State { obj: false, first: true });
230-
self.buf.push('[');
231-
}
232-
fn end_array(&mut self) {
233-
self.stack.pop();
234-
self.buf.push(']')
235-
}
236-
fn field(&mut self, name: &str) {
237-
self.object_comma();
238-
self.string_token(name);
239-
self.buf.push(':');
240-
}
241-
fn string(&mut self, value: &str) {
242-
self.array_comma();
243-
self.string_token(value);
244-
}
245-
fn string_token(&mut self, value: &str) {
246-
self.buf.push('"');
247-
self.buf.extend(value.escape_default());
248-
self.buf.push('"');
249-
}
250-
fn number(&mut self, value: f64) {
251-
self.array_comma();
252-
write!(self.buf, "{}", value).unwrap();
253-
}
254-
255-
fn array_comma(&mut self) {
256-
let state = self.stack.last_mut().unwrap();
257-
if state.obj {
258-
return;
259-
}
260-
if !state.first {
261-
self.buf.push(',');
262-
}
263-
state.first = false;
264-
}
265-
266-
fn object_comma(&mut self) {
267-
let state = self.stack.last_mut().unwrap();
268-
if !state.first {
269-
self.buf.push(',');
270-
}
271-
state.first = false;
272-
}
273-
}
274-
275-
impl fmt::Display for Json {
276-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
277-
write!(f, "{}", self.buf)
172+
fn to_json(&self, mut obj: write_json::Object<'_>) {
173+
obj.string("os", &self.os).string("cpu", &self.cpu).string("mem", &self.mem);
278174
}
279175
}

0 commit comments

Comments
 (0)