Rollup merge of #56091 - wesleywiser:fix_self_profiler_json, r=petrochenkov

Fix json output in the self-profiler

Fix missing ',' array element separators and convert NaN's to 0.

cc @Mark-Simulacrum
This commit is contained in:
kennytm 2018-11-24 01:31:54 +08:00 committed by GitHub
commit fb33fa4916
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -93,16 +93,27 @@ fn json(&self) -> String {
$( $(
let (hits, total) = self.query_counts.$name; let (hits, total) = self.query_counts.$name;
//normalize hits to 0%
let hit_percent =
if total > 0 {
((hits as f32) / (total as f32)) * 100.0
} else {
0.0
};
json.push_str(&format!( json.push_str(&format!(
"{{ \"category\": {}, \"time_ms\": {}, "{{ \"category\": {}, \"time_ms\": {},
\"query_count\": {}, \"query_hits\": {} }}", \"query_count\": {}, \"query_hits\": {} }},",
stringify!($name), stringify!($name),
self.times.$name / 1_000_000, self.times.$name / 1_000_000,
total, total,
format!("{:.2}", (((hits as f32) / (total as f32)) * 100.0)) format!("{:.2}", hit_percent)
)); ));
)* )*
//remove the trailing ',' character
json.pop();
json.push(']'); json.push(']');
json json