Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Time middleware preprocess/2, handle/2, postprocess/3

  • Loading branch information...
commit 45f96e162a872b02a1e6c78a9cffcf96fb2f01b8 1 parent b0be967
@knutin authored
View
3  src/elli_example_middleware.erl
@@ -15,12 +15,9 @@ handle(Req, _Args) ->
ignore
end.
-
-
%%
%% ELLI EVENT CALLBACKS
%%
-
handle_event(_Event, _Data, _Args) ->
ok.
View
4 src/elli_http.erl
@@ -11,6 +11,7 @@
-export([start_link/4]).
-export([mk_req/7]). %% useful when testing.
+-export([record_time/1]). %% For use in elli_middleware.erl
%% Exported for looping with a fully-qualified module name
-export([accept/4, handle_request/4, chunk_loop/1, split_args/1,
@@ -552,6 +553,9 @@ split_args(Qs) ->
t(Key) ->
put({time, Key}, os:timestamp()).
+record_time(Key) ->
+ t(Key).
+
get_timings() ->
lists:flatmap(fun ({{time, Key}, Val}) ->
if
View
10 src/elli_middleware.erl
@@ -64,8 +64,10 @@ process(Req, [{Mod, Args} | Mods]) ->
true ->
case Mod:handle(Req, Args) of
ignore ->
+ elli_http:record_time({middleware_handle_end, Mod}),
process(Req, Mods);
Response ->
+ elli_http:record_time({middleware_handle_end, Mod}),
Response
end;
false ->
@@ -77,7 +79,9 @@ preprocess(Req, []) ->
preprocess(Req, [{Mod, Args} | Mods]) ->
case erlang:function_exported(Mod, preprocess, 2) of
true ->
- preprocess(Mod:preprocess(Req, Args), Mods);
+ NewReq = Mod:preprocess(Req, Args),
+ elli_http:record_time({middleware_preprocess_end, Mod}),
+ preprocess(NewReq, Mods);
false ->
preprocess(Req, Mods)
end.
@@ -87,7 +91,9 @@ postprocess(_Req, Res, []) ->
postprocess(Req, Res, [{Mod, Args} | Mods]) ->
case erlang:function_exported(Mod, postprocess, 3) of
true ->
- postprocess(Req, Mod:postprocess(Req, Res, Args), Mods);
+ NewRes = Mod:postprocess(Req, Res, Args),
+ elli_http:record_time({middleware_postprocess_end, Mod}),
+ postprocess(Req, NewRes, Mods);
false ->
postprocess(Req, Res, Mods)
end.
View
23 src/elli_middleware_trace.erl
@@ -0,0 +1,23 @@
+%% @doc: Helper for testing midldewares. Stores all events in a public
+%% ETS table.
+
+-module(elli_middleware_trace).
+-export([handle/2, handle_event/3]).
+-export([clear/1]).
+
+handle(_, _) ->
+ ignore.
+
+handle_event(elli_startup, _, Args) ->
+ Name = name(Args),
+ Name = ets:new(Name, [named_table, public, ordered_set]),
+ ok;
+
+handle_event(Event, Details, Args) ->
+ ets:insert(name(Args), {erlang:now(), Event, Details}),
+ ok.
+
+clear(Name) ->
+ true = ets:delete_all_objects(Name).
+
+name(Args) -> proplists:get_value(name, Args).
View
22 test/elli_middleware_tests.erl
@@ -2,13 +2,16 @@
-include_lib("eunit/include/eunit.hrl").
-include("elli.hrl").
+-define(TRACE_TABLE, middleware_tests).
-elli_test_() ->
+
+elli_middleware_test_() ->
{setup,
fun setup/0, fun teardown/1,
[
?_test(hello_world()),
?_test(short_circuit()),
+ ?_test(timings()),
?_test(compress())
]}.
@@ -27,6 +30,18 @@ hello_world() ->
{ok, Response} = httpc:request(URL),
?assertEqual("Hello World!", body(Response)).
+timings() ->
+ elli_middleware_trace:clear(?TRACE_TABLE),
+
+ {ok, _} = httpc:request(get, {"http://localhost:3002/compressed",
+ [{"Accept-Encoding", "gzip"}]}, [], []),
+
+ [{_, request_complete, [_Request, _ResponseCode, _ResponseHeaders,
+ _ResponseBody, Timings]}] = ets:tab2list(?TRACE_TABLE),
+ ?assert(proplists:is_defined(
+ {middleware_postprocess_end, elli_middleware_compress}, Timings)),
+ ok.
+
compress() ->
{ok, Response} = httpc:request(get, {"http://localhost:3002/compressed",
@@ -88,6 +103,7 @@ setup() ->
{port, 514}]},
{elli_example_middleware, []},
{elli_middleware_compress, []},
+ {elli_middleware_trace, [{name, ?TRACE_TABLE}]},
{elli_example_callback, []}
]}
],
@@ -99,6 +115,10 @@ setup() ->
[P].
teardown(Pids) ->
+ application:stop(crypto),
+ application:stop(public_key),
+ application:stop(ssl),
+ inets:stop(),
[elli:stop(P) || P <- Pids].
View
4 test/elli_tests.erl
@@ -47,6 +47,10 @@ setup() ->
[P].
teardown(Pids) ->
+ application:stop(crypto),
+ application:stop(public_key),
+ application:stop(ssl),
+ inets:stop(),
[elli:stop(P) || P <- Pids].
Please sign in to comment.
Something went wrong with that request. Please try again.