Permalink
Browse files

add do='dump' option to <match>

  • Loading branch information...
1 parent bc9d400 commit 9abfa1a00af6f5472b74067488b75edc8fef2fc6 @nniclausse nniclausse committed Sep 9, 2010
Showing with 47 additions and 43 deletions.
  1. +4 −4 src/tsung/ts_mon_cache.erl
  2. +19 −23 src/tsung/ts_search.erl
  3. +17 −11 src/tsung_controller/ts_match_logger.erl
  4. +4 −2 src/tsung_controller/ts_mon.erl
  5. +3 −3 tsung-1.0.dtd
@@ -77,8 +77,8 @@ add(Data) ->
%% @spec add_match(Data::list(),{UserId::integer(),SessionId::integer(),RequestId::integer(),
%% TimeStamp::tuple()}) -> ok
-add_match(Data,{UserId,SessionId,RequestId,TimeStamp}) ->
- gen_server:cast(?MODULE, {add_match, Data, {UserId,SessionId,RequestId,TimeStamp}}).
+add_match(Data,{UserId,SessionId,RequestId,TimeStamp,Bin}) ->
+ gen_server:cast(?MODULE, {add_match, Data, {UserId,SessionId,RequestId,TimeStamp,Bin}}).
%%====================================================================
%% Server functions
@@ -124,9 +124,9 @@ handle_cast({add, Data}, State) when is_list(Data) ->
{noreply, LastState };
handle_cast({add, Data}, State) when is_tuple(Data) ->
{noreply,update_stats(Data, State)};
-handle_cast({add_match, Data=[First|_Tail],{UserId,SessionId,RequestId,TimeStamp}},
+handle_cast({add_match, Data=[First|_Tail],{UserId,SessionId,RequestId,TimeStamp,Bin}},
State=#state{stats=List, match=MatchList})->
- NewMatchList=lists:append([{UserId,SessionId,RequestId,TimeStamp,First}], MatchList),
+ NewMatchList=lists:append([{UserId,SessionId,RequestId,TimeStamp,First, Bin}], MatchList),
{noreply, State#state{stats = lists:append(Data, List), match = NewMatchList}};
handle_cast(_Msg, State) ->
@@ -128,21 +128,14 @@ match([Match=#match{'skip_headers'=http}|Tail], Data, Counts,DynVars) when is_bi
match([Match#match{'skip_headers'=no}|Tail], Data, Counts, DynVars)
end;
-match([Match=#match{'apply_to_content'=undefined}|Tail], Data, Counts,DynVars) when is_binary(Data)->
+match([Match=#match{'apply_to_content'=undefined}|Tail], Data, Counts,DynVars) ->
?DebugF("Matching Data size ~p; apply undefined~n",[size(Data)]),
- match([Match|Tail], binary_to_list(Data), Counts, DynVars);
-match([Match=#match{'apply_to_content'={Module,Fun}}|Tail], Data, Counts,DynVars) when is_binary(Data)->
+ match([Match|Tail], Data, Counts, [],DynVars);
+match([Match=#match{'apply_to_content'={Module,Fun}}|Tail], Data, Counts,DynVars) ->
?DebugF("Matching Data size ~p; apply ~p:~p~n",[size(Data),Module,Fun]),
NewData = Module:Fun(Data),
?DebugF("Match: apply result =~p~n",[NewData]),
- case is_binary(NewData) of
- true ->
- match([Match|Tail], binary_to_list(NewData), Counts, DynVars);
- false->
- match([Match|Tail], NewData, Counts, DynVars)
- end;
-match(Match, Data, Counts,DynVars) when is_list(Data) ->
- match(Match, Data, Counts, [], DynVars).
+ match([Match|Tail], NewData, Counts, [],DynVars).
%% @spec match(Match::#match{}, Data::binary() | list(), Count::tuple(),
%% Stats::list(), DynVars::term()) -> Count::integer()
@@ -151,38 +144,38 @@ match([], _Data, {Count,_, _,_}, Stats, _) ->
ts_mon:add(Stats),
Count;
match([Match=#match{regexp=RawRegExp,subst=Subst, do=Action, 'when'=When}
- |Tail], String,Counts,Stats,DynVars)->
+ |Tail], Data,Counts,Stats,DynVars)->
RegExp = case Subst of
true -> subst(RawRegExp, DynVars);
_ -> RawRegExp
end,
?DebugF("RegExp was ~p and now is ~p after substitution (~p)~n",[RawRegExp,RegExp,Subst]),
- case re:run(String, RegExp) of
+ case re:run(Data, RegExp) of
{When,_} ->
?LOGF("Ok Match (regexp=~p) do=~p~n",[RegExp,Action], ?INFO),
- setcount(Match, Counts, [{count, match}| Stats]);
+ setcount(Match, Counts, [{count, match}| Stats], Data);
When -> % nomatch
?LOGF("Bad Match (regexp=~p) do=~p~n",[RegExp, Action], ?INFO),
- setcount(Match, Counts, [{count, nomatch} | Stats]);
+ setcount(Match, Counts, [{count, nomatch} | Stats],Data);
{match,_} -> % match but when=nomatch
?LOGF("Ok Match (regexp=~p)~n",[RegExp], ?INFO),
case Action of
loop -> put(loop_count, 0);
restart -> put(restart_count, 0);
_ -> ok
end,
- match(Tail, String, Counts, [{count, match} | Stats],DynVars);
+ match(Tail, Data, Counts, [{count, match} | Stats],DynVars);
nomatch -> % nomatch but when=match
?LOGF("Bad Match (regexp=~p)~n",[RegExp], ?INFO),
case Action of
loop -> put(loop_count, 0);
restart -> put(restart_count, 0);
_ -> ok
end,
- match(Tail, String, Counts,[{count, nomatch} | Stats],DynVars);
+ match(Tail, Data, Counts,[{count, nomatch} | Stats],DynVars);
{error,_Error} ->
?LOGF("Error while matching: bad REGEXP (~p)~n", [RegExp], ?ERR),
- match(Tail, String, Counts,[{count, badregexp} | Stats],DynVars)
+ match(Tail, Data, Counts,[{count, badregexp} | Stats],DynVars)
end.
%%----------------------------------------------------------------------
@@ -193,13 +186,16 @@ match([Match=#match{regexp=RawRegExp,subst=Subst, do=Action, 'when'=When}
%% - if restart is true, we must start again the whole session, set count to MaxCount
%% - if stop is true, set count to 0
%%----------------------------------------------------------------------
-setcount(#match{do=continue}, {Count, _MaxC, _SessionId, _UserId}, Stats)->
+setcount(#match{do=continue}, {Count, _MaxC, _SessionId, _UserId}, Stats,_)->
ts_mon:add(Stats),
Count;
-setcount(#match{do=log}, {Count, MaxC, SessionId, UserId}, Stats)->
+setcount(#match{do=log}, {Count, MaxC, SessionId, UserId}, Stats,_)->
ts_mon:add_match(Stats,{UserId,SessionId,MaxC-Count}),
Count;
-setcount(#match{do=restart, max_restart=MaxRestart}, {Count, MaxC,SessionId,UserId}, Stats)->
+setcount(#match{do=dump}, {Count, MaxC, SessionId, UserId}, Stats,Data)->
+ ts_mon:add_match(Stats,{UserId,SessionId,MaxC-Count, Data}),
+ Count;
+setcount(#match{do=restart, max_restart=MaxRestart}, {Count, MaxC,SessionId,UserId}, Stats,_)->
CurRestart = get(restart_count),
Ids={UserId,SessionId,MaxC-Count},
?LOGF("Restart on (no)match ~p~n",[CurRestart], ?INFO),
@@ -217,7 +213,7 @@ setcount(#match{do=restart, max_restart=MaxRestart}, {Count, MaxC,SessionId,User
ts_mon:add_match([{count, match_restart} | Stats],Ids),
MaxC
end;
-setcount(#match{do=loop,loop_back=Back,max_loop=MaxLoop,sleep_loop=Sleep},{Count,_MaxC,_SessionId,_UserId},Stats)->
+setcount(#match{do=loop,loop_back=Back,max_loop=MaxLoop,sleep_loop=Sleep},{Count,_MaxC,_SessionId,_UserId},Stats,_)->
CurLoop = get(loop_count),
?LOGF("Loop on (no)match ~p~n",[CurLoop], ?INFO),
ts_mon:add([{count, match_loop} | Stats]),
@@ -235,7 +231,7 @@ setcount(#match{do=loop,loop_back=Back,max_loop=MaxLoop,sleep_loop=Sleep},{Count
timer:sleep(Sleep),
Count + 1 + Back
end;
-setcount(#match{do=abort}, {Count,MaxC,SessionId,UserId}, Stats) ->
+setcount(#match{do=abort}, {Count,MaxC,SessionId,UserId}, Stats,_) ->
ts_mon:add_match([{count, match_stop} | Stats],{UserId,SessionId,MaxC-Count}),
0.
@@ -47,8 +47,10 @@
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2,
code_change/3]).
--record(state, {filename, % log filename
+-record(state, {filename, % log filename
level, % type of backend: text|rrdtool|fullstats
+ dumpid=1, % current dump id
+ logdir,
fd % file descriptor
}).
@@ -77,7 +79,6 @@ stop() ->
add(Data) ->
gen_server:cast({global, ?MODULE}, {add, Data}).
-
%%%----------------------------------------------------------------------
%%% Callback functions from gen_server
%%%----------------------------------------------------------------------
@@ -98,7 +99,8 @@ init([LogDir]) ->
?LOG("starting match logger~n",?NOTICE),
io:format(Fd,"# timestamp userid sessionid requestid event~n",[]),
{ok, #state{ fd = Fd,
- filename = Filename
+ filename = Filename,
+ logdir = LogDir
}};
{error, Reason} ->
?LOGF("Can't open match log file! ~p~n",[Reason], ?ERR),
@@ -126,12 +128,12 @@ handle_call(Request, _From, State) ->
%% {stop, Reason, State} (terminate/2 is called)
%%----------------------------------------------------------------------
handle_cast({add, List}, State) when is_list(List)->
- lists:foreach(fun(X)-> log(X,State#state.fd) end, List),
- {noreply,State};
+ NewState=lists:foldl(fun(X,Acc)-> log(X,Acc) end,State, List),
+ {noreply,NewState};
handle_cast({add, Data}, State) when is_tuple(Data)->
- log(Data,State#state.fd),
- {noreply,State};
+ NewState=log(Data,State),
+ {noreply,NewState};
handle_cast({stop}, State) ->
{stop, normal, State};
@@ -171,8 +173,12 @@ code_change(_OldVsn, StateData, _Extra) ->
%%% Internal functions
%%%----------------------------------------------------------------------
-log({UserId,SessionId,RequestId,TimeStamp,{count, Val}}, File) ->
+log({UserId,SessionId,RequestId,TimeStamp,{count, Val},[]},State=#state{fd=File}) ->
TS=ts_utils:time2sec(TimeStamp),
- io:format(File,"~B ~B ~B ~B ~p~n",[TS,UserId,SessionId,RequestId,Val]).
-
-
+ io:format(File,"~B ~B ~B ~B ~p~n",[TS,UserId,SessionId,RequestId,Val]),
+ State;
+log({UserId,SessionId,RequestId,TimeStamp,{count, Val},Bin}, State=#state{logdir=LogDir, dumpid=Id}) ->
+ log({UserId,SessionId,RequestId,TimeStamp,{count, Val},[]}, State),
+ Filename=filename:join(LogDir, "match-"++ integer_to_list(Id) ++".dump"),
+ file:write_file(Filename,Bin),
+ State#state{dumpid=Id+1}.
@@ -106,10 +106,12 @@ add(nocache,Data) ->
add(Data) ->
ts_mon_cache:add(Data).
-add_match(Data=[Head|_],{UserId,SessionId,RequestId}) ->
+add_match(Data,{UserId,SessionId,RequestId}) ->
+ add_match(Data,{UserId,SessionId,RequestId,[]});
+add_match(Data=[Head|_],{UserId,SessionId,RequestId,Bin}) ->
TimeStamp=now(),
put(last_match,Head),
- ts_mon_cache:add_match(Data,{UserId,SessionId,RequestId,TimeStamp}).
+ ts_mon_cache:add_match(Data,{UserId,SessionId,RequestId,TimeStamp, Bin}).
status() ->
gen_server:call({global, ?MODULE}, {status}).
View
@@ -122,7 +122,7 @@ repeat | if | change_type | foreach)*>
<!ELEMENT match (#PCDATA)>
<!ATTLIST match
- do (continue|loop|abort|restart|log) "continue"
+ do (continue|loop|abort|restart|log|dump) "continue"
when (match|nomatch) "match"
subst (true|false) "false"
loop_back NMTOKEN "0"
@@ -296,8 +296,8 @@ repeat | if | change_type | foreach)*>
if | repeat | change_type | foreach )+>
<!ATTLIST for
var CDATA #REQUIRED
- from NMTOKEN #REQUIRED
- to NMTOKEN #REQUIRED
+ from CDATA #REQUIRED
+ to CDATA #REQUIRED
incr NMTOKEN "1">
<!ELEMENT foreach (request | thinktime | transaction | setdynvars | foreach |

0 comments on commit 9abfa1a

Please sign in to comment.