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