Browse Source

Erlang/mochiweb update (#2656)

* Fix db host for mochiweb, enable db tests

Having the wrong db host was causing the server to crash, which made
the json and plaintext tests fail.

I enabled the four db-related tests since they seem to be implemented.
They all pass except fortunes.  Unfortunately that implementation
appears to live in another github repo (utkarshkukreti/erl_bench) so
we can't fix it.

I removed the sed line from setup.sh since the file it was targeting
(mochiweb_bench_sup.erl) didn't have any of the strings it was trying
to replace.

* Erlang/mochiweb update
Nate 8 years ago
parent
commit
6e6cb1f036

+ 4 - 0
frameworks/Erlang/mochiweb/benchmark_config.json

@@ -5,6 +5,10 @@
       "setup_file": "setup",
       "json_url": "/json",
       "plaintext_url": "/plaintext",
+      "db_url": "/db",
+      "query_url": "/queries?queries=",
+      "fortune_url": "/fortunes",
+      "update_url": "/updates?queries=",
       "port": 8080,
       "approach": "Stripped",
       "classification": "Platform",

+ 1 - 1
frameworks/Erlang/mochiweb/priv/app.config

@@ -4,6 +4,6 @@
  {erl_bench, [{db_pool, 256},
               {db_user, "benchmarkdbuser"},
               {db_password, "benchmarkdbpass"},
-              {db_host, "localhost"},
+              {db_host, "TFB-database"},
               {db_port, 3306},
               {db_name, "hello_world"}]}].

+ 22 - 0
frameworks/Erlang/mochiweb/priv/templates/fortunes.dtl

