MOD optimisation des fonctions est_une_reponse_a_user et a_repondu_a_message du modul...
[euphorik.git] / modules / erl / euphorik_test.erl
index c602292..b705fc6 100644 (file)
 
 
 -module(euphorik_test).
--export([start/1, stop/1]).
+-export([start/2, stop/1]).
 -include("../include/euphorik_bd.hrl").
 
 
 % N est le nombre d'utilisateur
-start(N) ->
+% M est le nombre de message que chaque utilisateur va poster
+start(N, M) ->
    Ids = creer_users(N),
    lists:map(
       fun(Id) ->
@@ -35,7 +36,7 @@ start(N) ->
             fun() -> 
                {A1, A2, A3} = now(),
                random:seed(A1, A2, A3),
-               loop(Id)
+               loop(Id, M)
             end
          )
       end,
@@ -51,7 +52,7 @@ creer_users(N) ->
    creer_users(N, []).
 creer_users(0, Ids) -> lists:map(fun(#user{id = Id}) -> Id end, Ids);
 creer_users(N, Ids) ->
-   creer_users(N - 1, [euphorik_bd:nouveau_user(mot_rand(random:uniform(4) + 4), "", "") | Ids ]).
+   creer_users(N - 1, [euphorik_bd:nouveau_user(mot_rand(random:uniform(4) + 4), "", "", #profile{}) | Ids ]).
 
 
 % crée un message aléatoire et le renvoie
@@ -72,20 +73,24 @@ mot_rand(L, Mot) ->
 
 % Tire au hasard de 0 à 3 messages sur les 10 derniers postés, renvoie une liste de int()
 % répartition : 
-%  0 : 0.5
-%  1 : 0.3
+%  0 : 0.1
+%  1 : 0.7
 %  2 : 0.15
 %  3 : 0.05
 messages_id_rand() ->
-   Messages = lists:map(fun(#minichat{id = Id}) -> Id end, euphorik_bd:messages(30)),
    R = random:uniform(),
-   if R =< 0.5 -> [];
-      R > 0.5 andalso R =< 0.8 ->
-         tire_element_rand(1, Messages);
-      R > 0.8 andalso R =< 0.95 ->
-         tire_element_rand(2, Messages);
+   if R =< 0.1 ->
+         [];
       true ->
-         tire_element_rand(3, Messages)
+         Messages = lists:map(fun(#minichat{id = Id}) -> Id end, euphorik_bd:messages(8)),
+         if
+            R > 0.1 andalso R =< 0.8 ->
+               tire_element_rand(1, Messages);
+            R > 0.8 andalso R =< 0.95 ->
+               tire_element_rand(2, Messages);
+            true ->
+               tire_element_rand(3, Messages)
+         end
    end.
 
 
@@ -102,13 +107,14 @@ tire_element_rand(N, L, Elements) ->
          tire_element_rand(N-1, L, [E | Elements])
    end.
 
-
-loop(User_id) -> 
+loop(User_id, 0) ->
+   io:format("~p a fini~n", [User_id]);
+loop(User_id, M) -> 
    % attend un temp aléatoire compris entre 1 sec et 5 sec
    timer:sleep(1000 * random:uniform(5)),
    % poste un message aléatoire par une personne aléatoire répondant à des messages aléatoires
    {Message, Repond_a} = {message_rand(), messages_id_rand()},
    io:format("~p poste ~p et repond a ~w~n", [User_id, Message, Repond_a]),
    euphorik_bd:nouveau_message(Message, User_id, Repond_a),
-   loop(User_id).
+   loop(User_id, M - 1).
    
\ No newline at end of file