Skip to content

Commit 4c1917b

Browse files
committed
Track number of data bytes sent and received
1 parent aaf9f77 commit 4c1917b

File tree

2 files changed

+47
-6
lines changed

2 files changed

+47
-6
lines changed

lib/ice_agent.ex

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,22 @@ defmodule ExICE.ICEAgent do
206206
GenServer.cast(ice_agent, {:send_data, data})
207207
end
208208

209+
@doc """
210+
Gathers ICE agent statistics.
211+
212+
* `bytes_sent` - data bytes sent. This does not include connectivity checks and UDP/IP header sizes.
213+
* `bytes_received` - data bytes received. This does not include connectivity checks and UDP/IP header sizes.
214+
* `candidate_pairs` - list of current candidate pairs. It will change after doing an ICE restart.
215+
"""
216+
@spec get_stats(pid()) :: %{
217+
bytes_sent: non_neg_integer(),
218+
bytes_received: non_neg_integer,
219+
candidate_pairs: [CandidatePair.t()]
220+
}
221+
def get_stats(ice_agent) do
222+
GenServer.call(ice_agent, :get_stats)
223+
end
224+
209225
@doc """
210226
Restarts ICE.
211227
@@ -280,7 +296,10 @@ defmodule ExICE.ICEAgent do
280296
remote_pwd: nil,
281297
remote_cands: [],
282298
stun_servers: stun_servers,
283-
turn_servers: []
299+
turn_servers: [],
300+
# stats
301+
bytes_sent: 0,
302+
bytes_received: 0
284303
}
285304

286305
{:ok, state}
@@ -311,6 +330,17 @@ defmodule ExICE.ICEAgent do
311330
{:reply, {:ok, state.local_ufrag, state.local_pwd}, state}
312331
end
313332

333+
@impl true
334+
def handle_call(:get_stats, _from, state) do
335+
stats = %{
336+
bytes_sent: state.bytes_sent,
337+
bytes_received: state.bytes_received,
338+
candidate_pairs: Map.values(state.checklist)
339+
}
340+
341+
{:reply, stats, state}
342+
end
343+
314344
@impl true
315345
def handle_cast(
316346
{:set_remote_credentials, ufrag, pwd},
@@ -439,8 +469,8 @@ defmodule ExICE.ICEAgent do
439469
List.first(state.prev_valid_pairs)
440470

441471
dst = {pair.remote_cand.address, pair.remote_cand.port}
442-
do_send(pair.local_cand.socket, dst, data)
443-
{:noreply, state}
472+
bytes_sent = do_send(pair.local_cand.socket, dst, data)
473+
{:noreply, %{state | bytes_sent: state.bytes_sent + bytes_sent}}
444474
end
445475

446476
@impl true
@@ -587,7 +617,7 @@ defmodule ExICE.ICEAgent do
587617
end
588618
else
589619
notify(state.on_data, {:data, packet})
590-
{:noreply, state}
620+
{:noreply, %{state | bytes_received: state.bytes_received + byte_size(packet)}}
591621
end
592622
end
593623

@@ -1716,17 +1746,19 @@ defmodule ExICE.ICEAgent do
17161746
# retrying after getting EPERM seems to help
17171747
case :gen_udp.send(socket, dst, data) do
17181748
:ok ->
1719-
:ok
1749+
byte_size(data)
17201750

17211751
err ->
17221752
Logger.error("UDP send error: #{inspect(err)}. Retrying...")
17231753

17241754
case :gen_udp.send(socket, dst, data) do
17251755
:ok ->
17261756
Logger.debug("Successful retry")
1757+
byte_size(data)
17271758

17281759
err ->
17291760
Logger.error("Unseccessful retry: #{inspect(err)}. Giving up.")
1761+
0
17301762
end
17311763
end
17321764
end

test/ice_agent_test.exs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ defmodule ExICE.ICEAgentTest do
33

44
alias ExICE.ICEAgent
55

6-
test "gathering candidates" do
6+
test "gather_candidates/1" do
77
{:ok, agent} = ICEAgent.start_link(:controlling)
88
:ok = ICEAgent.gather_candidates(agent)
99

@@ -17,4 +17,13 @@ defmodule ExICE.ICEAgentTest do
1717
assert_receive {:ex_ice, ^agent, {:gathering_state_change, :gathering}}
1818
assert_receive {:ex_ice, ^agent, {:gathering_state_change, :complete}}
1919
end
20+
21+
test "get_stats/1" do
22+
{:ok, agent} = ICEAgent.start_link(:controlling)
23+
24+
assert %{bytes_sent: 0, bytes_received: 0, candidate_pairs: candidate_pairs} =
25+
ICEAgent.get_stats(agent)
26+
27+
assert is_list(candidate_pairs)
28+
end
2029
end

0 commit comments

Comments
 (0)