-
Notifications
You must be signed in to change notification settings - Fork 0
/
lasp_new_partition_overcounting.sh
executable file
·229 lines (198 loc) · 7.18 KB
/
lasp_new_partition_overcounting.sh
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/usr/bin/env escript
-author("Vitor Enes Duarte <[email protected]>").
main(_) ->
ValidDirectories = ordsets:from_list(["ad_counter"]),
%% Filter out invalid directories
Simulations0 = only_dirs(root_log_dir()),
Simulations1 = lists:filter(
fun(Simulation) ->
ordsets:is_element(Simulation, ValidDirectories)
end,
Simulations0
),
%% Generate plots
lists:foreach(
fun(Simulation) ->
SimulationDir = root_log_dir() ++ "/" ++ Simulation,
LocalAndDCOS = only_dirs(SimulationDir),
lists:foreach(
fun(Dir) ->
Path = SimulationDir ++ "/" ++ Dir,
EvalIds = only_dirs(Path),
generate_plots(Simulation ++ "/" ++ Dir, EvalIds)
end,
LocalAndDCOS
)
end,
Simulations1
).
%% @doc Generate plots.
generate_plots(Simulation, EvalIds) ->
Map = lists:foldl(
fun(EvalId, Acc) ->
Tokens = string:tokens(EvalId, "_"),
IdMaxIndex = length(Tokens) - 2,
ClientNumberIndex = length(Tokens) - 1,
PartitionProbabilityIndex = length(Tokens),
ClientNumber = lists:nth(ClientNumberIndex, Tokens),
PartitionProbability = lists:nth(PartitionProbabilityIndex, Tokens),
Id = string:join(lists:sublist(Tokens, IdMaxIndex), "_"),
case ClientNumber of
"64" ->
case Id == "client_server_delta_based_with_aae" orelse
Id == "code_client_server_delta_based_with_aae" orelse
Id == "peer_to_peer_delta_based_with_aae" orelse
Id == "code_peer_to_peer_delta_based_with_aae" of
true ->
io:format("Analysing ~p~n", [EvalId]),
EvalIdDir = root_log_dir() ++ "/" ++ Simulation ++ "/" ++ EvalId,
EvalTimestamps = only_dirs(EvalIdDir),
T = lists:foldl(
fun(EvalTimestamp, ToAverage0) ->
OvercountingFile = EvalIdDir ++ "/" ++ EvalTimestamp ++ "/overcounting",
Overcounting = get_overcounting(OvercountingFile),
orddict:store(
EvalTimestamp,
Overcounting,
ToAverage0
)
end,
orddict:new(),
EvalTimestamps
),
OvercountingAverage = average_overcounting(T),
PerPartitions0 = case orddict:find(Id, Acc) of
{ok, CPC} ->
CPC;
error ->
orddict:new()
end,
PerPartitions1 = orddict:store(list_to_integer(PartitionProbability), OvercountingAverage, PerPartitions0),
orddict:store(Id, PerPartitions1, Acc);
false ->
io:format("Ignoring ~p~n", [EvalId]),
Acc
end;
_ ->
io:format("Ignoring ~p~n", [EvalId]),
Acc
end
end,
orddict:new(),
EvalIds
),
io:format("~p~n", [Map]),
PlotDir = root_plot_dir() ++ "/" ++ Simulation ++ "/",
filelib:ensure_dir(PlotDir),
{InputFiles, Titles} = write_to_files(PlotDir, Map),
OutputFile = output_file(PlotDir, "partition_overcounting"),
Result = run_gnuplot(InputFiles, Titles, OutputFile),
io:format("Generating partition overcounting plot ~p. Output: ~p~n~n", [OutputFile, Result]).
%% @private
root_log_dir() ->
"logs".
%% @private
root_plot_dir() ->
"plots".
%% @private
gnuplot_file() ->
"partition_overcounting.gnuplot".
%% @private
output_file(PlotDir, Name) ->
PlotDir ++ Name ++ ".pdf".
%% @private
only_dirs(Dir) ->
{ok, DirFiles} = file:list_dir(Dir),
%% Ignore files
lists:filter(
fun(Elem) ->
filelib:is_dir(Dir ++ "/" ++ Elem)
end,
DirFiles
).
%% @private
get_overcounting(FilePath) ->
%% Open log file
{ok, FileDescriptor} = file:open(FilePath, [read]),
Line = io:get_line(FileDescriptor, ''),
list_to_float(Line).
%% @private
average_overcounting(ToAverage) ->
OvercountingSum = orddict:fold(
fun(_Timestamp, Overcounting, Acc) ->
Overcounting + Acc
end,
0,
ToAverage
),
OvercountingSum + orddict:size(ToAverage).
%% @private
write_to_files(PlotDir, Map) ->
orddict:fold(
fun(Id, PartitionsToOvercounting, {InputFiles0, Titles0}) ->
Title = get_title(Id),
InputFile = PlotDir ++ Id,
file:write_file(InputFile, "", [write]),
lists:foreach(
fun({Partitions, Overcounting}) ->
case Partitions of
"0" ->
ok;
_ ->
append_to_file(InputFile, Partitions, Overcounting)
end
end,
PartitionsToOvercounting
),
{[InputFile | InputFiles0], [Title | Titles0]}
end,
{[], []},
Map
).
%% @private
get_title("client_server_delta_based_with_aae") -> "Client Server";
get_title("code_client_server_delta_based_with_aae") -> "Client Server with Heavy Clients";
get_title("peer_to_peer_delta_based_with_aae") -> "Peer-to-Peer";
get_title("code_peer_to_peer_delta_based_with_aae") -> "Peer-to-Peer with Heavy Clients".
%% @private
append_to_file(InputFile, Clients, Overcounting) ->
Line = io_lib:format("~w,~w\n", [Clients, Overcounting]),
file:write_file(InputFile, Line, [append]).
%% @private
run_gnuplot(InputFiles, Titles, OutputFile) ->
Bin = case os:getenv("MESOS_TASK_ID", "false") of
"false" ->
"gnuplot";
_ ->
"/usr/bin/gnuplot"
end,
Command = Bin ++ " -e \""
++ "outputname='" ++ OutputFile ++ "'; "
++ "inputnames='" ++ join_filenames(InputFiles) ++ "'; "
++ "titles='" ++ join_titles(Titles) ++ "'\" " ++ gnuplot_file(),
io:format("~p~n~n", [Command]),
os:cmd(Command).
%% @private
join_filenames(InputFiles) ->
Line = lists:foldl(
fun(Elem, Acc) ->
Acc ++ Elem
++ " "
end,
"",
InputFiles
),
string:strip(Line).
%% @private
join_titles(Titles) ->
Line = lists:foldl(
fun(Elem, Acc) ->
% "overcounting.gnuplot" does not support titles with spaces
% But it converts all the "_" in the titles to " "
Acc ++ re:replace(Elem, " ", "_", [global, {return, list}])
++ " "
end,
"",
Titles
),
string:strip(Line).