From 7786d23b34f01fb15d97cc61decd2f04e65e70a4 Mon Sep 17 00:00:00 2001 From: Siraaj Khandkar Date: Sat, 19 Sep 2015 19:36:21 -0400 Subject: [PATCH] Test stats collection. --- rebar.config | 1 + test/beam_stats_consumer_statsd_SUITE.erl | 175 ++++++++++++++++++++++ 2 files changed, 176 insertions(+) diff --git a/rebar.config b/rebar.config index f1816dc..5eb75d1 100644 --- a/rebar.config +++ b/rebar.config @@ -1,5 +1,6 @@ { deps , [ {hope, ".*", {git, "https://github.com/ibnfirnas/hope.git", {tag, "3.8.0"}}} + , {meck, ".*", {git, "https://github.com/eproxus/meck.git" , {tag, "0.8.3"}}} ] }. diff --git a/test/beam_stats_consumer_statsd_SUITE.erl b/test/beam_stats_consumer_statsd_SUITE.erl index ffd0c46..16cb5fc 100644 --- a/test/beam_stats_consumer_statsd_SUITE.erl +++ b/test/beam_stats_consumer_statsd_SUITE.erl @@ -14,6 +14,7 @@ %% Test cases -export( [ t_send/1 + , t_collect/1 ]). -define(GROUP, beam_stats_consumer_statsd). @@ -28,6 +29,7 @@ all() -> groups() -> Tests = [ t_send + , t_collect ], Properties = [], [{?GROUP, Properties, Tests}]. @@ -212,3 +214,176 @@ t_send(_Cfg) -> Received -- [Expected] end, [] = lists:foldl(RemoveExpectedFromReceived, MsgsReceived, MsgsExpected). + +t_collect(_Cfg) -> + Pid0 = list_to_pid("<0.0.0>"), + Pid1 = list_to_pid("<0.1.0>"), + Pid2 = list_to_pid("<0.2.0>"), + Pid3 = list_to_pid("<0.3.0>"), + Process1 = + #beam_stats_process + { pid = Pid1 + , registered_name = {some, reg_name_foo} + , ancestry = + #beam_stats_process_ancestry + { raw_initial_call = {foo_mod, foo_fun, 2} + , otp_initial_call = none + , otp_ancestors = none + } + , status = running + , memory = 15 + , total_heap_size = 25 + , stack_size = 10 + , message_queue_len = 0 + }, + Process2 = + #beam_stats_process + { pid = Pid2 + , registered_name = none + , ancestry = + #beam_stats_process_ancestry + { raw_initial_call = {bar_mod, bar_fun, 1} + , otp_initial_call = none + , otp_ancestors = none + } + , status = running + , memory = 25 + , total_heap_size = 35 + , stack_size = 40 + , message_queue_len = 5 + }, + Process3 = + #beam_stats_process + { pid = Pid3 + , registered_name = none + , ancestry = + #beam_stats_process_ancestry + { raw_initial_call = {baz_mod, baz_fun, 3} + , otp_initial_call = {some, {baz_otp_mod, baz_otp_fun, 2}} + , otp_ancestors = {some, [Pid0, Pid1]} + } + , status = running + , memory = 25 + , total_heap_size = 35 + , stack_size = 40 + , message_queue_len = 1 + }, + Processes = + #beam_stats_processes + { individual_stats = + [ Process1 + , Process2 + , Process3 + ] + , count_all = 3 + , count_exiting = 0 + , count_garbage_collecting = 0 + , count_registered = 1 + , count_runnable = 0 + , count_running = 3 + , count_suspended = 0 + , count_waiting = 0 + }, + ETSTableStatsFoo = + #beam_stats_ets_table + { id = foo + , name = foo + , size = 5 + , memory = 40 + }, + ETSTableStatsBar = + #beam_stats_ets_table + { id = 37 + , name = bar + , size = 8 + , memory = 64 + }, + meck:new(beam_stats_source), + meck:expect(beam_stats_source, erlang_memory, + fun () -> [{mem_type_foo, 1}, {mem_type_bar, 2}, {mem_type_baz, 3}] end), + meck:expect(beam_stats_source, erlang_node, + fun () -> 'node_foo@host_bar' end), + meck:expect(beam_stats_source, erlang_registered, + fun () -> [reg_name_foo] end), + meck:expect(beam_stats_source, erlang_statistics, + fun (io ) -> {{input, 3}, {output, 7}} + ; (context_switches) -> {5, 0} + ; (reductions ) -> {0, 9} % 1st element is unused + ; (run_queue ) -> 17 + end + ), + meck:expect(beam_stats_source, ets_all, + fun () -> [foo, 37] end), + meck:expect(beam_stats_source, erlang_system_info, + fun (wordsize) -> 8 end), + meck:expect(beam_stats_source, ets_info, + fun (foo, memory) -> 5 + ; (foo, name ) -> foo + ; (foo, size ) -> 5 + ; (37 , memory) -> 8 + ; (37 , name ) -> bar + ; (37 , size ) -> 8 + end + ), + meck:expect(beam_stats_source, erlang_processes, + fun () -> [Pid1, Pid2, Pid3] end), + meck:expect(beam_stats_source, os_timestamp, + fun () -> {1, 2, 3} end), + meck:expect(beam_stats_source, erlang_process_info, + fun (P, K) when P == Pid1 -> + case K + of dictionary -> {K, []} + ; initial_call -> {K, {foo_mod, foo_fun, 2}} + ; registered_name -> {K, reg_name_foo} + ; status -> {K, running} + ; memory -> {K, 15} + ; total_heap_size -> {K, 25} + ; stack_size -> {K, 10} + ; message_queue_len -> {K, 0} + end + ; (P, K) when P == Pid2 -> + case K + of dictionary -> {K, []} + ; initial_call -> {K, {bar_mod, bar_fun, 1}} + ; registered_name -> [] + ; status -> {K, running} + ; memory -> {K, 25} + ; total_heap_size -> {K, 35} + ; stack_size -> {K, 40} + ; message_queue_len -> {K, 5} + end + ; (P, K) when P == Pid3 -> + Dict = + [ {'$initial_call', {baz_otp_mod, baz_otp_fun, 2}} + , {'$ancestors' , [Pid0, Pid1]} + ], + case K + of dictionary -> {K, Dict} + ; initial_call -> {K, {baz_mod, baz_fun, 3}} + ; registered_name -> [] + ; status -> {K, running} + ; memory -> {K, 25} + ; total_heap_size -> {K, 35} + ; stack_size -> {K, 40} + ; message_queue_len -> {K, 1} + end + end + ), + BEAMStatsExpected = + #beam_stats + { timestamp = {1, 2, 3} + , node_id = 'node_foo@host_bar' + , memory = [{mem_type_foo, 1}, {mem_type_bar, 2}, {mem_type_baz, 3}] + , io_bytes_in = 3 + , io_bytes_out = 7 + , context_switches = 5 + , reductions = 9 + , run_queue = 17 + , ets = [ETSTableStatsFoo, ETSTableStatsBar] + , processes = Processes + }, + BEAMStatsComputed = beam_stats_state:export(beam_stats_state:new()), + ct:log("BEAMStatsExpected: ~p~n", [BEAMStatsExpected]), + ct:log("BEAMStatsComputed: ~p~n", [BEAMStatsComputed]), + BEAMStatsExpected = BEAMStatsComputed, + meck:unload(beam_stats_source). -- 2.20.1