From 4e5ea0d27d4cd7937bcbbcf15339a3113f4482c0 Mon Sep 17 00:00:00 2001 From: Vladimir Dementyev Date: Fri, 26 Oct 2018 14:56:22 -0400 Subject: [PATCH] feature: add --json format --- bin/stackprof | 3 +++ lib/stackprof/report.rb | 5 +++++ 2 files changed, 8 insertions(+) diff --git a/bin/stackprof b/bin/stackprof index 3bd7bfd7..1dfd0644 100755 --- a/bin/stackprof +++ b/bin/stackprof @@ -8,6 +8,7 @@ parser = OptionParser.new(ARGV) do |o| o.banner = "Usage: stackprof [file.dump]+ [--text|--method=NAME|--callgrind|--graphviz]" o.on('--text', 'Text summary per method (default)'){ options[:format] = :text } + o.on('--json', 'JSON output (use with web viewers)'){ options[:format] = :json } o.on('--files', 'List of files'){ |f| options[:format] = :files } o.on('--limit [num]', Integer, 'Limit --text, --files, or --graphviz output to N entries'){ |n| options[:limit] = n } o.on('--sort-total', "Sort --text or --files output on total samples\n\n"){ options[:sort] = true } @@ -62,6 +63,8 @@ options.delete(:limit) if options[:limit] == 0 case options[:format] when :text report.print_text(options[:sort], options[:limit], options[:select_files], options[:reject_files], options[:select_names], options[:reject_names]) +when :json + report.print_json when :debug report.print_debug when :dump diff --git a/lib/stackprof/report.rb b/lib/stackprof/report.rb index 451bba8f..523646b3 100644 --- a/lib/stackprof/report.rb +++ b/lib/stackprof/report.rb @@ -68,6 +68,11 @@ def print_dump(f=STDOUT) f.puts Marshal.dump(@data.reject{|k,v| k == :files }) end + def print_json(f=STDOUT) + require "json" + f.puts JSON.generate(@data) + end + def print_stackcollapse raise "profile does not include raw samples (add `raw: true` to collecting StackProf.run)" unless raw = data[:raw]