-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_benchmark.erl
83 lines (66 loc) · 2.52 KB
/
my_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
-module(my_benchmark).
-export([test_send_message/0, test_timeline/0]).
run_benchmark(Name, Fun, Times) ->
ThisPid = self(),
lists:foreach(fun (N) ->
spawn_link( fun () ->
run_benchmark_once(Name, Fun, N),
ThisPid ! done
end),
receive done ->
ok
end
end, lists:seq(1, Times)).
run_benchmark_once(Name, Fun, N) ->
io:format("Starting benchmark ~s: ~p~n", [Name, N]),
StartTime = os:timestamp(),
statistics(runtime),
Fun(),
WallClockTime = timer:now_diff(os:timestamp(), StartTime),
{_, CpuTime} = statistics(runtime),
io:format("Wall clock time = ~p ms~n", [WallClockTime / 1000.0]),
io:format("CPU time = ~p ms~n", [CpuTime]),
io:format("~s done~n", [Name]).
initialize_server() ->
rand:seed_s(exsplus, {0, 0, 0}),
NumberOfUsers = 1000,
NumberOfSubscriptions = 100,
NumberOfMessages = 25,
io:format("Parameters:~n"),
io:format("Number of users : ~p~n", [NumberOfUsers]),
io:format("Number of Subscriptions : ~p~n", [NumberOfSubscriptions]),
io:format("Number of messages : ~p~n", [NumberOfMessages]),
io:format("~n"),
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)),
ServerPid = server_individual:initialize_with(Users), { ServerPid, 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() ->
{ServerPid, UserName} = initialize_server(),
run_benchmark("timeline",
fun () ->
lists:foreach(
fun(_) ->
server:get_timeline(ServerPid, pick_random(UserName))
end,
lists:seq(1,10000))
end, 30).
test_send_message() ->
{ServerPid, UserName} = initialize_server(),
run_benchmark("send message",
fun() ->
lists:foreach(
fun(_) ->
server:send_message(ServerPid, pick_random(UserName), "Test")
end,
lists:seq(1,10000))
end, 30).