-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdistributed_benchmark.erl
129 lines (112 loc) · 4.46 KB
/
distributed_benchmark.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
-module(distributed_benchmark).
-export([test_timeline/1, test_send_message/1, test_get_profile/1, test_system_load/2]).
run_benchmark(Name, Fun, Times, NumberOfServers) ->
io:format("run,wall_clock_time,cpu_time,number_of_servers,benchmark~n"),
ThisPid = self(),
lists:foreach(fun(N) ->
spawn_link(fun() ->
run_benchmark_once(Name, Fun, N, NumberOfServers),
ThisPid ! done
end),
receive done ->
ok
end
end, lists:seq(1, Times)).
run_load_benchmark(Name, Fun, Times, NumberOfServers) ->
io:format("run,number_of_servers,benchmark_name,average_message_queue_length~n"),
ThisPid = self(),
lists:foreach(
fun(N) ->
spawn_link(fun() ->
run_load_benchmark_once(Name, Fun, N, NumberOfServers), ThisPid ! done
end) ,
receive done ->
ok
end
end, lists:seq(1,Times)).
run_load_benchmark_once(Name, Fun, N, NumberOfServers) ->
Fun(),
io:format("~p,~p,~s,", [N,NumberOfServers,Name]).
run_benchmark_once(Name, Fun, N, NumberOfServers) ->
StartTime = os:timestamp(),
statistics(runtime),
Fun(),
WallClockTime = timer:now_diff(os:timestamp(), StartTime),
{_, CpuTime} = statistics(runtime),
io:format("~p,~p,~p,~p,~s~n", [N,WallClockTime / 10000.0, CpuTime,NumberOfServers,Name]).
initialize_server(PassedNumberOfServers) ->
NumberOfServers = PassedNumberOfServers,
rand:seed_s(exsplus, {0, 0, 0}),
NumberOfUsers = 2000,
NumberOfSubscriptions = 10,
NumberOfMessages = 30,
UserNames = [integer_to_list(I) || I <- lists:seq(1, NumberOfUsers)],
Users = dict:from_list(lists:map(fun(Name) ->
Subscriptions = [pick_random(UserNames) || _ <- lists:seq(1,NumberOfSubscriptions)],
Messages = [generate_message(Name, I) || I <- lists:seq(1, NumberOfMessages)],
User = {user, Name, sets:from_list(Subscriptions), Messages},
{Name, User}
end, UserNames )),
ServerPids = server_distributed:initialize_with(Users, NumberOfServers),
{ServerPids, UserNames}.
pick_random(List) ->
lists:nth(rand:uniform(length(List)), List).
generate_message(UserName, I) ->
Text = "Message" ++ integer_to_list(I) ++ " from " ++ UserName,
{message, UserName, Text, os:system_time()}.
test_timeline(NumberOfServers) -> % TODO Finish this
{ServerPids, UserNames} = initialize_server(NumberOfServers),
run_benchmark("Timeline",
fun() ->
lists:foreach(
fun (_) ->
server:get_timeline(pick_random(ServerPids), pick_random(UserNames))
end, lists:seq(1, 10000)
)
end, 30, NumberOfServers).
test_send_message(NumberOfServers) ->
{ServerPids, UserNames} = initialize_server(NumberOfServers),
run_benchmark("send_message",
fun () ->
lists:foreach(fun (_) ->
server:send_message(pick_random(ServerPids), pick_random(UserNames), "Test")
end,
lists:seq(1, 10000))
end,
30, NumberOfServers).
test_get_profile(NumberOfServers) ->
{ServerPids, UserNames} = initialize_server(NumberOfServers),
run_benchmark("get_profile",
fun () ->
lists:foreach(fun (_) ->
server:send_message(pick_random(ServerPids), pick_random(UserNames), "Test")
end,
lists:seq(1, 1000)),
lists:foreach(fun (_) ->
server:get_profile(pick_random(ServerPids), pick_random(UserNames))
end,
lists:seq(1, 10000))
end,
30, NumberOfServers).
test_system_load(NumberOfServers, NumberOfMessagesSent) ->
% io:format("Testing The average system Load~n"),
{ServerPids, UserNames} = initialize_server(NumberOfServers),
run_load_benchmark("system_load",
fun() ->
lists:foreach(fun (_) ->
server:send_message(pick_random(ServerPids), pick_random(UserNames), "Test")
end,
lists:seq(1, NumberOfMessagesSent)),
Total = sum(lists:map(
fun(Server) ->
{message_queue_len, NumberOfMessagesInServer} = erlang:process_info(Server, message_queue_len),
NumberOfMessagesInServer
end, ServerPids)),
io:format("~p~n", [Total])
end, 30, NumberOfServers).
sum(L) ->
sum(L, 0).
sum([H|T], Acc) ->
sum(T, H + Acc);
sum([], Acc) ->
Acc.