arkdro avatar arkdro committed 88b95db

add time accounting

Comments (0)

Files changed (1)

erlang/src/c_server.erl

 -export([init/1, handle_call/3, handle_cast/2, handle_info/2]).
 -export([terminate/2, code_change/3]).
 
--record(state, {status, port, portcmd, from}).
+-record(state, {status, port, portcmd, from,
+                start,      %% start time of current command
+                count=0,    %% number of commands sent
+                duration=0  %% now()
+               }).
 
 -define(SERVER, ?MODULE).
 
 
 %-------------------------------------------------------------------
 handle_call(ping, From, #state{status=running, port=Port} = State) ->
-    port_command(Port, term_to_binary(ping)),
-    {noreply, State#state{from=From}};
+    New = send_to_port(State, Port, term_to_binary(ping)),
+    {noreply, New#state{from=From}};
 handle_call({info, Term}, From, #state{status=running, port=Port} = State) ->
     Cmd = term_to_binary({info, Term}),
-    port_command(Port, Cmd),
-    {noreply, State#state{from=From}};
+    New = send_to_port(State, Port, Cmd),
+    {noreply, New#state{from=From}};
 handle_call(get, From, #state{status=running, port=Port} = State) ->
     Cmd = term_to_binary({get, none}),
-    port_command(Port, Cmd),
-    {noreply, State#state{from=From}};
+    New = send_to_port(State, Port, Cmd),
+    {noreply, New#state{from=From}};
 handle_call({multiply_list, List}, From,
             #state{status=running, port=Port} = State) ->
     Blist = bias_list(List),
     Cmd = term_to_binary({mult_list, Blist}),
-    port_command(Port, Cmd),
-    {noreply, State#state{from=From}};
+    New = send_to_port(State, Port, Cmd),
+    {noreply, New#state{from=From}};
 handle_call({multiply, N1, N2}, From,
             #state{status=running, port=Port} = State) ->
     B1 = bias_number(N1),
     B2 = bias_number(N2),
     Cmd = term_to_binary({multiply, B1, B2}),
-    port_command(Port, Cmd),
-    {noreply, State#state{from=From}};
+    New = send_to_port(State, Port, Cmd),
+    {noreply, New#state{from=From}};
 handle_call({sum, List}, From, #state{status=running, port=Port} = State) ->
     Blist = bias_list(List),
     Cmd = term_to_binary({sum_list, Blist}),
-    port_command(Port, Cmd),
-    {noreply, State#state{from=From}};
+    New = send_to_port(State, Port, Cmd),
+    {noreply, New#state{from=From}};
 handle_call({add, N1, N2}, From,
             #state{status=running, port=Port} = State) ->
     B1 = bias_number(N1),
     B2 = bias_number(N2),
     Cmd = term_to_binary({add, B1, B2}),
-    port_command(Port, Cmd),
-    {noreply, State#state{from=From}};
+    New = send_to_port(State, Port, Cmd),
+    {noreply, New#state{from=From}};
 handle_call({subtract, N1, N2}, From,
             #state{status=running, port=Port} = State) ->
     B1 = bias_number(N1),
     B2 = bias_number(N2),
     Cmd = term_to_binary({subtract, B1, B2}),
-    port_command(Port, Cmd),
-    {noreply, State#state{from=From}};
+    New = send_to_port(State, Port, Cmd),
+    {noreply, New#state{from=From}};
 handle_call({divide, N1, N2}, From,
             #state{status=running, port=Port} = State) ->
     B1 = bias_number(N1),
     B2 = bias_number(N2),
     Cmd = term_to_binary({divide, B1, B2}),
-    port_command(Port, Cmd),
-    {noreply, State#state{from=From}};
+    New = send_to_port(State, Port, Cmd),
+    {noreply, New#state{from=From}};
 handle_call(stop, _From, St) ->
     {stop, normal, ok, St};
 handle_call(status, _From, St) ->
     {noreply, State#state{status=running}};
 handle_info({Port, {data, <<131,100,0,4,"pong">>}},
             #state{from=From, port = Port} = State) ->
+    New = add_timing(State),
     gen_server:reply(From, pong),
-    {noreply, State#state{from=undefined}};
+    {noreply, New#state{from=undefined}};
 handle_info({Port, {data, Data}}, #state{from=From, port=Port} = State) ->
     Term = binary_to_term(Data),
     gen_server:reply(From, Term),
     [bias_number(X) || X <- L].
 
 %-------------------------------------------------------------------
+send_to_port(State, Port, Term) ->
+    port_command(Port, Term),
+    State#state{start=now()}.
+
+%-------------------------------------------------------------------
+add_timing(#state{start=T1, count=Cnt, duration=Dur} = State) ->
+    T2 = now(),
+    D = timer:now_diff(T2, T1),
+    State#state{start=undefined, count=Cnt+1, duration=Dur+D}.
+
+%-------------------------------------------------------------------
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.