<commit_before>%% ===================================================================
%% @author <NAME>
%% @copyright (C): 2013 ACK CYFRONET AGH
%% This software is released under the MIT license
%% cited in 'LICENSE.txt'.
%% @end
%% ===================================================================
%% @doc:This test checks how test framework affects tests' results (when
%% we compare results to multiple_ping results).
%% @end
%% ===================================================================

-module(veilcluster_driver_multiple_ping_v2).
-export([new/1, run/4]).

-include("basho_bench.hrl").
-include("registered_names.hrl").
-include("communication_protocol_pb.hrl").

-define(LogLoop, 100).
-define(LogLoopTime, 60000000).

new(_Id) -> 
    Hosts = basho_bench_config:get(cluster_hosts),
    CertFile = basho_bench_config:get(cert_file),
    Pong = #atom{value = "pong"},
    PongBytes = erlang:iolist_to_binary(communication_protocol_pb:encode_atom(Pong)),
    PongAns = #answer{answer_status = "ok", worker_answer = PongBytes},
    PongAnsBytes = erlang:iolist_to_binary(communication_protocol_pb:encode_answer(PongAns)),
    Args = {Hosts, CertFile, PongAnsBytes, closed, [], {?LogLoop, os:timestamp()}},
    ?DEBUG("Ping test initialized with params: ~p~n", [Args]),
    {ok, Args}.

run(Action, KeyGen, _ValueGen, {Hosts, CertFile, PongAnsBytes, SocketState, Socket, {LogLoop, LastTime}}) ->
    NewLoopValue = case LogLoop of
      0 ->
        Now = os:timestamp(),
        case timer:now_diff(Now, LastTime) > ?LogLoopTime of
          true ->
            ?DEBUG("Loop log~n", []),
            {?LogLoop, Now};
          false ->
            {?LogLoop, LastTime}
        end;
      _ ->
        {LogLoop -1, LastTime}
    end,

    Host = lists:nth((KeyGen() rem length(Hosts)) + 1 , Hosts),

    try
      {NewSocketState, NewSocket} = case SocketState of
        ok -> {SocketState, Socket};
        _ -> wss:connect(Host, 5555, [{certfile, CertFile}, {cacertfile, CertFile}, auto_handshake])
      end,

  %%     NewState = {Hosts, CertFile, PongAnsBytes, SocketState, NewSocket},
      case {NewSocketState, NewSocket} of
          {ok, _} ->
                  try ping(Action, NewSocket, PongAnsBytes) of
                      ok -> {ok, {Hosts, CertFile, PongAnsBytes, NewSocketState, NewSocket, NewLoopValue}}
                  catch
                      R1:R2 ->
                        wss:close(NewSocket),
                        {error, R1, R2, {Hosts, CertFile, PongAnsBytes, closed, [], NewLoopValue}}
                  end;
          {error, Error} -> {error, {connect, Error}, {Hosts, CertFile, PongAnsBytes, closed, [], NewLoopValue}};
          Other -> {error, {unknown_error, Other}, {Hosts, CertFile, PongAnsBytes, closed, [], NewLoopValue}}
      end
    catch
      E1:E2 ->
        {error, {error_thrown, E1, E2}, {Hosts, CertFile, PongAnsBytes, closed, [], NewLoopValue}}
    end.

ping(Module, Socket, PongAnsBytes) ->
    Ping = #atom{value = "ping"},
    PingBytes = erlang:iolist_to_binary(communication_protocol_pb:encode_atom(Ping)),

    Message = #clustermsg{module_name = atom_to_list(Module), message_type = "atom",
                            message_decoder_name = "communication_protocol", answer_type = "atom",
                            answer_decoder_name = "communication_protocol", synch = true, protocol_version = 1, input = PingBytes},
    Msg = erlang:iolist_to_binary(communication_protocol_pb:encode_clustermsg(Message)),

    wss:send(Socket, Msg),
    Ans =
        case wss:recv(Socket, 5000) of
            {ok, Ans1} -> Ans1;<commit_msg>one small fix + try clause to prevent unexpected crash<commit_after>%% ===================================================================
%% @author <NAME>
%% @copyright (C): 2013 ACK CYFRONET AGH
%% This software is released under the MIT license
%% cited in 'LICENSE.txt'.
%% @end
%% ===================================================================
%% @doc:This test checks how test framework affects tests' results (when
%% we compare results to multiple_ping results).
%% @end
%% ===================================================================

