本文介绍了使用Erlang代码段在TSUNG中进行算术运算时,得到{badarith,[{erlang,'+',[error,0],[]}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用TSUNG-Erlang功能使用算术代码片段,但无法顺利通过;在我的TSUNG控制器的日志中得到以下错误,



TSUNG-Erlang Snippet,

 < setdynvars sourcetype =filefileid =NBILM_testUsersdelimiter =;为了= 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 ,_ _ = _ = _ = = _(= io io [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[
{MIN,_} = string:to_integer(Minstr),
{ok,Countern} = ts_dynvars:lookup(counter,DynVars,999),
Counternstr = :flatten(io_lib:format(〜p,[Countern])),
{Counternum,_} = string:to_integer(Counternstr),
Mnum1 = MAX + Counternum rem(2 - 1) ,
Mnum1结束。
'>
< var name =mnum/>
< / setdynvars>

来自TSUNG控制器的错误日志事件

  = INFO REPORT ==== 5-May-2017 :: 11:42:40 === 
ts_client:(5:< 0.134.0>)停止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}]}}}


=错误报告==== 5 - 5月-2017 :: 11:42:40 ===
**状态机< 0.134.0>终止
**最后的消息是{timeout,#Ref< 0.0.8.22>,end_thinktime}
**当状态==认为
**数据== {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}


终止原因=
{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}]}}}

如果有人可以指出正在做的不正确,这将是非常有帮助的。

解决方案

消息说,在 Mnum1 = MAX + Counternum rem(2 - 1),中,您尝试添加错误 0 (顺便说一句 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}



进一步验证验证 Maxfound Maxstr



您还可以使用,它将直接返回一个整数。


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.

解决方案

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中进行算术运算时,得到{badarith,[{erlang,'+',[error,0],[]}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 15:47