Manage global reductions delta ourselves
[beam_stats.git] / test / beam_stats_consumer_statsd_SUITE.erl
1 -module(beam_stats_consumer_statsd_SUITE).
2
3 -include_lib("beam_stats/include/beam_stats.hrl").
4 -include_lib("beam_stats/include/beam_stats_ets_table.hrl").
5 -include_lib("beam_stats/include/beam_stats_process.hrl").
6 -include_lib("beam_stats/include/beam_stats_process_ancestry.hrl").
7 -include_lib("beam_stats/include/beam_stats_processes.hrl").
8
9 -export(
10 [ all/0
11 , groups/0
12 ]).
13
14 %% Test cases
15 -export(
16 [ t_full_cycle/1
17 ]).
18
19 -define(GROUP, beam_stats_consumer_statsd).
20
21 %% ============================================================================
22 %% Common Test callbacks
23 %% ============================================================================
24
25 all() ->
26 [{group, ?GROUP}].
27
28 groups() ->
29 Tests =
30 [ t_full_cycle
31 ],
32 Properties = [],
33 [{?GROUP, Properties, Tests}].
34
35 %% ============================================================================
36 %% Test cases
37 %% ============================================================================
38
39 t_full_cycle(_Cfg) ->
40 meck:new(beam_stats_source),
41 _BEAMStatsExpected = meck_expect_beam_stats(),
42
43 {ok,[hope,beam_stats]} = application:ensure_all_started(beam_stats),
44 ct:log("beam_stats started~n"),
45 ServerPort = 8125,
46 {ok, ServerSocket} = gen_udp:open(ServerPort, [binary, {active, false}]),
47 ct:log("UDP server started started~n"),
48 {ok, _} = beam_stats_consumer:add(beam_stats_consumer_statsd,
49 [ {consumption_interval , 60000}
50 , {dst_host , "localhost"}
51 , {dst_port , ServerPort}
52 , {src_port , 8124}
53 , {num_msgs_per_packet , 10}
54 ]
55 ),
56 ct:log("consumer added~n"),
57 _ = meck_expect_beam_stats(
58 % Double the original values, so that deltas will equal originals after
59 % 1 update of new beam_stats_state:t()
60 [ {io_bytes_in , 6}
61 , {io_bytes_out , 14}
62 , {context_switches , 10}
63 , {reductions , 18}
64 ]
65 ),
66 ct:log("meck_expect_beam_stats ok~n"),
67 {} = beam_stats_producer:sync_produce_consume(),
68 ct:log("produced and consumed~n"),
69 ok = application:stop(beam_stats),
70 ct:log("beam_stats stopped~n"),
71
72 ResultOfReceive1 = gen_udp:recv(ServerSocket, 0),
73 ResultOfReceive2 = gen_udp:recv(ServerSocket, 0),
74 ResultOfReceive3 = gen_udp:recv(ServerSocket, 0),
75 ResultOfReceive4 = gen_udp:recv(ServerSocket, 0),
76 ok = gen_udp:close(ServerSocket),
77 {ok, {_, _, PacketReceived1}} = ResultOfReceive1,
78 {ok, {_, _, PacketReceived2}} = ResultOfReceive2,
79 {ok, {_, _, PacketReceived3}} = ResultOfReceive3,
80 {ok, {_, _, PacketReceived4}} = ResultOfReceive4,
81 ct:log("PacketReceived1: ~n~s~n", [PacketReceived1]),
82 ct:log("PacketReceived2: ~n~s~n", [PacketReceived2]),
83 ct:log("PacketReceived3: ~n~s~n", [PacketReceived3]),
84 ct:log("PacketReceived4: ~n~s~n", [PacketReceived4]),
85 PacketsCombined =
86 << PacketReceived1/binary
87 , PacketReceived2/binary
88 , PacketReceived3/binary
89 , PacketReceived4/binary
90 >>,
91 ct:log("PacketsCombined: ~n~s~n", [PacketsCombined]),
92 MsgsExpected =
93 [ <<"beam_stats_v0.node_foo_host_bar.io.bytes_in:3|g">>
94 , <<"beam_stats_v0.node_foo_host_bar.io.bytes_out:7|g">>
95 , <<"beam_stats_v0.node_foo_host_bar.context_switches:5|g">>
96 , <<"beam_stats_v0.node_foo_host_bar.reductions:9|g">>
97 , <<"beam_stats_v0.node_foo_host_bar.run_queue:17|g">>
98 , <<"beam_stats_v0.node_foo_host_bar.memory.mem_type_foo:1|g">>
99 , <<"beam_stats_v0.node_foo_host_bar.memory.mem_type_bar:2|g">>
100 , <<"beam_stats_v0.node_foo_host_bar.memory.mem_type_baz:3|g">>
101 , <<"beam_stats_v0.node_foo_host_bar.ets_table.size.foo.NAMED:5|g">>
102 , <<"beam_stats_v0.node_foo_host_bar.ets_table.memory.foo.NAMED:40|g">>
103 , <<"beam_stats_v0.node_foo_host_bar.ets_table.size.bar.TID:16|g">>
104 , <<"beam_stats_v0.node_foo_host_bar.ets_table.memory.bar.TID:128|g">>
105
106 % Processes totals
107 , <<"beam_stats_v0.node_foo_host_bar.processes_count_all:4|g">>
108 , <<"beam_stats_v0.node_foo_host_bar.processes_count_exiting:0|g">>
109 , <<"beam_stats_v0.node_foo_host_bar.processes_count_garbage_collecting:0|g">>
110 , <<"beam_stats_v0.node_foo_host_bar.processes_count_registered:1|g">>
111 , <<"beam_stats_v0.node_foo_host_bar.processes_count_runnable:0|g">>
112 , <<"beam_stats_v0.node_foo_host_bar.processes_count_running:3|g">>
113 , <<"beam_stats_v0.node_foo_host_bar.processes_count_suspended:0|g">>
114 , <<"beam_stats_v0.node_foo_host_bar.processes_count_waiting:1|g">>
115
116 % Process 1
117 , <<"beam_stats_v0.node_foo_host_bar.process_memory.named--reg_name_foo:15|g">>
118 , <<"beam_stats_v0.node_foo_host_bar.process_total_heap_size.named--reg_name_foo:25|g">>
119 , <<"beam_stats_v0.node_foo_host_bar.process_stack_size.named--reg_name_foo:10|g">>
120 , <<"beam_stats_v0.node_foo_host_bar.process_message_queue_len.named--reg_name_foo:0|g">>
121
122 % Process 2
123 , <<"beam_stats_v0.node_foo_host_bar.process_memory.spawned-via--bar_mod-bar_fun-1--NONE--NONE:25|g">>
124 , <<"beam_stats_v0.node_foo_host_bar.process_total_heap_size.spawned-via--bar_mod-bar_fun-1--NONE--NONE:35|g">>
125 , <<"beam_stats_v0.node_foo_host_bar.process_stack_size.spawned-via--bar_mod-bar_fun-1--NONE--NONE:40|g">>
126 , <<"beam_stats_v0.node_foo_host_bar.process_message_queue_len.spawned-via--bar_mod-bar_fun-1--NONE--NONE:5|g">>
127
128 % Process 3 and 4, aggregated by origin
129 , <<"beam_stats_v0.node_foo_host_bar.process_memory.spawned-via--baz_mod-baz_fun-3--baz_otp_mod-baz_otp_fun-2--PID-PID:30|g">>
130 , <<"beam_stats_v0.node_foo_host_bar.process_total_heap_size.spawned-via--baz_mod-baz_fun-3--baz_otp_mod-baz_otp_fun-2--PID-PID:45|g">>
131 , <<"beam_stats_v0.node_foo_host_bar.process_stack_size.spawned-via--baz_mod-baz_fun-3--baz_otp_mod-baz_otp_fun-2--PID-PID:55|g">>
132 , <<"beam_stats_v0.node_foo_host_bar.process_message_queue_len.spawned-via--baz_mod-baz_fun-3--baz_otp_mod-baz_otp_fun-2--PID-PID:1|g">>
133 ],
134 MsgsReceived = binary:split(PacketsCombined, <<"\n">>, [global, trim]),
135 RemoveExpectedFromReceived =
136 fun (Expected, Received) ->
137 ct:log(
138 "Looking for expected msg ~p in remaining received ~p~n",
139 [Expected, Received]
140 ),
141 true = lists:member(Expected, Received),
142 Received -- [Expected]
143 end,
144 [] = lists:foldl(RemoveExpectedFromReceived, MsgsReceived, MsgsExpected),
145 meck:unload(beam_stats_source).
146
147 meck_expect_beam_stats() ->
148 meck_expect_beam_stats([]).
149
150 meck_expect_beam_stats(Overrides) ->
151 IOBytesIn = hope_kv_list:get(Overrides, io_bytes_in , 3),
152 IOBytesOut = hope_kv_list:get(Overrides, io_bytes_out, 7),
153 ContextSwitches = hope_kv_list:get(Overrides, context_switches, 5),
154 Reductions = hope_kv_list:get(Overrides, reductions, 9),
155 Pid0 = list_to_pid("<0.0.0>"),
156 Pid1 = list_to_pid("<0.1.0>"),
157 Pid2 = list_to_pid("<0.2.0>"),
158 Pid3 = list_to_pid("<0.3.0>"),
159 Pid4 = list_to_pid("<0.4.0>"),
160 Process1 =
161 #beam_stats_process
162 { pid = Pid1
163 , registered_name = {some, reg_name_foo}
164 , ancestry =
165 #beam_stats_process_ancestry
166 { raw_initial_call = {foo_mod, foo_fun, 2}
167 , otp_initial_call = none
168 , otp_ancestors = none
169 }
170 , status = running
171 , memory = 15
172 , total_heap_size = 25
173 , stack_size = 10
174 , message_queue_len = 0
175 },
176 Process2 =
177 #beam_stats_process
178 { pid = Pid2
179 , registered_name = none
180 , ancestry =
181 #beam_stats_process_ancestry
182 { raw_initial_call = {bar_mod, bar_fun, 1}
183 , otp_initial_call = none
184 , otp_ancestors = none
185 }
186 , status = running
187 , memory = 25
188 , total_heap_size = 35
189 , stack_size = 40
190 , message_queue_len = 5
191 },
192 Process3 =
193 #beam_stats_process
194 { pid = Pid3
195 , registered_name = none
196 , ancestry =
197 #beam_stats_process_ancestry
198 { raw_initial_call = {baz_mod, baz_fun, 3}
199 , otp_initial_call = {some, {baz_otp_mod, baz_otp_fun, 2}}
200 , otp_ancestors = {some, [Pid0, Pid1]}
201 }
202 , status = running
203 , memory = 25
204 , total_heap_size = 35
205 , stack_size = 40
206 , message_queue_len = 1
207 },
208 Process4 =
209 #beam_stats_process
210 { pid = Pid4
211 , registered_name = none
212 , ancestry =
213 #beam_stats_process_ancestry
214 { raw_initial_call = {baz_mod, baz_fun, 3}
215 , otp_initial_call = {some, {baz_otp_mod, baz_otp_fun, 2}}
216 , otp_ancestors = {some, [Pid0, Pid1]}
217 }
218 , status = waiting
219 , memory = 5
220 , total_heap_size = 10
221 , stack_size = 15
222 , message_queue_len = 0
223 },
224 Processes =
225 #beam_stats_processes
226 { individual_stats =
227 [ Process1
228 , Process2
229 , Process3
230 , Process4
231 ]
232 , count_all = 4
233 , count_exiting = 0
234 , count_garbage_collecting = 0
235 , count_registered = 1
236 , count_runnable = 0
237 , count_running = 3
238 , count_suspended = 0
239 , count_waiting = 1
240 },
241 ETSTableStatsFoo =
242 #beam_stats_ets_table
243 { id = foo
244 , name = foo
245 , size = 5
246 , memory = 40
247 },
248 ETSTableStatsBarA =
249 #beam_stats_ets_table
250 { id = 37
251 , name = bar
252 , size = 8
253 , memory = 64
254 },
255 ETSTableStatsBarB =
256 #beam_stats_ets_table
257 { id = 38
258 , name = bar
259 , size = 8
260 , memory = 64
261 },
262 meck:expect(beam_stats_source, erlang_memory,
263 fun () -> [{mem_type_foo, 1}, {mem_type_bar, 2}, {mem_type_baz, 3}] end),
264 meck:expect(beam_stats_source, erlang_node,
265 fun () -> 'node_foo@host_bar' end),
266 meck:expect(beam_stats_source, erlang_registered,
267 fun () -> [reg_name_foo] end),
268 meck:expect(beam_stats_source, erlang_statistics,
269 fun (io ) -> {{input, IOBytesIn}, {output, IOBytesOut}}
270 ; (context_switches) -> {ContextSwitches, 0}
271 ; (reductions ) -> {Reductions, undefined} % 2nd element is unused
272 ; (run_queue ) -> 17
273 end
274 ),
275 meck:expect(beam_stats_source, ets_all,
276 fun () -> [foo, 37, 38] end),
277 meck:expect(beam_stats_source, erlang_system_info,
278 fun (wordsize) -> 8 end),
279 meck:expect(beam_stats_source, ets_info,
280 fun (foo, memory) -> 5
281 ; (foo, name ) -> foo
282 ; (foo, size ) -> 5
283
284 ; (37 , memory) -> 8
285 ; (37 , name ) -> bar
286 ; (37 , size ) -> 8
287
288 ; (38 , memory) -> 8
289 ; (38 , name ) -> bar
290 ; (38 , size ) -> 8
291 end
292 ),
293 meck:expect(beam_stats_source, erlang_processes,
294 fun () -> [Pid1, Pid2, Pid3, Pid4] end),
295 meck:expect(beam_stats_source, os_timestamp,
296 fun () -> {1, 2, 3} end),
297 meck:expect(beam_stats_source, erlang_process_info,
298 fun (P, K) when P == Pid1 ->
299 case K
300 of dictionary -> {K, []}
301 ; initial_call -> {K, {foo_mod, foo_fun, 2}}
302 ; registered_name -> {K, reg_name_foo}
303 ; status -> {K, running}
304 ; memory -> {K, 15}
305 ; total_heap_size -> {K, 25}
306 ; stack_size -> {K, 10}
307 ; message_queue_len -> {K, 0}
308 end
309 ; (P, K) when P == Pid2 ->
310 case K
311 of dictionary -> {K, []}
312 ; initial_call -> {K, {bar_mod, bar_fun, 1}}
313 ; registered_name -> []
314 ; status -> {K, running}
315 ; memory -> {K, 25}
316 ; total_heap_size -> {K, 35}
317 ; stack_size -> {K, 40}
318 ; message_queue_len -> {K, 5}
319 end
320 ; (P, K) when P == Pid3 ->
321 Dict =
322 [ {'$initial_call', {baz_otp_mod, baz_otp_fun, 2}}
323 , {'$ancestors' , [Pid0, Pid1]}
324 ],
325 case K
326 of dictionary -> {K, Dict}
327 ; initial_call -> {K, {baz_mod, baz_fun, 3}}
328 ; registered_name -> []
329 ; status -> {K, running}
330 ; memory -> {K, 25}
331 ; total_heap_size -> {K, 35}
332 ; stack_size -> {K, 40}
333 ; message_queue_len -> {K, 1}
334 end
335 ; (P, K) when P == Pid4 ->
336 Dict =
337 [ {'$initial_call', {baz_otp_mod, baz_otp_fun, 2}}
338 , {'$ancestors' , [Pid0, Pid1]}
339 ],
340 case K
341 of dictionary -> {K, Dict}
342 ; initial_call -> {K, {baz_mod, baz_fun, 3}}
343 ; registered_name -> []
344 ; status -> {K, waiting}
345 ; memory -> {K, 5}
346 ; total_heap_size -> {K, 10}
347 ; stack_size -> {K, 15}
348 ; message_queue_len -> {K, 0}
349 end
350 end
351 ),
352 #beam_stats
353 { timestamp = {1, 2, 3}
354 , node_id = 'node_foo@host_bar'
355 , memory = [{mem_type_foo, 1}, {mem_type_bar, 2}, {mem_type_baz, 3}]
356 , io_bytes_in = IOBytesIn
357 , io_bytes_out = IOBytesOut
358 , context_switches = ContextSwitches
359 , reductions = 9
360 , run_queue = 17
361 , ets = [ETSTableStatsFoo, ETSTableStatsBarA, ETSTableStatsBarB]
362 , processes = Processes
363 }.
This page took 0.063008 seconds and 4 git commands to generate.