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