Permalink
Browse files

add dynamic code evaluation in set_dynvars (TSUN-87)

SVN Revision: 906
  • Loading branch information...
1 parent 86d651b commit 0fe1b005fddcd000d2a7f15e924e830ab9769fc4 @nniclausse nniclausse committed Oct 13, 2008
Showing with 22 additions and 6 deletions.
  1. +2 −0 src/tsung/ts_client.erl
  2. +12 −1 src/tsung/ts_utils.erl
  3. +6 −4 src/tsung_controller/ts_config.erl
  4. +2 −1 tsung-1.0.dtd
View
@@ -342,6 +342,8 @@ handle_next_action(State) ->
%%----------------------------------------------------------------------
set_dynvars(erlang,{Module,Callback},_Vars,DynData) ->
Module:Callback({self(),DynData#dyndata.dynvars});
+set_dynvars(code,Fun,_Vars,DynData) ->
+ Fun({self(),DynData#dyndata.dynvars});
set_dynvars(random,{number,Start,End},Vars,_DynData) ->
lists:map(fun(_) -> integer_to_list(Start+random:uniform(End-Start)) end,Vars);
set_dynvars(random,{string,Length},Vars,_DynData) ->
View
@@ -41,7 +41,7 @@
make_dir_rec/1, is_ip/1, from_https/1, to_https/1, keymax/2,
check_sum/3, check_sum/5, clean_str/1, file_to_list/1,
decode_base64/1, encode_base64/1, to_lower/1, release_is_newer_or_eq/1,
- randomstr/1,urandomstr/1,urandomstr_noflat/1]).
+ randomstr/1,urandomstr/1,urandomstr_noflat/1, eval/1]).
level2int("debug") -> ?DEB;
level2int("info") -> ?INFO;
@@ -637,3 +637,14 @@ urandomstr(Size) when is_integer(Size), Size >= 0 ->
randomstr(Size) when is_integer(Size), Size >= 0 ->
lists:map(fun (_) -> random:uniform(25) + $a end, lists:seq(1,Size)).
+
+%%----------------------------------------------------------------------
+%% @spec eval(string()) -> term()
+%% @doc evaluate strings as Erlang code at runtime
+%%----------------------------------------------------------------------
+eval(Code) ->
+ {ok, Scanned, _} = erl_scan:string(Code),
+ {ok, Parsed} = erl_parse:parse_exprs(Scanned),
+ {value, Result, _} = erl_eval:exprs(Parsed, erl_eval:new_bindings()),
+ Result.
+
@@ -381,10 +381,7 @@ parse(#xmlElement{name=dyn_variable, attributes=Attrs},
CompiledXPathExp = mochiweb_xpath:compile_xpath(FlattenExpr),
{xpath,Name,CompiledXPathExp}
end,
- NewDynVar = case DynVars of
- undefined ->[DynVar];
- _->[DynVar|DynVars]
- end,
+ NewDynVar = [DynVar|DynVars],
?LOGF("Add new dyn variable=~p in session ~p~n",
[NewDynVar,CurS#session.id],?INFO),
Conf#config{ dynvar= NewDynVar };
@@ -540,6 +537,11 @@ parse(Element = #xmlElement{name=setdynvars, attributes=Attrs},
"erlang" ->
[Module,Callback] = string:tokens(getAttr(string,Attrs,callback,none),":"),
{setdynvars,erlang,{list_to_atom(Module),list_to_atom(Callback)},Vars};
+ "eval" ->
+ Snippet = getAttr(string,Attrs,code,""),
+ Fun= ts_utils:eval(Snippet),
+ true = is_function(Fun, 1),
+ {setdynvars,code,Fun,Vars};
"file" ->
Order = getAttr(atom,Attrs,order,iter),
FileId = getAttr(atom,Attrs,fileid,none),
View
@@ -206,8 +206,9 @@ repeat)*>
<!ELEMENT setdynvars (var*) >
<!ATTLIST setdynvars
- sourcetype (random_string | urandom_string | random_number | file | erlang ) #REQUIRED
+ sourcetype (random_string | urandom_string | random_number | file | erlang | eval) #REQUIRED
callback CDATA #IMPLIED
+ code CDATA #IMPLIED
fileid CDATA #IMPLIED
order (iter | random ) #IMPLIED
delimiter CDATA #IMPLIED

0 comments on commit 0fe1b00

Please sign in to comment.