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 :cry:

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 Announcing Top

jakub-zawislak
Hi everyone, I’m coming from the Symfony (PHP) framework. I like Phoenix, but it has a one thing that was build much better in the Symfo...
New
devonestes
Introducing assertions, the library that helps you write really great test assertions! GitHub: GitHub - devonestes/assertions: Helpful a...
New
bryanjos
Hi, I just published version 0.23.0 of Elixirscript. https://github.com/bryanjos/elixirscript/blob/master/CHANGELOG.md Most of the chan...
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
riverrun
I’ve just released version 3 of Comeonin, a password hashing library. The following small changes have been made: changes to the NIF c...
New
Crowdhailer
Experimenting with this code. OK.try do user <- fetch_user(1) cart <- fetch_cart(1) order = checkout(cart, user) save_orde...
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
mindok
What is ContEx? A pure Elixir server-side data plotting/charting library outputting SVG. It has nice barcharts in particular and works g...
New
woylie
I released Doggo, a collection of unstyled Phoenix components. https://github.com/woylie/doggo Features Unstyled Phoenix components....
New
mattludwigs
Grizzly is a library for working with Z-Wave devices. Z-Wave is a low-frequency radio protocol for controlling smart home devices on a me...
New

Other popular topics Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 126479 1222
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement