getting {badarith,[{erlang,'+',[error,0],[

2019-09-14 02:10发布

问题:

I have wriiten a arithmetic snippet using TSUNG-Erlang function but unable to get through it successfully ; getting following error in my TSUNG controller's log ,

TSUNG-Erlang Snippet,

<setdynvars sourcetype="file" fileid="NBILM_testUsers" delimiter=";" order="iter">
<var name="minnum"/>
<var name="maxnum"/>
</setdynvars>



          <setdynvars sourcetype="eval"
                code='fun({Pid,DynVars})->
                       {ok,Maxfound}=ts_dynvars:lookup(maxnum,DynVars),
                       Maxstr =   lists:flatten(io_lib:format("~p",[Maxfound])),
                       {MAX, _} = string:to_integer(Maxstr),
                       {ok,Minfound}=ts_dynvars:lookup(minnum,DynVars),
                       Minstr =   lists:flatten(io_lib:format("~p",[Minfound])),
                       {MIN, _} = string:to_integer(Minstr),
                       {ok,Countern} = ts_dynvars:lookup(counter,DynVars,999),
                       Counternstr =   lists:flatten(io_lib:format("~p",[Countern])),
                       {Counternum, _} = string:to_integer(Counternstr),
                       Mnum1 = MAX + Counternum rem ( 2 - 1 ),
                       Mnum1 end.
                '>
          <var name="mnum" />
        </setdynvars>

Erroneous log events from TSUNG Controller,

   =INFO REPORT==== 5-May-2017::11:42:40 ===
           ts_client:(5:<0.134.0>) Stop in state think, reason= {badarith,
                                                                 [{erlang,
                                                                   '+',
                                                                   [error,0],
                                                                   []},
                                                                  {erl_eval,
                                                                   do_apply,6,
                                                                   [{file,
                                                                     "erl_eval.erl"},
                                                                    {line,
                                                                     669}]},
                                                                  {erl_eval,
                                                                   expr,5,
                                                                   [{file,
                                                                     "erl_eval.erl"},
                                                                    {line,
                                                                     438}]},
                                                                  {erl_eval,
                                                                   exprs,5,
                                                                   [{file,
                                                                     "erl_eval.erl"},
                                                                    {line,
                                                                     122}]},
                                                                  {ts_client,
                                                                   handle_next_action,
                                                                   1,
                                                                   [{file,
                                                                     "src/tsung/ts_client.erl"},
                                                                    {line,
                                                                     459}]},
                                                                  {gen_fsm,
                                                                   handle_msg,
                                                                   7,
                                                                   [{file,
                                                                     "gen_fsm.erl"},
                                                                    {line,
                                                                     518}]},
                                                                  {proc_lib,
                                                                   init_p_do_apply,
                                                                   3,
                                                                   [{file,
                                                                     "proc_lib.erl"},
                                                                    {line,
                                                                     239}]}]}


    =ERROR REPORT==== 5-May-2017::11:42:40 ===
** State machine <0.134.0> terminating
** Last message in was {timeout,#Ref<0.0.8.22>,end_thinktime}
** When State == think
**      Data  == {state_rcv,none,
                     {{0,0,0,0},0},
                     undefined,0,10000,"xyz",80,ts_tcp,
                     {proto_opts,negociate,"/http-bind/",false,"/chat",
                         "binary",10,3,600000,infinity,infinity,32768,32768,
                         undefined,undefined,[],false,true},
                     false,1,undefined,true,undefined,
                     {1493,964755,255814},
                     18,18,false,undefined,0,[],<<>>,
                     {http,0,0,-1,
                         {none,none},
                         false,false,
                         {false,false},
                         [],"tsung",[]},
                     0,1,524288,524288,
                     [{tsung_userid,1}],
                     ts_http,[],undefined,full}


Reason for termination =
{badarith,[{erlang,'+',[error,0],[]},
              {erl_eval,do_apply,6,[{file,"erl_eval.erl"},{line,669}]},
              {erl_eval,expr,5,[{file,"erl_eval.erl"},{line,438}]},
              {erl_eval,exprs,5,[{file,"erl_eval.erl"},{line,122}]},
              {ts_client,handle_next_action,1,
                         [{file,"src/tsung/ts_client.erl"},{line,459}]},
              {gen_fsm,handle_msg,7,[{file,"gen_fsm.erl"},{line,518}]},
              {proc_lib,init_p_do_apply,3,
                        [{file,"proc_lib.erl"},{line,239}]}]}

It would be really helpful, if someone can point what am doing incorrectly.

回答1:

The message says that in the line Mnum1 = MAX + Counternum rem ( 2 - 1 ), you are trying to add error with 0 (by the way Counternum rem ( 2 - 1 ) is always equal to 0 so there must be an error here).

Max is the result of {MAX, _} = string:to_integer(Maxstr),, it is equal to error if Maxstr is not a string starting by an integer:

  • "123" will return {123,[]}
  • "123 ab" will return {123," ab"}
  • "a123" will return {error,no_integer}
  • an_atom will return {error,not_a_list}

To debug further verify the value of Maxfound and Maxstr

you can also shorten your code using the function io:fread/2 which will directly return an integer.



标签: erlang tsung erl