Consolidate data source calls in 1, mockable, module.
authorSiraaj Khandkar <siraaj@khandkar.net>
Sat, 19 Sep 2015 23:20:44 +0000 (19:20 -0400)
committerSiraaj Khandkar <siraaj@khandkar.net>
Sat, 19 Sep 2015 23:20:44 +0000 (19:20 -0400)
src/beam_stats_ets.erl
src/beam_stats_ets_table.erl
src/beam_stats_process.erl
src/beam_stats_processes.erl
src/beam_stats_source.erl [new file with mode: 0644]
src/beam_stats_state.erl

index 3965765..cfc0606 100644 (file)
@@ -14,4 +14,5 @@
 -spec collect() ->
     t().
 collect() ->
 -spec collect() ->
     t().
 collect() ->
-    lists:map(fun beam_stats_ets_table:of_id/1, ets:all()).
+    TableIDs = beam_stats_source:ets_all(),
+    lists:map(fun beam_stats_ets_table:of_id/1, TableIDs).
index b603656..622c0f2 100644 (file)
 -spec of_id(id()) ->
     t().
 of_id(ID) ->
 -spec of_id(id()) ->
     t().
 of_id(ID) ->
-    WordSize      = erlang:system_info(wordsize),
-    NumberOfWords = ets:info(ID, memory),
+    WordSize      = beam_stats_source:erlang_system_info(wordsize),
+    NumberOfWords = beam_stats_source:ets_info(ID, memory),
     NumberOfBytes = NumberOfWords * WordSize,
     #?MODULE
     { id     = ID
     NumberOfBytes = NumberOfWords * WordSize,
     #?MODULE
     { id     = ID
-    , name   = ets:info(ID, name)
-    , size   = ets:info(ID, size)
+    , name   = beam_stats_source:ets_info(ID, name)
+    , size   = beam_stats_source:ets_info(ID, size)
     , memory = NumberOfBytes
     }.
 
     , memory = NumberOfBytes
     }.
 
index ab6d1c2..e2751ea 100644 (file)
@@ -119,7 +119,7 @@ pid_info_exn(Pid, Key) ->
     Value.
 
 pid_info_opt(Pid, Key) ->
     Value.
 
 pid_info_opt(Pid, Key) ->
-    case {Key, erlang:process_info(Pid, Key)}
+    case {Key, beam_stats_source:erlang_process_info(Pid, Key)}
     of  {registered_name, []}           -> none
     ;   {_              , {Key, Value}} -> {some, Value}
     end.
     of  {registered_name, []}           -> none
     ;   {_              , {Key, Value}} -> {some, Value}
     end.
index 2505178..90c3886 100644 (file)
@@ -21,7 +21,8 @@
 -spec collect() ->
     t().
 collect() ->
 -spec collect() ->
     t().
 collect() ->
-    Ps = [beam_stats_process:of_pid(P) || P <- erlang:processes()],
+    Pids = beam_stats_source:erlang_processes(),
+    Ps = [beam_stats_process:of_pid(P) || P <- Pids],
     ?T
     { individual_stats
         = Ps
     ?T
     { individual_stats
         = Ps
@@ -32,7 +33,7 @@ collect() ->
     , count_garbage_collecting
         = length([P || P <- Ps, P#beam_stats_process.status =:= garbage_collecting])
     , count_registered
     , count_garbage_collecting
         = length([P || P <- Ps, P#beam_stats_process.status =:= garbage_collecting])
     , count_registered
-        = length(registered())
+        = length(beam_stats_source:erlang_registered())
     , count_runnable
         = length([P || P <- Ps, P#beam_stats_process.status =:= runnable])
     , count_running
     , count_runnable
         = length([P || P <- Ps, P#beam_stats_process.status =:= runnable])
     , count_running
diff --git a/src/beam_stats_source.erl b/src/beam_stats_source.erl
new file mode 100644 (file)
index 0000000..eb0d7d1
--- /dev/null
@@ -0,0 +1,44 @@
+-module(beam_stats_source).
+
+-export(
+    [ erlang_memory/0
+    , erlang_node/0
+    , erlang_process_info/2
+    , erlang_processes/0
+    , erlang_registered/0
+    , erlang_statistics/1
+    , erlang_system_info/1
+    , ets_all/0
+    , ets_info/2
+    , os_timestamp/0
+    ]).
+
+erlang_memory() ->
+    erlang:memory().
+
+erlang_node() ->
+    erlang:node().
+
+erlang_process_info(Pid, Key) ->
+    erlang:process_info(Pid, Key).
+
+erlang_processes() ->
+    erlang:processes().
+
+erlang_registered() ->
+    erlang:registered().
+
+erlang_statistics(Key) ->
+    erlang:statistics(Key).
+
+erlang_system_info(Key) ->
+    erlang:system_info(Key).
+
+ets_all() ->
+    ets:all().
+
+ets_info(Table, Key) ->
+    ets:info(Table, Key).
+
+os_timestamp() ->
+    os:timestamp().
index ad424d5..d514f95 100644 (file)
@@ -64,8 +64,8 @@ new() ->
     t().
 new(#totals{}=TotalsPrevious) ->
     ?T
     t().
 new(#totals{}=TotalsPrevious) ->
     ?T
-    { timestamp       = os:timestamp()
-    , node_id         = erlang:node()
+    { timestamp       = beam_stats_source:os_timestamp()
+    , node_id         = beam_stats_source:erlang_node()
     , snapshots       = snapshots_new()
     , deltas          = deltas_new()
     , totals_previous = TotalsPrevious
     , snapshots       = snapshots_new()
     , deltas          = deltas_new()
     , totals_previous = TotalsPrevious
@@ -123,14 +123,14 @@ export(
 
 snapshots_new() ->
     #snapshots
 
 snapshots_new() ->
     #snapshots
-    { memory     = erlang:memory()
+    { memory     = beam_stats_source:erlang_memory()
     , processes  = beam_stats_processes:collect()
     , processes  = beam_stats_processes:collect()
-    , run_queue  = erlang:statistics(run_queue)
+    , run_queue  = beam_stats_source:erlang_statistics(run_queue)
     , ets        = beam_stats_ets:collect()
     }.
 
 deltas_new() ->
     , ets        = beam_stats_ets:collect()
     }.
 
 deltas_new() ->
-    {_ReductionsTotal, ReductionsDelta} = erlang:statistics(reductions),
+    {_ReductionsTotal, ReductionsDelta} = beam_stats_source:erlang_statistics(reductions),
     #deltas
     { reductions = ReductionsDelta
     }.
     #deltas
     { reductions = ReductionsDelta
     }.
@@ -138,8 +138,8 @@ deltas_new() ->
 totals_new() ->
     { {input  , IOBytesIn}
     , {output , IOBytesOut}
 totals_new() ->
     { {input  , IOBytesIn}
     , {output , IOBytesOut}
-    } = erlang:statistics(io),
-    {ContextSwitches, 0} = erlang:statistics(context_switches),
+    } = beam_stats_source:erlang_statistics(io),
+    {ContextSwitches, 0} = beam_stats_source:erlang_statistics(context_switches),
     #totals
     { io_bytes_in      = IOBytesIn
     , io_bytes_out     = IOBytesOut
     #totals
     { io_bytes_in      = IOBytesIn
     , io_bytes_out     = IOBytesOut
This page took 0.047794 seconds and 4 git commands to generate.