获取 badarith,[erlang,'+',[error,0],[],同时使用 Erlang 片段在 TSUNG 中执行算术运算

Posted

技术标签:

【中文标题】获取 badarith,[erlang,\'+\',[error,0],[],同时使用 Erlang 片段在 TSUNG 中执行算术运算【英文标题】:getting badarith,[erlang,'+',[error,0],[], while performing arithmetic operation in TSUNG using Erlang snippet获取 badarith,[erlang,'+',[error,0],[],同时使用 Erlang 片段在 TSUNG 中执行算术运算 【发布时间】:2017-10-03 12:56:06 【问题描述】:

我用TSUNG-Erlang函数写了一个算术sn-p,但无法成功通过它;在我的 TSUNG 控制器的日志中出现以下错误,

TSUNG-Erlang 片段,

<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>

TSUNG 控制器的错误日志事件,

   =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]]

如果有人能指出做错了什么,那将非常有帮助。

【问题讨论】:

MAXerror,而不是整数。 Maxstr 是不是以整数开头的字符串? 【参考方案1】:

消息说,在Mnum1 = MAX + Counternum rem ( 2 - 1 ), 行中,您尝试将error0 相加(顺便说一句,Counternum rem ( 2 - 1 ) 始终等于 0,所以这里肯定有错误)。

Max是MAX, _ = string:to_integer(Maxstr),的结果,如果Maxstr不是以整数开头的字符串则等于错误:

"123" 将返回 123,[] "123 ab" 将返回 123," ab" "a123" 将返回 error,no_integer an_atom 将返回 error,not_a_list

调试进一步验证MaxfoundMaxstr的值

您还可以使用函数io:fread/2 缩短代码,该函数将直接返回一个整数。

【讨论】:

同意!!我错误地输入了我用于调试的 arith 表达式,所以实际的 exprn 是“Mnum1 = Min + ((Counternum rem (Max-Min+1)))”,我尝试通过编写变量 vaue 来调试步骤,它就像下面

以上是关于获取 badarith,[erlang,'+',[error,0],[],同时使用 Erlang 片段在 TSUNG 中执行算术运算的主要内容,如果未能解决你的问题,请参考以下文章

Erlang中除以零的精确异常类型

Erlang gen_server:如何捕获错误?

[Erlang37]error/1 exit/1 exit/2 throw/1的区别

E1.获取Elixir/Erlang版本信息

在dtrace中获取Erlang消息的消息内容

如何获取 erlang 故障转储文件?