Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 159 lines (126 sloc) 5.114 kb
4aea292 @oliv3 Statistics module added
oliv3 authored
1 %%%-------------------------------------------------------------------
2 %%% File : yaws_stats.erl
3 %%% Author : Olivier Girondel <olivier@biniou.info>
4 %%% Description : Statistics module for Yaws
5 %%%
6 %%% Created : 11 Apr 2009 by Olivier Girondel <olivier@biniou.info>
7 %%%-------------------------------------------------------------------
8 -module(yaws_stats).
9
10 -behaviour(gen_server).
11
12 -include("yaws.hrl").
13
14 %% API
15 -export([start_link/0, stop/1]).
16
17 %% gen_server callbacks
18 -export([init/1, handle_call/3, handle_cast/2, handle_info/2,
19 terminate/2, code_change/3]).
20
21 -export([hit/0, sent/1]).
22 -export([get/1]).
23
24 %% statistics
25 -record(stats, {
26 hits = 0,
27 sent = 0
28 }).
29
30 %%====================================================================
31 %% API
32 %%====================================================================
33 %%--------------------------------------------------------------------
34 %% Function: start_link() -> {ok,Pid} | ignore | {error,Error}
35 %% Description: Starts the server
36 %%--------------------------------------------------------------------
37 start_link() ->
38 gen_server:start_link(?MODULE, [], []).
39
40
41 stop(Pid) ->
42 gen_server:cast(Pid, {stop}).
43
44
45 hit() ->
46 case get_stats() of
47 undefined ->
48 ok;
49 Pid ->
50 gen_server:cast(Pid, {hit})
51 end.
52
53
54 sent(Bytes) ->
55 case get_stats() of
56 undefined ->
57 ok;
58 Pid ->
59 gen_server:cast(Pid, {sent, Bytes})
60 end.
61
62
63 get(Pid) ->
64 case Pid of
65 undefined ->
66 undefined;
67 Pid ->
68 gen_server:call(Pid, {get})
69 end.
70
71
72 %%====================================================================
73 %% gen_server callbacks
74 %%====================================================================
75
76 %%--------------------------------------------------------------------
77 %% Function: init(Args) -> {ok, State} |
78 %% {ok, State, Timeout} |
79 %% ignore |
80 %% {stop, Reason}
81 %% Description: Initiates the server
82 %%--------------------------------------------------------------------
83 init([]) ->
84 {ok, #stats{}}.
85
86 %%--------------------------------------------------------------------
87 %% Function: %% handle_call(Request, From, State) -> {reply, Reply, State} |
88 %% {reply, Reply, State, Timeout} |
89 %% {noreply, State} |
90 %% {noreply, State, Timeout} |
91 %% {stop, Reason, Reply, State} |
92 %% {stop, Reason, State}
93 %% Description: Handling call messages
94 %%--------------------------------------------------------------------
95 handle_call({get}, _From, State) ->
96 Reply = {State#stats.hits, State#stats.sent},
97 {reply, Reply, State};
98
99
100 handle_call(_Request, _From, State) ->
101 Reply = ok,
102 {reply, Reply, State}.
103
104 %%--------------------------------------------------------------------
105 %% Function: handle_cast(Msg, State) -> {noreply, State} |
106 %% {noreply, State, Timeout} |
107 %% {stop, Reason, State}
108 %% Description: Handling cast messages
109 %%--------------------------------------------------------------------
110 handle_cast({hit}, Stats) ->
111 NewHits = Stats#stats.hits+1,
112 {noreply, Stats#stats{hits=NewHits}};
113
114
115 handle_cast({sent, Bytes}, Stats) ->
116 NewSent = Stats#stats.sent+Bytes,
117 {noreply, Stats#stats{sent=NewSent}};
118
119
120 handle_cast({stop}, Stats) ->
121 {stop, normal, Stats};
122
123
124 handle_cast(_Msg, State) ->
125 {noreply, State}.
126
127 %%--------------------------------------------------------------------
128 %% Function: handle_info(Info, State) -> {noreply, State} |
129 %% {noreply, State, Timeout} |
130 %% {stop, Reason, State}
131 %% Description: Handling all non call/cast messages
132 %%--------------------------------------------------------------------
133 handle_info(_Info, State) ->
134 {noreply, State}.
135
136 %%--------------------------------------------------------------------
137 %% Function: terminate(Reason, State) -> void()
138 %% Description: This function is called by a gen_server when it is about to
139 %% terminate. It should be the opposite of Module:init/1 and do any necessary
140 %% cleaning up. When it returns, the gen_server terminates with Reason.
141 %% The return value is ignored.
142 %%--------------------------------------------------------------------
143 terminate(Reason, State) ->
144 io:format("yaws_stat ~p terminated (~p, ~p)~n", [self(), Reason, State]),
145 ok.
146
147 %%--------------------------------------------------------------------
148 %% Func: code_change(OldVsn, State, Extra) -> {ok, NewState}
149 %% Description: Convert process state when code is changed
150 %%--------------------------------------------------------------------
151 code_change(_OldVsn, State, _Extra) ->
152 {ok, State}.
153
154 %%--------------------------------------------------------------------
155 %%% Internal functions
156 %%--------------------------------------------------------------------
157 get_stats() ->
158 (erlang:get(sc))#sconf.stats.
Something went wrong with that request. Please try again.