Skip to content

Commit 81da767

Browse files
authored
Merge pull request #103 from palkan/feature/print-json
Add --json format
2 parents 1af2220 + 4e5ea0d commit 81da767

2 files changed

Lines changed: 8 additions & 0 deletions

File tree

bin/stackprof

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ parser = OptionParser.new(ARGV) do |o|
88
o.banner = "Usage: stackprof [file.dump]+ [--text|--method=NAME|--callgrind|--graphviz]"
99

1010
o.on('--text', 'Text summary per method (default)'){ options[:format] = :text }
11+
o.on('--json', 'JSON output (use with web viewers)'){ options[:format] = :json }
1112
o.on('--files', 'List of files'){ |f| options[:format] = :files }
1213
o.on('--limit [num]', Integer, 'Limit --text, --files, or --graphviz output to N entries'){ |n| options[:limit] = n }
1314
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
6263
case options[:format]
6364
when :text
6465
report.print_text(options[:sort], options[:limit], options[:select_files], options[:reject_files], options[:select_names], options[:reject_names])
66+
when :json
67+
report.print_json
6568
when :debug
6669
report.print_debug
6770
when :dump

lib/stackprof/report.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ def print_dump(f=STDOUT)
6868
f.puts Marshal.dump(@data.reject{|k,v| k == :files })
6969
end
7070

71+
def print_json(f=STDOUT)
72+
require "json"
73+
f.puts JSON.generate(@data)
74+
end
75+
7176
def print_stackcollapse
7277
raise "profile does not include raw samples (add `raw: true` to collecting StackProf.run)" unless raw = data[:raw]
7378

0 commit comments

Comments
 (0)