<commit_before>-module(mzb_api_bench).

-behaviour(mzb_pipeline).

-export([
    start_link/2,
    interrupt_bench/1,
    get_status/1,
    seconds/0,
    send_email_report/2,
    request_report/2,
    log_file/1,
    metrics_file/1,
    remote_path/2
]).


-ifdef(TEST).
-export([allocate_hosts/2]).
-endif.

-define(DEFLATE_FLUSH_INTERVAL, 10000).

%% mzb_pipeline callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
         terminate/2, workflow_config/1, handle_stage/3,
         handle_pipeline_status/2]).

start_link(Id, Params) ->
    mzb_pipeline:start_link(?MODULE, [Id, Params], []).

get_status(Pid) ->
    mzb_pipeline:call(Pid, status).

interrupt_bench(Pid) ->
    mzb_pipeline:stop(Pid).

request_report(Pid, Emails) ->
    mzb_pipeline:call(Pid, {request_report, Emails}).

init([Id, Params]) ->
    Now = os:timestamp(),
    StartTime = seconds(Now),
    % Acceptance tests assume the following purpose format: bench-<id>-<timestamp>
    % {{_, M, D}, {H, Mi, S}} = calendar:now_to_universal_time(now()),
    % Purpose = mzb_string:format("bench-~2.10.0B-~2.10.0B-~2.10.0B-~2.10.0B-~2.10.0B-~b", [M,D,H,Mi,S,Id]),
    Purpose = mzb_string:format("bench-~b-~b", [Id, StartTime]),
    Includes = mzb_bc:maps_get(includes, Params, []),
    VMArgs = case maps:find(vm_args, Params) of
        {ok, [_|_] = List} -> List;
        _ -> application:get_env(mzbench_api, vm_args, undefined)
    end,
    #{name := ScriptName} = maps:get(script, Params),
    MetricPrefix = generate_graphite_prefix(mzbl_script:get_benchname(ScriptName)),
    NodeInstallSpec =
        case application:get_env(mzbench_api, mzbench_rsync, undefined) of
            undefined ->
                GitRepo = application:get_env(mzbench_api, mzbench_git, undefined),
                GitBranch = maps:get(node_commit, Params),
                Branch = case GitBranch of
                    undefined ->
                        {ok, GitRev} = application:get_key(mzbench_api, vsn),
                        GitRev;
                    _ -> GitBranch
                end,
                mzbl_script:make_git_install_spec(GitRepo, Branch, "node");
            Remote -> mzbl_script:make_rsync_install_spec(Remote, "node", ["deps", "ebin", ".make"])
        end,
    BenchName =
        case maps:find(benchmark_name, Params) of
            {ok, undefined} -> ScriptName;
            {ok, BN} -> BN;
            error -> ScriptName
        end,
    Config = #{
        id => Id,<commit_msg>Reuse _build directory of rsynced node for faster development<commit_after>-module(mzb_api_bench).

-behaviour(mzb_pipeline).

-export([
    start_link/2,
    interrupt_bench/1,
    get_status/1,
    seconds/0,
    send_email_report/2,
    request_report/2,
    log_file/1,
    metrics_file/1,
    remote_path/2
]).


-ifdef(TEST).
-export([allocate_hosts/2]).
-endif.

-define(DEFLATE_FLUSH_INTERVAL, 10000).

%% mzb_pipeline callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
         terminate/2, workflow_config/1, handle_stage/3,
         handle_pipeline_status/2]).

start_link(Id, Params) ->
    mzb_pipeline:start_link(?MODULE, [Id, Params], []).

get_status(Pid) ->
    mzb_pipeline:call(Pid, status).

interrupt_bench(Pid) ->
    mzb_pipeline:stop(Pid).

request_report(Pid, Emails) ->
    mzb_pipeline:call(Pid, {request_report, Emails}).

init([Id, Params]) ->
    Now = os:timestamp(),
    StartTime = seconds(Now),
    % Acceptance tests assume the following purpose format: bench-<id>-<timestamp>
    % {{_, M, D}, {H, Mi, S}} = calendar:now_to_universal_time(now()),
    % Purpose = mzb_string:format("bench-~2.10.0B-~2.10.0B-~2.10.0B-~2.10.0B-~2.10.0B-~b", [M,D,H,Mi,S,Id]),
    Purpose = mzb_string:format("bench-~b-~b", [Id, StartTime]),
    Includes = mzb_bc:maps_get(includes, Params, []),
    VMArgs = case maps:find(vm_args, Params) of
        {ok, [_|_] = List} -> List;
        _ -> application:get_env(mzbench_api, vm_args, undefined)
    end,
    #{name := ScriptName} = maps:get(script, Params),
    MetricPrefix = generate_graphite_prefix(mzbl_script:get_benchname(ScriptName)),
    NodeInstallSpec =
        case application:get_env(mzbench_api, mzbench_rsync, undefined) of
            undefined ->
                GitRepo = application:get_env(mzbench_api, mzbench_git, undefined),
                GitBranch = maps:get(node_commit, Params),
                Branch = case GitBranch of
                    undefined ->
                        {ok, GitRev} = application:get_key(mzbench_api, vsn),
                        GitRev;
                    _ -> GitBranch
                end,
                mzbl_script:make_git_install_spec(GitRepo, Branch, "node");
            Remote -> mzbl_script:make_rsync_install_spec(Remote, "node", [])
        end,
    BenchName =
        case maps:find(benchmark_name, Params) of
            {ok, undefined} -> ScriptName;
            {ok, BN} -> BN;
            error -> ScriptName
        end,
    Config = #{
        id => Id,
{'fraction_non_alphanumeric': 0.14188784673717822, 'fraction_numerical': 0.017162243065256436, 'mean_word_length': 2.6637426900584797, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5974699', 'n_tokens_mistral': 1814, 'n_tokens_neox': 1771, 'n_words': 290}