From c7dc868ed7b2899f585fa64ede938e665f965067 Mon Sep 17 00:00:00 2001 From: Wesley Wiser Date: Mon, 19 Nov 2018 23:26:00 -0500 Subject: [PATCH] Fix json output in the self-profiler Fix missing ',' array element separators and convert NaN's to 0. --- src/librustc/util/profiling.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/librustc/util/profiling.rs b/src/librustc/util/profiling.rs index 37073b6e820..dfd3f2ee184 100644 --- a/src/librustc/util/profiling.rs +++ b/src/librustc/util/profiling.rs @@ -93,16 +93,27 @@ fn json(&self) -> String { $( 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!( "{{ \"category\": {}, \"time_ms\": {}, - \"query_count\": {}, \"query_hits\": {} }}", + \"query_count\": {}, \"query_hits\": {} }},", stringify!($name), self.times.$name / 1_000_000, total, - format!("{:.2}", (((hits as f32) / (total as f32)) * 100.0)) + format!("{:.2}", hit_percent) )); )* + //remove the trailing ',' character + json.pop(); + json.push(']'); json -- 2.44.0