MOD optimisation des fonctions est_une_reponse_a_user et a_repondu_a_message du modul...
[euphorik.git] / modules / erl / euphorik_test.erl
1 % coding: utf-8
2 % Copyright 2008 Grégory Burri
3 %
4 % This file is part of Euphorik.
5 %
6 % Euphorik is free software: you can redistribute it and/or modify
7 % it under the terms of the GNU General Public License as published by
8 % the Free Software Foundation, either version 3 of the License, or
9 % (at your option) any later version.
10 %
11 % Euphorik is distributed in the hope that it will be useful,
12 % but WITHOUT ANY WARRANTY; without even the implied warranty of
13 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 % GNU General Public License for more details.
15 %
16 % You should have received a copy of the GNU General Public License
17 % along with Euphorik. If not, see <http://www.gnu.org/licenses/>.
18 %
19 % Module de test de euphorik.
20 % Crée un certain nombre d'utilisateur et post des messages aléatoire.
21
22
23 -module(euphorik_test).
24 -export([start/2, stop/1]).
25 -include("../include/euphorik_bd.hrl").
26
27
28 % N est le nombre d'utilisateur
29 % M est le nombre de message que chaque utilisateur va poster
30 start(N, M) ->
31 Ids = creer_users(N),
32 lists:map(
33 fun(Id) ->
34 timer:sleep(100),
35 spawn(
36 fun() ->
37 {A1, A2, A3} = now(),
38 random:seed(A1, A2, A3),
39 loop(Id, M)
40 end
41 )
42 end,
43 Ids
44 ).
45
46 stop(Pids) ->
47 lists:foreach(fun(Pid) -> exit(Pid, kill) end, Pids).
48
49
50 % Crée N user avec des noms aléatoires et renvoie la liste des id.
51 creer_users(N) ->
52 creer_users(N, []).
53 creer_users(0, Ids) -> lists:map(fun(#user{id = Id}) -> Id end, Ids);
54 creer_users(N, Ids) ->
55 creer_users(N - 1, [euphorik_bd:nouveau_user(mot_rand(random:uniform(4) + 4), "", "", #profile{}) | Ids ]).
56
57
58 % crée un message aléatoire et le renvoie
59 message_rand() ->
60 lists:flatten(message_rand(random:uniform(10), [])).
61 message_rand(0, Mots) -> Mots;
62 message_rand(N, Mots) ->
63 message_rand(N - 1, [mot_rand(random:uniform(2) + 5), $ | Mots]).
64
65
66 % Renvoie une succession de lettre aléatoire
67 mot_rand(L) ->
68 mot_rand(L, []).
69 mot_rand(0, Mot) -> Mot;
70 mot_rand(L, Mot) ->
71 mot_rand(L - 1, [random:uniform($z - $a + 1) + $a - 1 | Mot]).
72
73
74 % Tire au hasard de 0 à 3 messages sur les 10 derniers postés, renvoie une liste de int()
75 % répartition :
76 % 0 : 0.1
77 % 1 : 0.7
78 % 2 : 0.15
79 % 3 : 0.05
80 messages_id_rand() ->
81 R = random:uniform(),
82 if R =< 0.1 ->
83 [];
84 true ->
85 Messages = lists:map(fun(#minichat{id = Id}) -> Id end, euphorik_bd:messages(8)),
86 if
87 R > 0.1 andalso R =< 0.8 ->
88 tire_element_rand(1, Messages);
89 R > 0.8 andalso R =< 0.95 ->
90 tire_element_rand(2, Messages);
91 true ->
92 tire_element_rand(3, Messages)
93 end
94 end.
95
96
97 % tire N element distinct parmis la liste L proposée
98 tire_element_rand(N, L) when N =< length(L) ->
99 tire_element_rand(N, L, []).
100 tire_element_rand(0, _, Elements) -> Elements;
101 tire_element_rand(N, L, Elements) ->
102 E = lists:nth(random:uniform(length(L)), L),
103 E_se_trouve_dans_Elements = lists:any(fun(E2) -> E2 =:= E end, Elements),
104 if E_se_trouve_dans_Elements -> % si E a déjà été tiré on recommence sans rien changer
105 tire_element_rand(N, L, Elements);
106 true ->
107 tire_element_rand(N-1, L, [E | Elements])
108 end.
109
110 loop(User_id, 0) ->
111 io:format("~p a fini~n", [User_id]);
112 loop(User_id, M) ->
113 % attend un temp aléatoire compris entre 1 sec et 5 sec
114 timer:sleep(1000 * random:uniform(5)),
115 % poste un message aléatoire par une personne aléatoire répondant à des messages aléatoires
116 {Message, Repond_a} = {message_rand(), messages_id_rand()},
117 io:format("~p poste ~p et repond a ~w~n", [User_id, Message, Repond_a]),
118 euphorik_bd:nouveau_message(Message, User_id, Repond_a),
119 loop(User_id, M - 1).
120