Skip to content

Commit

Permalink
[erlperf] introduce --init_all, --init_runner_all, --done_all
Browse files Browse the repository at this point in the history
Implements #17
  • Loading branch information
Maxim Fedorov committed May 24, 2022
1 parent 45852d9 commit 3758c6b
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/erlang.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
otp_version: ['23', '24', '25'']
otp_version: ['23', '24', '25']
os: [ubuntu-latest]
steps:
- uses: actions/checkout@v2
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## 2.0.2
- added convenience command line options: init_all, done_all, init_runner_all

## 2.0.1
- minor bugfixes (friendlier error reporting)

Expand Down
2 changes: 1 addition & 1 deletion src/erlperf.app.src
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{application, erlperf,
[{description, "Erlang Performance & Benchmarking Suite"},
{vsn, "2.0.1"},
{vsn, "2.0.2"},
{registered, [
erlperf_sup, erlperf_job_sup, erlperf_monitor,
erlperf_history, erlperf_file_log, erlperf_cluster_monitor
Expand Down
29 changes: 26 additions & 3 deletions src/erlperf_cli.erl
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ main(Args) ->
%% find all runners
Code0 = [parse_code(C) || C <- maps:get(code, RunOpts)],
%% find associated init, init_runner, done
{_, Code} = lists:foldl(fun callable/2, {RunOpts, Code0}, [init, init_runner, done]),
{_, Code} = lists:foldl(fun callable/2, {RunOpts, Code0},
[{init, init_all}, {init_runner, init_runner_all}, {done, done_all}]),
%% figure out whether concurrency run is requested
COpts = case maps:find(squeeze, RunOpts) of
{ok, true} ->
Expand Down Expand Up @@ -99,8 +100,24 @@ compile_errors([{_, []} | Tail]) ->
compile_errors([{L, [{_Anno, Mod, Err} | T1]} | Tail]) ->
lists:flatten(Mod:format_error(Err) ++ io_lib:format("~n", [])) ++ compile_errors([{L, T1} | Tail]).

callable(Type, {Args, Acc}) ->
{Args, merge_callable(Type, maps:get(Type, Args, []), Acc, [])}.
callable({Type, Default}, {Args, Acc}) ->
case maps:find(Type, Args) of
error when is_map_key(Default, Args) ->
%% default is set, no overrides
{Args, merge_callable(Type, lists:duplicate(length(Acc), [maps:get(Default, Args)]), Acc, [])};
error ->
%% no overrides, no default - most common case
{Args, merge_callable(Type, [], Acc, [])};
{ok, Overrides} when is_map_key(Default, Args) ->
%% some overrides, and the default as well
%% extend the Overrides array to expected size by adding default value
Def = [maps:get(Default, Args)],
Complete = Overrides ++ [Def || _ <- lists:seq(1, length(Acc) - length(Overrides))],
{Args, merge_callable(Type, Complete, Acc, [])};
{ok, NoDefault} ->
%% no default, but some arguments are defined
{Args, merge_callable(Type, NoDefault, Acc, [])}
end.

merge_callable(_Type, [], Acc, Merged) ->
lists:reverse(Merged) ++ Acc;
Expand Down Expand Up @@ -190,6 +207,12 @@ arguments() ->
help => "done code", nargs => 1, action => append},
#{name => init_runner, long => "-init_runner",
help => "init_runner code", nargs => 1, action => append},
#{name => init_all, long => "-init_all",
help => "default init code for all runners"},
#{name => done_all, long => "-done_all",
help => "default done code for all runners"},
#{name => init_runner_all, long => "-init_runner_all",
help => "default init_runner code for all runners"},
#{name => code,
help => "code to test", nargs => nonempty_list, action => extend}
]}.
Expand Down
32 changes: 29 additions & 3 deletions test/erlperf_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@

-export([
cmd_line_simple/1, cmd_line_verbose/1, cmd_line_compare/1,
cmd_line_squeeze/1, cmd_line_usage/1, cmd_line_init/1,
cmd_line_usage/1, cmd_line_init/1,
cmd_line_double/1, cmd_line_triple/1, cmd_line_pg/1, cmd_line_mfa/1,
cmd_line_recorded/1,
cmd_line_squeeze/0
cmd_line_squeeze/0, cmd_line_squeeze/1,
cmd_line_all/0, cmd_line_all/1
]).

-export([mfa_squeeze/0, mfa_squeeze/1, replay/1, do_anything/1]).
Expand Down Expand Up @@ -80,7 +81,8 @@ groups() ->
cmd_line_triple,
cmd_line_pg,
cmd_line_mfa,
cmd_line_recorded
cmd_line_recorded,
cmd_line_all
]},
{squeeze, [], [
mfa_squeeze
Expand Down Expand Up @@ -453,6 +455,30 @@ cmd_line_recorded(Config) ->
?assertMatch(["[{ets,insert,[test_ets_tab,{100,40}]},", "...]", "1" | _], string:lexemes(LN2, " ")),
ok.

cmd_line_all() ->
[{doc, "Test init_all, done_all, init_runner_all options "}].

%% ./erlperf 'runner(X)->timer:sleep(X).' 'runner(X)->timer:sleep(X).' 'runner(X)->timer:sleep(X).'
%% --init_all '5.' --init '1.' --init_runner_all 'ir(Z) -> Z * 2.' --init_runner '5.' --init_runner '2.' --done_all '2.'
cmd_line_all(Config) when is_list(Config) ->
Code = "runner(X)->timer:sleep(X).",
Code2 = "runner(Y)->timer:sleep(Y).",
%% how this test works:
%% --init_all returns 5 for all 3 tests, for code#1 --init is overridden to be 1.
%% --init_runner_all returns 2x of init result, but there is override for #1 and #2 returning 5 and 2
%% resulting delays are 5, 2 and 10.
Out = capture_io(fun () -> erlperf_cli:main(
[Code, Code2, Code, "--init_all", "5.", "--init", "1.", "--init_runner_all", "ir(Z) -> Z * 2.",
"--init_runner", "5.", "--init_runner", "2.",
"--done_all", "2.", "-s", "2", "--duration", "100"]) end), %% unrelated parts to make the test quicker
[{Code2, 1, C1, _, R}, {Code, 1, C2, _, R2}, {Code, 1, C3, _, R3}] = parse_out(Out),
%% tests sorting as well
?assert(C1 > 25 andalso C1 < 55, {qps, C1}), %% 2 ms delay
?assert(C2 > 10 andalso C2 < 25, {qps, C2}), %% 5 ms delay
?assert(C3 > 5 andalso C3 < 11, {qps, C3}), %% 10 ms delay
?assert(R > R2), %% 5 ms delay is less than 2 ms
?assert(R2 > R3). %% 5 ms delay is more than 10 ms

%%--------------------------------------------------------------------
%% record-replay

Expand Down

0 comments on commit 3758c6b

Please sign in to comment.