Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gootik/metronome
Erlang timing library
https://github.com/gootik/metronome
erlang instrumentation metronome stats statsd stopwatch timer
Last synced: 4 months ago
JSON representation
Erlang timing library
- Host: GitHub
- URL: https://github.com/gootik/metronome
- Owner: gootik
- License: mit
- Created: 2017-09-01T16:02:00.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2017-09-05T11:01:53.000Z (over 7 years ago)
- Last Synced: 2024-09-30T03:42:11.116Z (4 months ago)
- Topics: erlang, instrumentation, metronome, stats, statsd, stopwatch, timer
- Language: Erlang
- Size: 14.6 KB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Metronome [![Hex.pm](https://img.shields.io/hexpm/v/metronome.svg)](https://hex.pm/packages/metronome)
=====A simple library to add instrumentation to your Erlang code. This is a more
dynamic way of doing `timer:tc/3` or even a lazier version of it.Metronome will measure runtime of parts of your code and calls a method on your
predefined module with the results. For example, if you compile your code with
`{d, 'metronome_callback_mod', 'sample_callback'}` and run the following:```erlang
-include_lib("metronome/include/metronome.hrl").?timed(total_sum,
begin
?timed(sum_1, First = lists:sum(lists:seq(0, 100))),
?timed(sum_2, Second = lists:sum(lists:seq(0, 100))),
_ = First + Second
end).
```You will see 3 calls to `sample_callback` module:
```erlang
sample_callback:tick(sum_1, Microseconds).
sample_callback:tick(sum_2, Microseconds).
sample_callback:tick(total_sum, Microseconds).
```
If you don't want to use the macros or if they are giving you trouble, you can
also explicitly add the start/stop points. The above code can be written like below
with no difference in execution:```erlang
{stopwatch_start, total_sum},{stopwatch_start, sum_1},
First = lists:sum(lists:seq(0, 100)),
{stopwatch_stop},{stopwatch_start, sum_2},
Second = lists:sum(lists:seq(0, 100)),
{stopwatch_stop},_ = First + Second,
{stopwatch_stop}.
```### Usage
1. Add the package to your rebar.config
```erlang
{deps, [
{metronome, "0.0.2"}
]}.
```2. Add the parse transform to your rebar.config
```erlang
{erl_opts, [
{parse_transform, metronome_transform}
]}.
```3. [Optional] Configure a custom callback module by using overrides:
```erlang
{overrides, [
{override, metronome, [
{erl_opts, [
{d, 'metronome_callback_mod', 'my_sample_mod'
}]
}]}
]}.
```4. Instrument your code:
```erlang
{stopwatch_start, some_name},
--- Intensive amount of work ---
{stopwatch_stop}.
```
or use the included macro:```erlang
-include_lib("metronome/include/metronome.hrl").?timed(some_name,
--- Intensive amount of work ---
).
```
### Why
I really wanted to learn more about parse transforms and also make my own life
easier. I usually mix this library with [statsderl](https://github.com/lpgauth/statsderl)
to make things really easy to report timing of different parts of my code.### Custom callback
If you want a customized callback versus the provided one, you have to create a
module that implements the `metronome_callback` behavior. An example using `statsderl`
would look like this:```erlang
-module(metronome_statsderl).-behavior(metronome_callback).
-export([
tick/2
]).tick(Name, Microseconds) ->
statsderl:timing(atom_to_list(Name), Microseconds, 1).```