-
Notifications
You must be signed in to change notification settings - Fork 0
/
lasp_partition_overcounting_plot.sh
executable file
·221 lines (191 loc) · 6.11 KB
/
lasp_partition_overcounting_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
#!/usr/bin/env escript
-author("Vitor Enes Duarte <[email protected]>").
main(_) ->
ValidDirectories = ordsets:from_list(["ad_counter_partition_overcounting"]),
%% 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) ->
{ClientServer, PeerToPeer, CodePeerToPeer} = lists:foldl(
fun(EvalId, {CS0, PP0, CPP0}) ->
CPP1 = case lists:prefix("code_peer_to_peer_", EvalId) of
true ->
[EvalId | CPP0];
false ->
CPP0
end,
PP1 = case lists:prefix("peer_to_peer", EvalId) of
true ->
[EvalId | PP0];
false ->
PP0
end,
CS1 = case lists:prefix("client_server", EvalId) of
true ->
[EvalId | CS0];
false ->
CS0
end,
{CS1, PP1, CPP1}
end,
{[], [], []},
EvalIds
),
ClientServerAvg = average(Simulation, ClientServer),
PeerToPeerAvg = average(Simulation, PeerToPeer),
CodePeerToPeerAvg = average(Simulation, CodePeerToPeer),
io:format("Client Server Average ~p~n", [ClientServerAvg]),
io:format("Peer-to-Peer Average ~p~n", [PeerToPeerAvg]),
io:format("Code Peer-to-Peer Average ~p~n", [CodePeerToPeerAvg]),
InputFile1 = write_to_file(Simulation, "client_server", ClientServerAvg),
InputFile2 = write_to_file(Simulation, "peer_to_peer", PeerToPeerAvg),
InputFile3 = write_to_file(Simulation, "code_peer_to_peer", CodePeerToPeerAvg),
InputFiles = [InputFile1, InputFile2, InputFile3],
Titles = ["Client Server", "Peer-to-Peer", "Code + Peer-to-Peer"],
OutputFile = output_file(Simulation),
Result = run_gnuplot(InputFiles, Titles, OutputFile),
io:format("Generating average plot ~p. Output: ~p~n~n", [OutputFile, Result]),
delete_files(InputFiles).
%% @private
average(Simulation, EvalIds) ->
lists:foldl(
fun(EvalId, Map) ->
PartitionProbability = get_partition_probability(EvalId),
EvalIdDir = root_log_dir() ++ "/" ++ Simulation ++ "/" ++ EvalId,
EvalTimestamps = only_dirs(EvalIdDir),
Avg = average_executions(EvalIdDir, EvalTimestamps),
orddict:store(PartitionProbability, Avg, Map)
end,
orddict:new(),
EvalIds
).
%% @private
get_partition_probability(EvalId) ->
Tokens = string:tokens(EvalId, "_"),
PartitionProbability = lists:last(Tokens),
list_to_integer(PartitionProbability).
%% @private
average_executions(EvalIdDir, EvalTimestamps) ->
Sum = lists:foldl(
fun(EvalTimestamp, Acc) ->
File = EvalIdDir ++ "/" ++ EvalTimestamp ++ "/overcounting",
Overcounting = get_overcounting(File),
Acc + Overcounting
end,
0,
EvalTimestamps
),
Sum / length(EvalTimestamps).
%% @private
get_overcounting(FilePath) ->
%% Open log file
{ok, FileDescriptor} = file:open(FilePath, [read]),
Line = io:get_line(FileDescriptor, ''),
list_to_float(Line).
%% @private
write_to_file(Simulation, Name, Map) ->
Path = root_plot_dir() ++ "/" ++ Simulation ++ "/",
filelib:ensure_dir(Path),
InputFile = Path ++ Name ++ ".csv",
file:write_file(InputFile, "", [write]),
lists:foreach(
fun({PartitionProbability, Overcounting}) ->
append_to_file(InputFile, PartitionProbability, Overcounting)
end,
Map
),
InputFile.
%% @private
append_to_file(InputFile, PartitionProbability, Overcounting) ->
Line = io_lib:format("~w,~w\n", [PartitionProbability, Overcounting]),
file:write_file(InputFile, Line, [append]).
%% @private
root_log_dir() ->
"logs".
%% @private
root_plot_dir() ->
"plots".
%% @private
gnuplot_file() ->
"partition_overcounting.gnuplot".
%% @private
output_file(Simulation) ->
root_plot_dir() ++ "/" ++ Simulation ++ "/partition_overcounting.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
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) ->
% "partition_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).
%% @private
delete_files(Files) ->
lists:foreach(
fun(File) ->
ok = file:delete(File)
end,
Files
).