improving elixir code quality through static analysis and profiling

30
Improving Elixir code quality through static analysis and profiling A primer to Credo, Dialyzer, and Erlang profiling tools

Upload: james-wheaton

Post on 13-Apr-2017

599 views

Category:

Software


1 download

TRANSCRIPT

Page 1: Improving Elixir code quality through static analysis and profiling

Improving Elixir code quality through static analysis

and profiling

A primer to Credo, Dialyzer, and Erlang profiling tools

Page 2: Improving Elixir code quality through static analysis and profiling

reject apathetic code using the Elixir library credo

Page 3: Improving Elixir code quality through static analysis and profiling

Credo – Elixir code linter

•  github.com/rrrene/credo•  Focuses on teaching and code consistency •  Implements its own style guide

•  If you are a Rubyist it is best described as an opinionated mix between Inch and Rubocop.

Page 4: Improving Elixir code quality through static analysis and profiling

Credo’s Creed

•  Be consistent in your choices –  (i.e. apply the same rules everywhere)

•  Care about the readability of your code –  (e.g. when in doubt, spread text vertically rather

than horizontally)

•  And care about easier maintenance –  (avoid confusing names, etc.)

Page 5: Improving Elixir code quality through static analysis and profiling

This is especially important because we are such a young community.

All the code we put out there is worth its weight in gold if it is easy to comprehend and invites people to learn and contribute.

Page 6: Improving Elixir code quality through static analysis and profiling

Credo is your coding buddy

•  Shows refactoring opportunities in your code – Reports TODO and FIXME

•  Shows complex & duplicated code fragments •  Warns you about common mistakes •  Shows inconsistencies

in your naming scheme •  Helps you enforce a

desired coding style

Page 7: Improving Elixir code quality through static analysis and profiling
Page 8: Improving Elixir code quality through static analysis and profiling

What do all those arrows (↑ ↗ → ↘ ↓) mean?

•  Each issue is assigned a priority based on: –  a base priority set by the config –  a dynamic component based on:

•  violation severity •  location in the source code

•  Priorities hint at the importance of each issue –  ↑ highest and ↓ lowest

•  By default, only issues with a positive priority (↑ ↗ →) are part of the report – Show low priority issues with mix credo --strict

Page 9: Improving Elixir code quality through static analysis and profiling

Add Credo to your Travis CI build

defp deps do [ {:credo, "~> 0.3", only: [:dev, :test]} ]end

mix.exs

script: - mix test - mix credo --strict

.travis.yml

Page 10: Improving Elixir code quality through static analysis and profiling

Dialyzer a static analysis tool

Page 11: Improving Elixir code quality through static analysis and profiling

Dialyzer / Dialyxir What’s the difference?

•  Dialyzer will analyze Erlang and other languages that compile to BEAM bytecode for the Erlang VM.

•  Dialyxir provides Mix tasks to simplify use of Dialyzer in Elixir projects.

Page 12: Improving Elixir code quality through static analysis and profiling

Persistent Lookup Table (PLT) is cached output from dialyzer

$ mix dialyzer.pltStarting PLT Core Build ... this will take awhile

[ :erts, :kernel, :stdlib, :crypto, :public_key ]

batteries included

mix.exsdef project do [ app: :my_app, ..., dialyzer: [plt_add_apps: [:mnesia] ] # add your own appsend

Page 13: Improving Elixir code quality through static analysis and profiling

What does Dialyzer analyze?

It warns you about type mismatches! For better results, use Elixir typespecs ( @spec )

Checks if a function doesn’t terminate … and other issues that are commonly detected by static language compilers

Page 14: Improving Elixir code quality through static analysis and profiling
Page 15: Improving Elixir code quality through static analysis and profiling

Type specification ... is a supertype of the success typing

“Practical Type Inference Based on Success Typings” by Tobias Lindahl and Konstantinos Sagonas

TL;DR: it is a soft typing system –  Incorporates subtyping –  Allows for compositional, bottom-up type inference

Page 16: Improving Elixir code quality through static analysis and profiling

binary_tree.ex:179: Type specification 'Elixir.Exads.DataStructures.BinarySearchTree’ :breadth_first_search(#{}) -> [any()] is a supertype of the success typing: 'Elixir.Exads.DataStructures.BinarySearchTree’ :breadth_first_search(#{}) -> [any(),...]

@spec breadth_first_search(%{}) :: list(any)

before

after@spec breadth_first_search(%{}) :: nonempty_list(any)

Page 17: Improving Elixir code quality through static analysis and profiling

Add Dialyzer to your Travis CI build

def project do [ app: :my_app, ..., dialyzer: [plt_file: "./plt/.local.plt"] ]enddefp deps do [ {:dialyxir, "~> 0.3.3", only: :dev} ]end

mix.exs

before_script: - mix dialyzer.pltscript: - mix dialyzer --halt-exit-status

.travis.yml

Page 18: Improving Elixir code quality through static analysis and profiling

profiling

Page 19: Improving Elixir code quality through static analysis and profiling

Where are the bottlenecks in my application?

$ iex –S mixiex> :observer.start

:eflame.apply/5$ stack_to_flame.sh < stacks.out > flame.svg$ open flame.svg

$ mix profile.fprof

Page 20: Improving Elixir code quality through static analysis and profiling

iex> :observer.startA GUI tool for observing an Erlang system

– system information – application supervisor trees – process information – ETS tables – Mnesia tables – contains a frontend for Erlang tracing

with module ttb.

Page 21: Improving Elixir code quality through static analysis and profiling
Page 22: Improving Elixir code quality through static analysis and profiling
Page 23: Improving Elixir code quality through static analysis and profiling
Page 24: Improving Elixir code quality through static analysis and profiling
Page 25: Improving Elixir code quality through static analysis and profiling
Page 26: Improving Elixir code quality through static analysis and profiling

:eflame.apply/5github.com/proger/eflame

Page 27: Improving Elixir code quality through static analysis and profiling

mix profile.fprof

Profiles the given file or expression using Erlang’s fprof tool.

fprof can be useful when you want to discover the bottlenecks of a sequential code.

Page 28: Improving Elixir code quality through static analysis and profiling
Page 29: Improving Elixir code quality through static analysis and profiling

$ MIX_ENV=prod mix compile

$ time mix profile.fprof \

--callers=true \

--sort=own \

--details \

-e "Chess.run_test"

Page 30: Improving Elixir code quality through static analysis and profiling

mix credo

mix dialyzer

:observer.start

:eflame.apply/5

mix profile.fprof