Просмотр исходного кода

verify Re-enabling cowboy; trying to fix db

Mike Smith 11 лет назад
Родитель
Сommit
4a57c3b82b

+ 2 - 3
cowboy/benchmark_config

@@ -5,7 +5,7 @@
       "setup_file": "setup_erlang",
       "json_url": "/json",
       "db_url": "/db",
-      "query_url": "/db?queries=",
+      "query_url": "/query?queries=",
       "port": 8080,
       "approach": "Realistic",
       "classification": "Platform",
@@ -19,7 +19,6 @@
       "database_os": "Linux",
       "display_name": "cowboy",
       "notes": "",
-      "versus": "",
-      "skip": "true"
+      "versus": ""
   }}]
 }

+ 1 - 1
cowboy/src/db_handler.erl

@@ -22,7 +22,7 @@ handle(Req, State) ->
 			        {result_packet, _, _, [[ID, Rand]], _} <- [emysql:execute(test_pool, db_stmt, [random:uniform(10000)]) || _ <- lists:seq(1, I) ]],
 			{Res, Req1}
 		end,
-	{ok, Req3} = cowboy_req:reply(200, [{<<"Content-Type">>, <<"application/json">>}], jiffy:encode(JSON), Req2),
+	{ok, Req3} = cowboy_req:reply(200, [{<<"Content-Type">>, <<"application/json">>}], jiffy:encode(JSON:sublist(L,1)), Req2),
 	{ok, Req3, State}.
 
 terminate(_Reason, _Req, _State) ->

+ 2 - 1
cowboy/src/hello_world_app.erl

@@ -20,7 +20,8 @@ start(_Type, _Args) ->
 	Dispatch = cowboy_router:compile([
 		{'_', [
 			{"/json", json_handler, []},
-			{"/db", db_handler, []}
+			{"/db", db_handler, []},
+      {"/query", query_handler, []}
 		]}
 	]),
 	{ok, _} = cowboy:start_http(http, 5000, [{port, 8080}], [

+ 29 - 0
cowboy/src/query_handler.erl

@@ -0,0 +1,29 @@
+%% Feel free to use, reuse and abuse the code in this file.
+
+%% @doc Hello world handler.
+-module(query_handler).
+
+-export([init/3]).
+-export([handle/2]).
+-export([terminate/3]).
+
+init(_Transport, Req, []) ->
+  {ok, Req, undefined}.
+
+handle(Req, State) ->
+        random:seed(erlang:now()),
+        {JSON, Req2} = case cowboy_req:qs_val(<<"queries">>, Req) of
+    {undefined, Req1} ->
+      {result_packet, _, _, [[ID, Rand]], _} = emysql:execute(test_pool, db_stmt, [random:uniform(10000)]),
+      {[{[{<<"id">>, ID}, {<<"randomNumber">>, Rand}]}], Req1};
+    {N, Req1} ->
+      I = list_to_integer(binary_to_list(N)),
+      Res = [ {[{<<"id">>, ID}, {<<"randomNumber">>, Rand}]} || 
+              {result_packet, _, _, [[ID, Rand]], _} <- [emysql:execute(test_pool, db_stmt, [random:uniform(10000)]) || _ <- lists:seq(1, I) ]],
+      {Res, Req1}
+    end,
+  {ok, Req3} = cowboy_req:reply(200, [{<<"Content-Type">>, <<"application/json">>}], jiffy:encode(JSON), Req2),
+  {ok, Req3, State}.
+
+terminate(_Reason, _Req, _State) ->
+  ok.