forked from erszcz/tsung
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tsung_controller.erl
128 lines (119 loc) · 5.3 KB
/
tsung_controller.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
%%% This code was developped by IDEALX (http://IDEALX.org/) and
%%% contributors (their names can be found in the CONTRIBUTORS file).
%%% Copyright (C) 2000-2001 IDEALX
%%%
%%% This program is free software; you can redistribute it and/or modify
%%% it under the terms of the GNU General Public License as published by
%%% the Free Software Foundation; either version 2 of the License, or
%%% (at your option) any later version.
%%%
%%% This program is distributed in the hope that it will be useful,
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
%%% GNU General Public License for more details.
%%%
%%% You should have received a copy of the GNU General Public License
%%% along with this program; if not, write to the Free Software
%%% Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
%%%
%%% In addition, as a special exception, you have the permission to
%%% link the code of this program with any library released under
%%% the EPL license and distribute linked combinations including
%%% the two.
-module(tsung_controller).
-vc('$Id$ ').
-author('nicolas.niclausse@niclux.org').
-export([start/2, start_phase/3, stop/1, stop_all/1, status/1]).
-behaviour(application).
-include("ts_profile.hrl").
-include_lib("kernel/include/file.hrl").
%%----------------------------------------------------------------------
%% Func: start/2
%% Returns: {ok, Pid} |
%% {ok, Pid, State} |
%% {error, Reason}
%%----------------------------------------------------------------------
start(_Type, _StartArgs) ->
error_logger:tty(false),
{ok, {LogDir, _Name}} = ts_utils:setsubdir(?config(log_file)),
erlang:display("Log directory is: " ++ LogDir),
LogFile = filename:join(LogDir, atom_to_list(node()) ++ ".log"),
case error_logger:logfile({open, LogFile }) of
ok ->
case ts_controller_sup:start_link(LogDir) of
{ok, Pid} ->
{ok, Pid};
Error ->
?LOGF("Can't start ! ~p ~n",[Error], ?ERR),
Error
end;
{error, Reason} ->
Msg = "Error while opening log file: " ,
?LOGF(Msg ++ " ~p ~n",[Reason], ?ERR),
erlang:display(Msg ++ Reason),
{error, Reason}
end.
start_phase(load_config, _StartType, _PhaseArgs) ->
Conf = ?config(config_file),
Timeout = case file:read_file_info(Conf) of
{ok, #file_info{size=Size}} when Size > 10000000 -> % > 10MB
erlang:display(["Can take up to 5mn to read config ",Size]),
300000; % 10mn
{ok, #file_info{size=Size}} when Size > 1000000 -> % > 1MB
erlang:display(["Can take up to 3mn to read config ",Size]),
180000; % 5mn
{ok, #file_info{size=Size}} ->
120000 % 2mn
end,
case ts_config_server:read_config(Conf,Timeout) of
{error,Reason}->
erlang:display(["Config Error, aborting ! ", Reason]),
init:stop();
ok -> ok
end;
start_phase(start_os_monitoring, _StartType, _PhaseArgs) ->
ts_os_mon:activate();
start_phase(start_clients, _StartType, _PhaseArgs) ->
ts_mon:start_clients({?config(clients),
?config(dump),
?config(stats_backend)}).
%%----------------------------------------------------------------------
%% Func: status/1
%% Returns: any
%%----------------------------------------------------------------------
status([Host]) when is_atom(Host)->
_List = net_adm:world_list([Host]),
global:sync(),
Msg = case catch ts_mon:status() of
{Clients, Count, Connected, Interval, Phase} ->
S1 = io_lib:format("Tsung is running [OK]~n" ++
" Current request rate: ~.2f req/sec~n" ++
" Current users: ~p~n" ++
" Current connected users: ~p ~n",
[Count/Interval, Clients, Connected]),
{ok, Nodes, Ended_Beams} = ts_config_server:status(),
case {Phase, Nodes == Ended_Beams} of
{error, _} -> % newphase not initialised, first phase
S1 ++ " Current phase: 1";
{_, true} ->
S1 ++ " Current phase: last, waiting for pending clients";
{{ok,P}, _} ->
NPhases = (P div Nodes) + 1,
io_lib:format("~s Current phase: ~p",[S1,NPhases])
end;
{'EXIT', {noproc, _}} ->
"Tsung is not started"
end,
io:format("~s~n",[Msg]).
%%----------------------------------------------------------------------
%% Func: stop/1
%% Returns: any
%%----------------------------------------------------------------------
stop(_State) ->
stop.
%%----------------------------------------------------------------------
%% Func: stop_all/0
%% Returns: any
%%----------------------------------------------------------------------
stop_all(Arg) ->
ts_utils:stop_all(Arg,'ts_mon').