-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlasp_new_transmission_plot.sh
executable file
·321 lines (281 loc) · 12.9 KB
/
lasp_new_transmission_plot.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#!/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),
HeavyClients = lists:nth(1, Tokens) == "code",
Id = string:join(lists:sublist(Tokens, IdMaxIndex), "_"),
case PartitionProbability of
"0" ->
case HeavyClients of
false ->
io:format("Analysing ~p~n", [EvalId]),
EvalIdDir = root_log_dir() ++ "/" ++ Simulation ++ "/" ++ EvalId,
EvalTimestamps = only_dirs(EvalIdDir),
T = lists:foldl(
fun(EvalTimestamp, ToAverage0) ->
EvalDir = EvalIdDir ++ "/" ++ EvalTimestamp,
io:format("Analysing ~p~n", [EvalDir]),
%% Before
%% {ServerMB, ClientsMB} = get_server_and_clients_mb(EvalDir),
%% Now
Tuple = get_state_and_protocol_mb(EvalDir),
orddict:store(
EvalTimestamp,
Tuple,
ToAverage0
)
end,
orddict:new(),
EvalTimestamps
),
Tuple = average_state_and_protocol_mb(T),
PerClients0 = case orddict:find(Id, Acc) of
{ok, CPC} ->
CPC;
error ->
orddict:new()
end,
PerClients1 = orddict:store(ClientNumber, Tuple, PerClients0),
orddict:store(Id, PerClients1, Acc);
true ->
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),
InputFile = PlotDir ++ "transmission",
OutputFile = output_file(PlotDir, "transmission"),
Header = "ABCXYZ,32_s,32_p,64_s,64_p,128_s,128_p,256_s,256_p,512_s,512_p,1024_s,1024_p\n",
L1 = io_lib:format("dc-state,~w,~w,~w,~w,~w,~w,~w,~w,~w,~w,~w,~w\n",
[
gb(element(1, orddict:fetch("32", orddict:fetch("client_server_state_based_with_aae_test", Map)))),
gb(element(2, orddict:fetch("32", orddict:fetch("client_server_state_based_with_aae_test", Map)))),
gb(element(1, orddict:fetch("64", orddict:fetch("client_server_state_based_with_aae_test", Map)))),
gb(element(2, orddict:fetch("64", orddict:fetch("client_server_state_based_with_aae_test", Map)))),
gb(element(1, orddict:fetch("128", orddict:fetch("client_server_state_based_with_aae_test", Map)))),
gb(element(2, orddict:fetch("128", orddict:fetch("client_server_state_based_with_aae_test", Map)))),
gb(element(1, orddict:fetch("256", orddict:fetch("client_server_state_based_with_aae_test", Map)))),
gb(element(2, orddict:fetch("256", orddict:fetch("client_server_state_based_with_aae_test", Map)))),
0,
0,
0,
0
]),
L2 = io_lib:format("hg-state,~w,~w,~w,~w,~w,~w,~w,~w,~w,~w,~w,~w\n",
[
gb(element(1, orddict:fetch("32", orddict:fetch("peer_to_peer_state_based_with_aae_test", Map)))),
gb(element(2, orddict:fetch("32", orddict:fetch("peer_to_peer_state_based_with_aae_test", Map)))),
gb(element(1, orddict:fetch("64", orddict:fetch("peer_to_peer_state_based_with_aae_test", Map)))),
gb(element(2, orddict:fetch("64", orddict:fetch("peer_to_peer_state_based_with_aae_test", Map)))),
gb(element(1, orddict:fetch("128", orddict:fetch("peer_to_peer_state_based_with_aae_test", Map)))),
gb(element(2, orddict:fetch("128", orddict:fetch("peer_to_peer_state_based_with_aae_test", Map)))),
gb(element(1, orddict:fetch("256", orddict:fetch("peer_to_peer_state_based_with_aae_test", Map)))),
gb(element(2, orddict:fetch("256", orddict:fetch("peer_to_peer_state_based_with_aae_test", Map)))),
gb(element(1, orddict:fetch("512", orddict:fetch("peer_to_peer_state_based_with_aae_test", Map)))),
gb(element(2, orddict:fetch("512", orddict:fetch("peer_to_peer_state_based_with_aae_test", Map)))),
gb(element(1, orddict:fetch("1024", orddict:fetch("peer_to_peer_state_based_with_aae_test", Map)))),
gb(element(2, orddict:fetch("1024", orddict:fetch("peer_to_peer_state_based_with_aae_test", Map))))
]),
L3 = io_lib:format("hg-delta,~w,~w,~w,~w,~w,~w,~w,~w,~w,~w,~w,~w\n",
[
gb(element(1, orddict:fetch("32", orddict:fetch("peer_to_peer_delta_based_with_aae_test", Map)))),
gb(element(2, orddict:fetch("32", orddict:fetch("peer_to_peer_delta_based_with_aae_test", Map)))),
gb(element(1, orddict:fetch("64", orddict:fetch("peer_to_peer_delta_based_with_aae_test", Map)))),
gb(element(2, orddict:fetch("64", orddict:fetch("peer_to_peer_delta_based_with_aae_test", Map)))),
gb(element(1, orddict:fetch("128", orddict:fetch("peer_to_peer_delta_based_with_aae_test", Map)))),
gb(element(2, orddict:fetch("128", orddict:fetch("peer_to_peer_delta_based_with_aae_test", Map)))),
gb(element(1, orddict:fetch("256", orddict:fetch("peer_to_peer_delta_based_with_aae_test", Map)))),
gb(element(2, orddict:fetch("256", orddict:fetch("peer_to_peer_delta_based_with_aae_test", Map)))),
gb(element(1, orddict:fetch("512", orddict:fetch("peer_to_peer_delta_based_with_aae_test", Map)))),
gb(element(2, orddict:fetch("512", orddict:fetch("peer_to_peer_delta_based_with_aae_test", Map)))),
gb(element(1, orddict:fetch("1024", orddict:fetch("peer_to_peer_delta_based_with_aae_test", Map)))),
gb(element(2, orddict:fetch("1024", orddict:fetch("peer_to_peer_delta_based_with_aae_test", Map))))
]),
%% truncate file
write_to_file(InputFile, ""),
append_to_file(InputFile, Header),
append_to_file(InputFile, L1),
append_to_file(InputFile, L2),
append_to_file(InputFile, L3),
Result = run_gnuplot(InputFile, OutputFile),
io:format("Generating transmission plot ~p. Output: ~p~n~n", [OutputFile, Result]).
%% @private
gb(MB) ->
MB / 1024.
%% @private
root_log_dir() ->
"logs".
%% @private
root_plot_dir() ->
"plots".
%% @private
gnuplot_file() ->
"new_transmission.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
only_csv_files(LogDir) ->
{ok, LogFiles} = file:list_dir(LogDir),
%% Ignore not csv files
lists:filter(
fun(Elem) ->
case re:run(Elem, ".*.csv") of
{match, _} ->
true;
nomatch ->
false
end
end,
LogFiles
).
%% @private
get_state_and_protocol_mb(EvalDir) ->
LogFiles = only_csv_files(EvalDir),
lists:foldl(
fun(File, {StateAcc, DisseminationAcc, OverlayAcc}) ->
FilePath = EvalDir ++ "/" ++ File,
{State, Dissemination, Overlay} = get_state_and_protocol_transmission(FilePath),
{StateAcc + State, DisseminationAcc + Dissemination, OverlayAcc + Overlay}
end,
{0, 0, 0},
LogFiles
).
%% @private
get_state_and_protocol_transmission(FilePath) ->
%% Open log file
{ok, FileDescriptor} = file:open(FilePath, [read]),
%% Ignore the first line
[_ | Lines] = read_lines(FilePath, FileDescriptor),
LogTypeToLastBytes = lists:foldl(
fun(Line, Acc) ->
%% Parse log line
[Type0, _Time0, Bytes0] = string:tokens(Line, ",\n"),
TypeA = list_to_atom(Type0),
{BytesF, _} = string:to_float(Bytes0),
orddict:store(TypeA, BytesF, Acc)
end,
orddict:new(),
Lines
),
Result = lists:foldl(
fun({LogType, Bytes}, {StateAcc, DisseminationAcc, OverlayAcc}) ->
case log_type(LogType) of
state ->
{StateAcc + Bytes, DisseminationAcc, OverlayAcc};
dissemination_protocol ->
{StateAcc, DisseminationAcc + Bytes, OverlayAcc};
overlay_protocol ->
{StateAcc, DisseminationAcc, OverlayAcc + Bytes};
ignore ->
{StateAcc, DisseminationAcc, OverlayAcc}
end
end,
{0, 0, 0},
LogTypeToLastBytes
),
file:close(FileDescriptor),
Result.
%% @private
log_type(memory) -> ignore;
log_type(convergence) -> ignore;
log_type(experiment_started) -> ignore;
log_type(aae_send) -> state;
log_type(aae_send_protocol) -> dissemination_protocol;
log_type(delta_send) -> state;
log_type(delta_send_protocol) -> dissemination_protocol;
log_type(broadcast) -> state;
log_type(broadcast_protocol) -> overlay_protocol.
%% @private
read_lines(FilePath, FileDescriptor) ->
case io:get_line(FileDescriptor, '') of
eof ->
[];
{error, Error} ->
lager:warning("Error while reading line from file ~p. Error: ~p", [FilePath, Error]),
[];
Line ->
[Line | read_lines(FilePath, FileDescriptor)]
end.
%% @private
write_to_file(InputFile, Line) ->
file:write_file(InputFile, Line, [write]).
%% @private
append_to_file(InputFile, Line) ->
file:write_file(InputFile, Line, [append]).
%% @doc Average all executions
average_state_and_protocol_mb(ToAverage) ->
{NumberOfExecutions, StateSum, DisseminationSum, OverlaySum} = orddict:fold(
fun(_Timestamp, {StateMB, DisseminationMB, OverlayMB}, {CountAcc, StateAcc, DisseminationAcc, OverlayAcc}) ->
{CountAcc + 1, StateAcc + StateMB, DisseminationAcc + DisseminationMB, OverlayAcc + OverlayMB}
end,
{0, 0, 0, 0},
ToAverage
),
{StateSum / NumberOfExecutions, DisseminationSum / NumberOfExecutions, OverlaySum / NumberOfExecutions}.
%% @private
run_gnuplot(InputFile, OutputFile) ->
Bin = case os:getenv("MESOS_TASK_ID", "false") of
"false" ->
"gnuplot";
_ ->
"/usr/bin/gnuplot"
end,
Command = Bin ++ " -e \""
++ "outputname='" ++ OutputFile ++ "'; "
++ "inputname='" ++ InputFile ++ "'\" " ++ gnuplot_file(),
io:format("~p~n~n", [Command]),
os:cmd(Command).