@@ -0,0 +1,22 @@
+{#  -*- html -*- #}
+<!DOCTYPE html>
+<html>
+<head>
+  <title>Fortunes</title>
+</head>
+
+<body>
+  <table>
+    <tr>
+      <th>id</th>
+      <th>message</th>
+    </tr>
+{% for fortune in fortunes %}
+    <tr>
+      <td>{{fortune.id}}</td>
+      <td>{{fortune.message}}</td>
+    </tr>
+{% endfor %}
+  </table>
+</body>
+</html>

+ 10 - 2
frameworks/Erlang/mochiweb/rebar.config

@@ -1,5 +1,13 @@
 %% -*- erlang -*-
 {deps, [
-  {erl_bench, ".*", {git, "git://github.com/utkarshkukreti/erl_bench.git", "cb70233d7de1e800893cf1460f181a706aa08a38"}},
-  {mochiweb, "2.9.0", {git, "git://github.com/mochi/mochiweb.git", {tag, "v2.9.0"}}}
+  {mochiweb, "2.9.0", {git, "git://github.com/mochi/mochiweb.git", {tag, "v2.9.0"}}},
+  {jsonx, ".*", {git, "git://github.com/iskra/jsonx.git", "9c95948c6835827ed61a9506ae4a9aba61acf335"}},
+  {emysql, ".*", {git, "git://github.com/deadtrickster/Emysql.git"}},
+  {erlydtl, "0.11.1", {git, "git://github.com/erlydtl/erlydtl.git", {tag, "0.11.1"}}}
+]}.
+{erlydtl_opts, [
+    {doc_root,   "priv/templates"},
+    {out_dir,    "ebin"},
+    {source_ext, ".dtl"},
+    {module_ext, "_view"}
 ]}.

+ 0 - 2
frameworks/Erlang/mochiweb/setup.sh

@@ -1,7 +1,5 @@
 #!/bin/bash
 
-sed -i 's|"benchmarkdbpass", ".*", 3306|"benchmarkdbpass", "'"${DBHOST}"'", 3306|g' src/mochiweb_bench_sup.erl
-
 fw_depends erlang mysql
 
 rm -rf deps/* ebin/*

+ 35 - 0
frameworks/Erlang/mochiweb/src/erl_bench.erl

@@ -0,0 +1,35 @@
+-module(erl_bench).
+-export([hello_json/0, hello_plain/0, random_json/0, randoms_json/1, update_randoms_json/1, fortunes_html/0]).
+
+hello_json() ->
+    jsonx:encode([{<<"message">>, <<"Hello, World!">>}]).
+
+hello_plain() ->
+    <<"Hello, world!">>.
+
+random_json() ->
+    RandomId = randoms:random_id(),
+    jsonx:encode(find(RandomId)).
+
+randoms_json(Count) ->
+    Json = [find(Id) || Id <- randoms:random_ids(Count)],
+    jsonx:encode(Json).
+
+update_randoms_json(Count) ->
+    Json = [update(Id) || Id <- randoms:random_ids(Count)],
+    jsonx:encode(Json).
+
+fortunes_html() ->
+    Props = [[{message, Message}, {id, Id}] || {Message, Id} <- fortunes:all()],
+    {ok, Html} = fortunes_view:render([{fortunes, Props}]),
+    Html.
+
+%% private
+
+find(Id) ->
+    {Id, RandomNumber} = randoms:find(Id),
+    [{<<"id">>, Id}, {<<"randomNumber">>, RandomNumber}].
+
+update(Id) ->
+    {Id, RandomNumber} = randoms:update(Id),
+    [{<<"id">>, Id}, {<<"randomNumber">>, RandomNumber}].

+ 15 - 0
frameworks/Erlang/mochiweb/src/fortunes.erl

@@ -0,0 +1,15 @@
+-module(fortunes).
+-export([init/0, all/0]).
+
+init() ->
+    emysql:prepare(all_fortunes, <<"SELECT message, id FROM Fortune;">>).
+
+format_list({Message, Id}) ->
+    lists:flatten(io_lib:format("(Message: ~s, Id: ~b)", [Message, Id])).
+
+format_lists(Fortunes) ->
+    string:join(lists:map(fun format_list/1, Fortunes), ", ").
+
+all() ->
+    Fortunes = [{Message, Id} || [Message, Id] <- store:rows(all_fortunes, [])] ++ [{<<"Additional fortune added at request time.">>, 0}],
+    lists:sort(Fortunes).

+ 1 - 1
frameworks/Erlang/mochiweb/src/mochiweb_bench.app.src

@@ -6,4 +6,4 @@
   {registered, []},
   {mod, {mochiweb_bench_app, []}},
   {env, []},
-  {applications, [kernel, stdlib, erl_bench]}]}.
+  {applications, [kernel, stdlib, crypto, emysql]}]}.

+ 2 - 1
frameworks/Erlang/mochiweb/src/mochiweb_bench_app.erl

@@ -7,7 +7,8 @@ start() ->
     application:ensure_all_started(mochiweb_bench).
 
 start(_Type, _StartArgs) ->
-    {ok, Port} = application:get_env(mochiweb_bench, http_port),
+    store:init(),
+    Port = 8080,
     mochiweb_bench_sup:start_link([{port, Port}]).
 
 stop(_State) ->

+ 32 - 0
frameworks/Erlang/mochiweb/src/randoms.erl

@@ -0,0 +1,32 @@
+-module(randoms).
+-export([init/0, random_id/0, random_ids/1, find/1, update/1]).
+
+-define(world_max_records, 10000).
+
+init() ->
+    random:seed(erlang:now()),
+    emysql:prepare(find_random, <<"SELECT id, randomNumber FROM World WHERE id = ? LIMIT 1;">>),
+    emysql:prepare(update_random, <<"UPDATE World SET randomNumber = ? WHERE id = ? LIMIT 1;">>).
+
+random_id() -> random:uniform(?world_max_records).
+
+random_ids(Times) -> random_ids(Times, []).
+
+random_ids(0,     Acc) -> Acc;
+random_ids(Times, Acc) -> random_ids(Times - 1, [random(Acc) | Acc]).
+
+find(Id) ->
+    [[Id, RandomNumber]] = store:rows(find_random, [Id]),
+    {Id, RandomNumber}.
+
+update(Id) ->
+    NewRandomNumber = random:uniform(10000),
+    emysql:execute(db_pool, update_random, [NewRandomNumber, Id]),
+    {Id, NewRandomNumber}.
+
+random(List) ->
+    Rand = random_id(),
+    case lists:member(Rand, List) of
+        true -> random(List);
+        false -> Rand
+    end.

+ 20 - 0
frameworks/Erlang/mochiweb/src/store.erl

@@ -0,0 +1,20 @@
+-module(store).
+-include_lib("emysql/include/emysql.hrl").
+-export([init/0, rows/2]).
+
+init() ->
+    PoolSize = 256,
+    User = "benchmarkdbuser",
+    Password = "benchmarkdbpass",
+    Host = "TFB-database",
+    Port = 3306,
+    Database = "hello_world",
+
+    emysql:add_pool(db_pool, PoolSize, User, Password, Host, Port, Database, utf8),
+
+    randoms:init(),
+    fortunes:init().
+
+rows(Statement, Query) ->
+    Result = emysql:execute(db_pool, Statement, Query),
+    Result#result_packet.rows.