MOD mineurs
[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([
25 start/2,
26 stop/1,
27 bench_get_messages/0,
28 bench_get_messages_avec_2_conversations/0
29 ]).
30 -include("../include/euphorik_bd.hrl").
31
32
33 % N est le nombre d'utilisateur
34 % M est le nombre de message que chaque utilisateur va poster
35 start(N, M) ->
36 Ids = creer_users(N),
37 lists:map(
38 fun(Id) ->
39 timer:sleep(100),
40 spawn(
41 fun() ->
42 {A1, A2, A3} = now(),
43 random:seed(A1, A2, A3),
44 loop(Id, M)
45 end
46 )
47 end,
48 Ids
49 ).
50
51 stop(Pids) ->
52 lists:foreach(fun(Pid) -> exit(Pid, kill) end, Pids).
53
54
55 bench_get_messages() ->
56 T = [
57 {page,"chat"},
58 {cookie,"5G84A5CJXMCPEHNI8T5A9"},
59 {message_count,40},
60 {last_message_id,0},
61 {main_page,1},
62 {troll_id,0},
63 {conversations,{array,[]}}
64 ],
65 moyenne_temps(euphorik_protocole, wait_event, [T], 20).
66
67
68 bench_get_messages_avec_2_conversations() ->
69 T = [
70 {page,"chat"},
71 {cookie,"5G84A5CJXMCPEHNI8T5A9"},
72 {message_count,40},
73 {last_message_id,0},
74 {main_page,1},
75 {troll_id,0},
76 {conversations,{array, [
77 {struct, [
78 {root, 921},
79 {page,1},
80 {last_message_id,0}
81 ]},
82 {struct, [
83 {root, 772},
84 {page, 1},
85 {last_message_id, 0}
86 ]}
87 ]}}
88 ],
89 moyenne_temps(euphorik_protocole, wait_event, [T], 20).
90
91
92 moyenne_temps(Module, Fun, Args, N) ->
93 moyenne_temps(Module, Fun, Args, N, N, 0).
94 moyenne_temps(_, _, _, 0, Total, Temps_acc) ->
95 Temps_acc / Total;
96 moyenne_temps(Module, Fun, Args, N, Total, Temps_acc) ->
97 {Temps, _} = timer:tc(Module, Fun, Args),
98 moyenne_temps(Module, Fun, Args, N - 1, Total, Temps_acc + Temps).
99
100
101 % Crée N user avec des noms aléatoires et renvoie la liste des id.
102 creer_users(N) ->
103 creer_users(N, []).
104 creer_users(0, Ids) -> lists:map(fun(#user{id = Id}) -> Id end, Ids);
105 creer_users(N, Ids) ->
106 creer_users(N - 1, [euphorik_bd:nouveau_user(mot_rand(random:uniform(4) + 4), "", "", #profile{}) | Ids ]).
107
108
109 % crée un message aléatoire et le renvoie
110 message_rand() ->
111 lists:flatten(message_rand(random:uniform(10), [])).
112 message_rand(0, Mots) -> Mots;
113 message_rand(N, Mots) ->
114 message_rand(N - 1, [mot_rand(random:uniform(2) + 5), $ | Mots]).
115
116
117 % Renvoie une succession de lettre aléatoire
118 mot_rand(L) ->
119 mot_rand(L, []).
120 mot_rand(0, Mot) -> Mot;
121 mot_rand(L, Mot) ->
122 mot_rand(L - 1, [random:uniform($z - $a + 1) + $a - 1 | Mot]).
123
124
125 % Tire au hasard de 0 à 3 messages sur les 10 derniers postés, renvoie une liste de int()
126 % répartition :
127 % 0 : 0.1
128 % 1 : 0.7
129 % 2 : 0.15
130 % 3 : 0.05
131 messages_id_rand() ->
132 R = random:uniform(),
133 if R =< 0.1 ->
134 [];
135 true ->
136 Messages = lists:map(fun(#minichat{id = Id}) -> Id end, euphorik_bd:messages(8)),
137 if
138 R > 0.1 andalso R =< 0.8 ->
139 tire_element_rand(1, Messages);
140 R > 0.8 andalso R =< 0.95 ->
141 tire_element_rand(2, Messages);
142 true ->
143 tire_element_rand(3, Messages)
144 end
145 end.
146
147
148 % tire N element distinct parmis la liste L proposée
149 tire_element_rand(N, L) when N =< length(L) ->
150 tire_element_rand(N, L, []);
151 tire_element_rand(_, _) ->
152 [].
153 tire_element_rand(0, _, Elements) -> Elements;
154 tire_element_rand(N, L, Elements) ->
155 E = lists:nth(random:uniform(length(L)), L),
156 E_se_trouve_dans_Elements = lists:any(fun(E2) -> E2 =:= E end, Elements),
157 if E_se_trouve_dans_Elements -> % si E a déjà été tiré on recommence sans rien changer
158 tire_element_rand(N, L, Elements);
159 true ->
160 tire_element_rand(N-1, L, [E | Elements])
161 end.
162
163 loop(User_id, 0) ->
164 io:format("~p a fini~n", [User_id]);
165 loop(User_id, M) ->
166 % attend un temp aléatoire compris entre 2 sec et 10 sec
167 timer:sleep(2000 * random:uniform(10)),
168 % poste un message aléatoire par une personne aléatoire répondant à des messages aléatoires
169 {Message, Repond_a} = {message_rand(), messages_id_rand()},
170 io:format("~p poste ~p et repond a ~w~n", [User_id, Message, Repond_a]),
171 case euphorik_bd:nouveau_message(Message, User_id, Repond_a) of
172 {erreur, E} ->
173 io:format("~p : erreur : ~p~n", [User_id, E]),
174 loop(User_id, M);
175 _ ->
176 loop(User_id, M - 1)
177 end.
178
179