From a32290ff80ee81ca6e04e02ed221c3b8559812d1 Mon Sep 17 00:00:00 2001 From: Dmitry Vlasov Date: Fri, 19 May 2017 18:26:17 +0300 Subject: [PATCH 1/3] added 'hide_x_bottom_lines' and 'sort_alphabetically' options --- README.md | 16 ++++++++++++++++ lib/flamegraph.rb | 6 ++++++ 2 files changed, 22 insertions(+) diff --git a/README.md b/README.md index 9b0129f..427c65b 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,22 @@ end ``` +You can also pass some options: + +```ruby + +# will hide bottom x lines of the graph +Flamegraph.generate(filename, hide_x_bottom_lines: 20) do + # your work here +end + +# will order stacktraces alphabetically, like the original Flamegraph (https://github.com/brendangregg/FlameGraph) does +Flamegraph.generate(filename, sort_alphabetically: true) do + # your work here +end + +``` + ## Demo Demo of: https://github.com/SamSaffron/flamegraph/blob/master/demo/demo.rb diff --git a/lib/flamegraph.rb b/lib/flamegraph.rb index 6cfb441..3935b19 100644 --- a/lib/flamegraph.rb +++ b/lib/flamegraph.rb @@ -44,6 +44,12 @@ def self.generate(filename=nil, opts = {}) yield end end + if opts[:hide_x_bottom_lines] + backtraces.map! { |b| b[0..-(opts[:hide_bottom_lines].to_i + 1)] }.reject(&:empty?) + end + if opts[:sort_alphabetically] + backtraces.sort_by! {|b| b.reverse.join('---')} + end embed_resources = (filename && !opts.key?(:embed_resources)) || opts[:embed_resources] From cee62a83474093cdef1944009865564c702b00e2 Mon Sep 17 00:00:00 2001 From: Dmitry Vlasov Date: Fri, 19 May 2017 19:12:17 +0300 Subject: [PATCH 2/3] small fix --- lib/flamegraph.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/flamegraph.rb b/lib/flamegraph.rb index 3935b19..30c6af5 100644 --- a/lib/flamegraph.rb +++ b/lib/flamegraph.rb @@ -45,7 +45,7 @@ def self.generate(filename=nil, opts = {}) end end if opts[:hide_x_bottom_lines] - backtraces.map! { |b| b[0..-(opts[:hide_bottom_lines].to_i + 1)] }.reject(&:empty?) + backtraces.map! { |b| b[0..-(opts[:hide_x_bottom_lines].to_i + 1)] }.reject(&:empty?) end if opts[:sort_alphabetically] backtraces.sort_by! {|b| b.reverse.join('---')} From 5ade8ec61eeaa7133d8dea59718e8f4fb7d53b0f Mon Sep 17 00:00:00 2001 From: Dmitry Vlasov Date: Wed, 31 Mar 2021 01:19:32 +0300 Subject: [PATCH 3/3] prefer native flamegraph options with stackprof --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 427c65b..0e4a480 100644 --- a/README.md +++ b/README.md @@ -49,12 +49,13 @@ Flamegraph.generate(filename, hide_x_bottom_lines: 20) do end # will order stacktraces alphabetically, like the original Flamegraph (https://github.com/brendangregg/FlameGraph) does -Flamegraph.generate(filename, sort_alphabetically: true) do +Flamegraph.generate(filename, sort_alphabetically: true) do # TODO: consider https://gist.github.com/BrVer/9854d6fb5b9546987c49e0dcf8ead380 instead # your work here end ``` + ## Demo Demo of: https://github.com/SamSaffron/flamegraph/blob/master/demo/demo.rb