From 3758c6bb4192b798e4929a0962a7ae27385b9460 Mon Sep 17 00:00:00 2001 From: Maxim Fedorov Date: Tue, 24 May 2022 13:32:16 -0700 Subject: [PATCH] [erlperf] introduce --init_all, --init_runner_all, --done_all Implements #17 --- .github/workflows/erlang.yml | 2 +- CHANGELOG.md | 3 +++ src/erlperf.app.src | 2 +- src/erlperf_cli.erl | 29 ++++++++++++++++++++++++++--- test/erlperf_SUITE.erl | 32 +++++++++++++++++++++++++++++--- 5 files changed, 60 insertions(+), 8 deletions(-) diff --git a/.github/workflows/erlang.yml b/.github/workflows/erlang.yml index af74006..035a551 100644 --- a/.github/workflows/erlang.yml +++ b/.github/workflows/erlang.yml @@ -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 diff --git a/CHANGELOG.md b/CHANGELOG.md index eca341f..cf77551 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/src/erlperf.app.src b/src/erlperf.app.src index 2ee12af..31e079a 100644 --- a/src/erlperf.app.src +++ b/src/erlperf.app.src @@ -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 diff --git a/src/erlperf_cli.erl b/src/erlperf_cli.erl index 44a99d4..04634be 100644 --- a/src/erlperf_cli.erl +++ b/src/erlperf_cli.erl @@ -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} -> @@ -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; @@ -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} ]}. diff --git a/test/erlperf_SUITE.erl b/test/erlperf_SUITE.erl index 0fff8b2..f79ac76 100644 --- a/test/erlperf_SUITE.erl +++ b/test/erlperf_SUITE.erl @@ -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]). @@ -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 @@ -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