tmbb

tmbb

Incendium - easily profile your web applications with flamegraphs

Incendium

Easy profiling for your Phoenix controller actions (and other functions) using flamegraphs.

Example flamegraph: Example flamegraph — Incendium v0.5.0

Documentation can be found at https://hexdocs.pm/incendium.

Rationale

Profiling Elixir code is easy using the default Erlang tools, such as fprof.

These tools produce a lot of potentially useful data, but visualizing and interpreting all that data is not easy.

The erlang tool eflame contains some utilities to generate flamegraphs from the results of profiling your code

But… eflame expects you to manually run a bash script, which according to my reading seems to call a perl (!) script that generates an interactive SVG.

And although the generated SVGs support some minimal interaction, it’s possible to do better.

When developping a web application, one can take advantage of the web browser as a highly dynamic tool for visualization using SVG, HTML and Javascript.

Fortunately there is a very good javascrtip library to generate flamegraphs: d3-flame-graph.

By reading :eflame stacktrace samples and converting them into a format that d3-flame-graph can understand, we can render the flamegraph in a webpage.

That way, instead of the manual steps above you can just visit an URL in your web application.

Usage

To use incendium in your web application, you need to follow these steps:

1. Add Incendium as a dependency

def deps do
  [
    {:incendium, "~> 0.2.0"}
  ]
end

You can make it a :dev only dependency if you wish, but Incendium will only decorate your functions if you’re in :dev mode. Incendium decorators won’t decorate your functions in :prod (profilers such as eflame should never be used in :prod because they add a very significant overhead; your code will be ~10-12 times slower)

2. Create an Incendium Controller for your application

# lib/my_app_web/controllers/incencdium_controller.ex
defmodule MyApp.IncendiumController do
  use Incendium.Controller,
    routes_module: MyAppWeb.Router.Helpers,
    otp_app: :my_app
end

There is no need to define an accompanying view.

Currently the controller is not extensible (and there aren’t many natural extension points anyway).
Upon compilation, the controller will automcatically add the files incendium.js and incendium.css to your priv/static directory so that those static files will be served using the normal Phoenix mechanisms.
On unusual Phoenix apps which have static files in other places, this might not work as expected.
Currently there isn’t a way to override the place where the static files should be added.

3. Add the controller to your Router

# lib/my_app_web/controllers/router.ex

  require Incendium

  scope "/incendium", MyAppWeb do
    Incendium.routes(IncendiumController)
  end

4. Decorate the functions you want to profile

Incendium decorators depend on the decorator package.

defmodule MyAppWeb.MyContext.ExampleController do
  use MyAppWeb.Mandarin, :controller
  # Activate the incendium decorators
  use Incendium.Decorator

  # Each invocation of the `index/2` function will be traced and profiled.
  @decorate incendium_profile_with_tracing()
  def index(conn, params) do
    resources = MyContext.list_resources(params)
    render(conn, "index.html", resources: resources)
  end
end

Currently incendium only supports tracing profilers (which are very slow and not practical in production).
In the future we may support better options such as sampling profilers.

5. Visit the /incendium route to see the generated flamegraph

Each time you run a profiled function, a new stacktrace will be generated. Stacktraces are not currently saves, you can only access the latest one. In the future we might add a persistence layer that stores a number of stacktraces instead of keeping just the last one.

Here you can find an example flamegraph with explanations about how to interact with it .

Most Liked

PragTob

PragTob

:wave:

Thanks for the ping.

Sad things first, I’ve been battling hand/arm problems for close to 5 months and haven’t done any OSS or gaming or general fun things in the time, hence benchee might appear stale… and there’s also a lot of unreleased stuff on main from even before that :’(

We should definitely have that, and we might already have it!

There is a feature that has been on main forever (thanks to idea from @josevalim and implementation by @pablocostass ) to have profile_after which runs builtin profilers after benchmarks initial PR

It should be semi easy to extend it to support non builtin profilers/that you can give it custom args. Maybe it already does, can’t check right now.

That said, I worry about my hands and if the past is an indicator of the future won’t get to make a new release or changes any time soon. Sorry :cry: It’s scary for me so I’m minimizing what I can to have a chance to be “good” again. Once I am, fixing that damn mac bug in nano time measurements releasing a new versiona nd brushing up benchee (and simplecov) is top of the priority list.

tmbb

tmbb

Incendium can now (v0.3.0) integrate with Benchee to generate reproducible benchmarks with performance data displayed as flamegraphs (using samples from multiple iterations, of course).

The logical way would be to create an Incendium formatter for benchee, but unfortunately that’s impossible. Incendium needs to run the benchmark twice (one without profiling so that we get accurate runtimes and another with profiling so that we get flamegraphs). This means I need control before execution (to set up profiling and create some processes that will gather the relevant metrics generated by the profiler), during execution (so that I can run the benchmark suite tiwce) and after execution (as a normal benchee formatter, to generate the HTML report

The limitations above mean that you need to call Incendium.run(benchmark, options) instead of Benchee.run(benchmark, options_with_incendim_specific_parts).

Example HTML report here: https://hexdocs.pm/incendium/0.3.0/assets/Example.html
Note that the flamegraph width is scaled according to the scenario runtime (longer runtimes create wider flamegraphs). The scaling of flamegraph widths can be disabled with an option.

Pinging @PragTob because he might be interested in this. I’d appreciate a way of running this with Benchee.run/2 instead of having to call Incendum.run/2.

The old Pheonix-based features still work as before.

Where Next?

Popular in Libraries Top

kip
Image is an image processing library for Elixir. It is based upon the fabulous vix library that provides a libvips wrapper for Elixir. I...
574 16576 179
New
tmbb
I’ve been working on two packages (not on hex.pm yet) to build admin interfaces for phoenix apps: bureaucrat - which contains a bunch ...
New
Crowdhailer
Raxx is an alternative to Plug and is inspired by projects such as Rack(Ruby) and Ring(Clojure). 1.0-rc.1 is now available. To use it re...
New
zoltanszogyenyi
Hey everyone :wave: Excited to join this forum - I am one of the founders and current project maintainers of a popular and open-source U...
New
sabiwara
Dune is a sandbox for Elixir and aims to safely evaluate user-provided code. You can try it out using this basic Elixir playground made ...
New
tfwright
After working on it for a couple of months and using it in production for most of that time, today I’ve released LiveAdmin, a LiveView ba...
New
alisinabh
Hey everyone i’ve developed a library for Jalaali calendar for elixir which supports converting Gregorian dates to Jalaali and vice vers...
New
zorbash
I created Kitto a framework for dashboards inspired by Dashing. [demo] The distributed characteristics of Elixir and the low memory foo...
New
mischov
import Meeseeks.CSS html = HTTPoison.get!("https://news.ycombinator.com/").body for story <- Meeseeks.all(html, css("tr.athing")) do...
New
maltoe
Hello! Came here to announce ChromicPDF, a pet project PDF generator I’ve been working on for the past few months. Why another PDF gener...
New

Other popular topics Top

chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New

Sub Categories:

We're in Beta

About us Mission Statement