Browse files

Replaced Greenspunned disk_log with the built in version.

  • Loading branch information...
1 parent 9ab835b commit 7f765c46810d06aad571d8d7effbe340387c78c1 @ericmoritz ericmoritz committed Jun 14, 2012
Showing with 54 additions and 127 deletions.
  1. +5 −4 cat_stats
  2. +4 −4 compile_stats
  3. +3 −2 runtest
  4. +0 −47 src/packet_reader.erl
  5. +0 −15 src/wsdemo_client.erl
  6. +42 −55 src/wsdemo_logger.erl
View
9 cat_stats
@@ -6,10 +6,11 @@
main([LogFile]) ->
code:add_paths(["ebin"]),
- PR = packet_reader:open(LogFile),
- PR:foldl(fun(Term, _) ->
- io:format("~w~n", [Term])
- end, '_');
+ wsdemo_logger:foldl(fun(Term, _) ->
+ io:format("~w~n", [Term])
+ end, '_',
+ LogFile),
+ ok;
main(_) ->
usage().
View
8 compile_stats
@@ -72,10 +72,10 @@ main([LogFile]) ->
folsom_metrics:new_counter(crashes),
folsom_metrics:new_counter(connection_timeouts),
- PR = packet_reader:open(LogFile),
-
- PR:foldl(fun reducer/2, #state{clients=dict:new(),
- messages=dict:new()}),
+ wsdemo_logger:foldl(fun reducer/2,
+ #state{clients=dict:new(),
+ messages=dict:new()},
+ LogFile),
io:format("~p~n", [stats()]);
main(_) ->
View
5 runtest
@@ -15,12 +15,13 @@ main([LogFile, SecondsStr, Server, PortStr, ClientsStr]) ->
"Data File: ~p~n",
[Clients, Seconds, LogFile]),
- wsdemo_logger:start_link(LogFile),
+ wsdemo_logger:new(LogFile),
wsdemo_stats:start_link(Server, Port, Clients),
timer:sleep(Seconds * 1000),
io:format("Finished, syncing stats~n", []),
- wsdemo_logger:stop(),
+
+ wsdemo_logger:close(),
ok;
main([LogFile, SecondsStr, ClientsStr]) ->
main([LogFile, SecondsStr, "localhost", "8000", ClientsStr]);
View
47 src/packet_reader.erl
@@ -1,47 +0,0 @@
--module(packet_reader).
-
--export([open/1, next/1, foldl/3]).
-
-open(Filename) ->
- {ok, FH} = file:open(Filename, [read, binary]),
- new(FH, <<>>).
-
-new(FH, Rest) ->
- {?MODULE, FH, Rest}.
-
-next({_, FH, Rest}) ->
- case file:read(FH, 2) of
- {ok, Size} ->
- read_packet(FH, <<Rest/binary, Size/binary>>);
- Other ->
- Other
- end.
-
-foldl(Fun, Acc0, PR) ->
- case PR:next() of
- {ok, Packet, PR2} ->
- Acc1 = Fun(Packet, Acc0),
- PR2:foldl(Fun, Acc1);
- eof ->
- Acc0;
- Other ->
- Other
- end.
-
-%% Internal
-
-read_packet(FH, CurrentBin) ->
- case erlang:decode_packet(2, CurrentBin, []) of
- {ok, Packet, Rest} ->
- NewReader = new(FH, Rest),
- {ok, binary_to_term(Packet), NewReader};
- {more, Length} ->
- case file:read(FH, Length) of
- {ok, More} ->
- read_packet(FH, <<CurrentBin/binary, More/binary>>);
- Else ->
- Else
- end;
- {error, Reason} ->
- {error, Reason}
- end.
View
15 src/wsdemo_client.erl
@@ -60,18 +60,3 @@ ws_onclose(Client, State) ->
wsdemo_logger:event({ws_onclose, self()}),
State.
-encode_now({MegaSecs, Secs, MicroSecs}) ->
- <<MegaSecs:32, Secs:32, MicroSecs:32>>.
-
-decode_now(<<MegaSecs:32, Secs:32, MicroSecs:32>>) ->
- {MegaSecs, Secs, MicroSecs}.
-
--ifdef(TEST).
-
-decode_now_test_() ->
- Now = {1338,474888,611762},
- [
- ?_assertEqual(Now, decode_now(encode_now(Now)))
- ].
-
--endif.
View
97 src/wsdemo_logger.erl
@@ -1,68 +1,55 @@
-module(wsdemo_logger).
--behaviour(gen_server).
--define(SERVER, ?MODULE).
--record(state, {fh}).
%% ------------------------------------------------------------------
%% API Function Exports
%% ------------------------------------------------------------------
--export([start_link/1, event/1, stop/0]).
-
-%% ------------------------------------------------------------------
-%% gen_server Function Exports
-%% ------------------------------------------------------------------
-
--export([init/1, handle_call/3, handle_cast/2, handle_info/2,
- terminate/2, code_change/3]).
+-export([new/1, event/1, close/0, foldl/3]).
%% ------------------------------------------------------------------
%% API Function Definitions
%% ------------------------------------------------------------------
-start_link(Filename) ->
- gen_server:start_link({local, ?SERVER}, ?MODULE, [Filename], []).
-
-event(Term) ->
- gen_server:cast(?SERVER, {event, {erlang:now(), Term}}).
-
-%% Finish up all the pending messages and then stop the server
-stop() ->
- gen_server:call(?SERVER, stop, infinity).
-
-%% ------------------------------------------------------------------
-%% gen_server Function Definitions
-%% ------------------------------------------------------------------
-
-init([Filename]) ->
- {ok, FH} = file:open(Filename, [write,binary]),
- {ok, #state{fh=FH}}.
-
-handle_call(stop, _From, State) ->
- {stop, normal, ok, State};
-handle_call(_Request, _From, State) ->
- {reply, ok, State}.
-
-handle_cast({event, Data}, State) ->
- FH = State#state.fh,
- Bin = term_to_binary(Data),
- BinSize = size(Bin),
- file:write(FH, <<BinSize:16, Bin/binary>>),
- {noreply, State};
-handle_cast(_Msg, State) ->
- {noreply, State}.
-
-handle_info(_Info, State) ->
- {noreply, State}.
-
-terminate(_Reason, #state{fh=FH}) ->
- file:close(FH),
+new(Filename) ->
+ case file:delete(Filename) of
+ {error, enoent} ->
+ pass;
+ ok ->
+ pass;
+ {error, Reason} ->
+ exit({error, Reason})
+ end,
+
+ % We deleted any existing log so anything other than
+ % {ok, Log} is something to crash over.
+ {ok, _Log} = disk_log:open([{name, ?MODULE},
+ {file, Filename},
+ {size, infinity}]),
ok.
-code_change(_OldVsn, State, _Extra) ->
- {ok, State}.
-
-%% ------------------------------------------------------------------
-%% Internal Function Definitions
-%% ------------------------------------------------------------------
-
+event(Term) ->
+ disk_log:alog(?MODULE, {erlang:now(), Term}).
+
+close() ->
+ disk_log:close(?MODULE).
+
+
+foldl(Fun, Acc, Filename) ->
+ {ok, DL} = disk_log:open([{file, Filename},
+ {name, {?MODULE, make_ref()}},
+ {mode, read_only}]),
+ foldl(Fun, Acc, DL, start).
+
+%% Internal
+foldl(Fun, Acc, DL, Cont) ->
+ case disk_log:chunk(DL, Cont) of
+ eof ->
+ Acc;
+ {Cont2, Terms} ->
+ Acc0 = lists:foldl(Fun, Acc, Terms),
+ foldl(Fun, Acc0, DL, Cont2)
+ end.
+
+
+
+

0 comments on commit 7f765c4

Please sign in to comment.