-module(veilcluster_driver_multiple_ping_v2).
-export([new/1, run/4]).

-include("basho_bench.hrl").
-include("registered_names.hrl").
-include("communication_protocol_pb.hrl").

-define(LogLoop, 100).
-define(LogLoopTime, 60000000).

new(_Id) -> 
    Hosts = basho_bench_config:get(cluster_hosts),
    CertFile = basho_bench_config:get(cert_file),
    Pong = #atom{value = "pong"},
    PongBytes = erlang:iolist_to_binary(communication_protocol_pb:encode_atom(Pong)),
    PongAns = #answer{answer_status = "ok", worker_answer = PongBytes},
    PongAnsBytes = erlang:iolist_to_binary(communication_protocol_pb:encode_answer(PongAns)),
    Args = {Hosts, CertFile, PongAnsBytes, closed, [], {?LogLoop, os:timestamp()}},
    ?DEBUG("Ping test initialized with params: ~p~n", [Args]),
    {ok, Args}.

run(Action, KeyGen, _ValueGen, {Hosts, CertFile, PongAnsBytes, SocketState, Socket, {LogLoop, LastTime}}) ->
    NewLoopValue = case LogLoop of
      0 ->
        Now = os:timestamp(),
        case timer:now_diff(Now, LastTime) > ?LogLoopTime of
          true ->
            ?DEBUG("Loop log~n", []),
            {?LogLoop, Now};
          false ->
            {?LogLoop, LastTime}
        end;
      _ ->
        {LogLoop -1, LastTime}
    end,

    Host = lists:nth((KeyGen() rem length(Hosts)) + 1 , Hosts),

    try
      {NewSocketState, NewSocket} = case SocketState of
        ok -> {SocketState, Socket};
        _ -> wss:connect(Host, 5555, [{certfile, CertFile}, {cacertfile, CertFile}, auto_handshake])
      end,

  %%     NewState = {Hosts, CertFile, PongAnsBytes, SocketState, NewSocket},
      case {NewSocketState, NewSocket} of
          {ok, _} ->
                  try ping(Action, NewSocket, PongAnsBytes) of
                      ok -> {ok, {Hosts, CertFile, PongAnsBytes, NewSocketState, NewSocket, NewLoopValue}};
											Other-> %just in case, it should never happen
												?DEBUG("Some unexpected error: ~p~n", [Other]),
												{error,unexpected_error, {Hosts, CertFile, PongAnsBytes, closed, [], NewLoopValue}}
                  catch
                      _R1:R2 ->
                        wss:close(NewSocket),
                        {error, R2, {Hosts, CertFile, PongAnsBytes, closed, [], NewLoopValue}}
                  end;
          {error, Error} -> {error, {connect, Error}, {Hosts, CertFile, PongAnsBytes, closed, [], NewLoopValue}};
          Other -> {error, {unknown_error, Other}, {Hosts, CertFile, PongAnsBytes, closed, [], NewLoopValue}}
      end
    catch
      E1:E2 ->
        {error, {error_thrown, E1, E2}, {Hosts, CertFile, PongAnsBytes, closed, [], NewLoopValue}}
    end.

ping(Module, Socket, PongAnsBytes) ->
    Ping = #atom{value = "ping"},
    PingBytes = erlang:iolist_to_binary(communication_protocol_pb:encode_atom(Ping)),

    Message = #clustermsg{module_name = atom_to_list(Module), message_type = "atom",
                            message_decoder_name = "communication_protocol", answer_type = "atom",
                            answer_decoder_name = "communication_protocol", synch = true, protocol_version = 1, input = PingBytes},
    Msg = erlang:iolist_to_binary(communication_protocol_pb:encode_clustermsg(Message)),

    wss:send(Socket, Msg),
    Ans =
        case wss:recv(Socket, 5000) of
            {ok, Ans1} -> Ans1;
{'fraction_non_alphanumeric': 0.18230419673883988, 'fraction_numerical': 0.010558674151296445, 'mean_word_length': 3.178112786152987, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '1089343', 'n_tokens_mistral': 2467, 'n_tokens_neox': 2294, 'n_words': 513}