Skip to content

Instantly share code, notes, and snippets.

@RoadRunnr
Created August 10, 2021 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RoadRunnr/73eef1f6e2d5009acc4b7f00d2185e37 to your computer and use it in GitHub Desktop.
Save RoadRunnr/73eef1f6e2d5009acc4b7f00d2185e37 to your computer and use it in GitHub Desktop.
-module(testm).
-behaviour(gen_statem).
-export([start_link/0, run/1, run/2]).
-export([callback_mode/0, init/1, terminate/3]).
-export([handle_event/4]).
-define(SERVER, ?MODULE).
start_link() ->
gen_statem:start_link({local, ?SERVER}, ?MODULE, [], []).
callback_mode() -> handle_event_function.
init([]) ->
process_flag(trap_exit, true),
Data = data_next(#{cnt => 0}),
{ok, state_name, Data}.
handle_event({call,From}, _Msg, State, Data0) ->
Data = data_next(Data0),
{next_state, State, Data, [{reply,From,ok}]}.
terminate(_Reason, _State, _Data) ->
void.
data_next(#{cnt := Cnt0} = Data) ->
Cnt = (Cnt0 + 1) rem 256,
Data#{cnt => Cnt, bin => binary:copy(<<Cnt:8>>, 1024)}.
run(Cnt, Repeat) ->
io:format("exec time | min ms | max ms~n"
"----------+--------+--------~n"),
lists:foreach(
fun(X) ->
{Min, Max} = run(Cnt),
io:format(" ~8w | ~6w | ~6w~n", [X, Min, Max])
end, lists:seq(1, Repeat)),
ok.
run(Cnt) ->
{ok, Pid} = start_link(),
L = lists:seq(1, Cnt),
Ts0 = lists:map(
fun(_) ->
{T, _} = timer:tc(gen_statem, call, [Pid, do]),
T
end, L),
erlang:exit(Pid, normal),
Ts = lists:sort(Ts0),
{hd(Ts), lists:last(Ts)}.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment