-
Notifications
You must be signed in to change notification settings - Fork 2
/
CsvUtil.java
253 lines (231 loc) · 9.2 KB
/
CsvUtil.java
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
package scenario.util;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.LinkedList;
import java.util.List;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVPrinter;
import org.apache.commons.csv.CSVRecord;
import metrics.MetricConsts;
import metrics.embedding.AcceptedVnrMetric;
import metrics.embedding.AveragePathLengthMetric;
import metrics.embedding.OperatingCostMetric;
import metrics.embedding.TotalCommunicationCostMetricA;
import metrics.embedding.TotalCommunicationCostMetricB;
import metrics.embedding.TotalCommunicationCostMetricC;
import metrics.embedding.TotalCommunicationCostMetricD;
import metrics.embedding.TotalCommunicationCostObjectiveC;
import metrics.embedding.TotalCommunicationCostObjectiveD;
import metrics.embedding.TotalPathCostMetric;
import metrics.embedding.TotalTafCommunicationCostMetric;
import metrics.manager.GlobalMetricsManager;
import model.SubstrateNetwork;
/**
* CSV utilities for exporting the metrics.
*
* @author Maximilian Kratz {@literal <maximilian.kratz@es.tu-darmstadt.de>}
*/
public class CsvUtil {
/**
* Private constructor ensures no instantiation of this class.
*/
private CsvUtil() {
}
/**
* Counter for the number of lines within the CSV output file.
*/
protected static int csvCounter = 0;
/**
* CSV file header format for normal runs (simulation).
*/
private static CSVFormat formatNormal = CSVFormat.DEFAULT.withHeader(//
"counter", //
"timestamp", //
"lastVNR", //
"time_pm", //
"time_ilp", //
"time_deploy", //
"time_rest", //
"accepted_vnrs", //
"total_path_cost", //
"average_path_length", //
"total_communication_cost_a", //
"total_communication_cost_b", //
"total_communication_cost_c", //
"total_communication_cost_d", //
"total_communication_objective_c", //
"total_communication_objective_d", //
"total_taf_communication_cost", //
"operation_cost", //
"memory_start", //
"memory_ilp", //
"memory_end", //
"memory_pid_max");
/**
* CSV file header format for mean and standard derivation (after simulation).
*/
private static CSVFormat formatStats = CSVFormat.DEFAULT.withHeader(//
"counter", //
"time_pm", "time_pm_stddev", //
"time_ilp", "time_ilp_stddev", //
"time_deploy", "time_deploy_stddev", //
"time_rest", "time_rest_stddev", //
"accepted_vnrs", "accepted_vnrs_stddev", //
"total_path_cost", "total_path_cost_stddev", //
"average_path_length", "average_path_length_stddev", //
"total_communication_cost_a", "total_communication_cost_a_stddev", //
"total_communication_cost_b", "total_communication_cost_b_stddev", //
"total_communication_cost_c", "total_communication_cost_c_stddev", //
"total_communication_cost_d", "total_communication_cost_d_stddev", //
"total_communication_objective_c", "total_communication_objective_c_stddev", //
"total_communication_objective_d", "total_communication_objective_d_stddev", //
"total_taf_communication_cost", "total_taf_communication_cost_stddev", //
"operation_cost", "operation_cost_stddev", //
"memory_start", "memory_start_stddev", //
"memory_ilp", "memory_ilp_stddev", //
"memory_end", "memory_end_stddev", //
"memory_pid_max", "memory_pid_max_stddev", //
"time_total", "time_total_stddev" //
);
/**
* CSV file header format for summing up the time of an experiment (after
* simulation).
*/
private static CSVFormat formatTimeSum = CSVFormat.DEFAULT.withHeader( //
"time_total", "time_total_stddev", //
"time_pm", "time_pm_stddev", //
"time_ilp", "time_ilp_stddev", //
"time_deploy", "time_deploy_stddev", //
"time_rest", "time_rest_stddev" //
);
/**
* Appends the current state of the metrics to the CSV file.
*
* @param lastVnr The Name of the last embedded virtual network (request).
* @param csvPath Path for the CSV file.
* @param sNet Substrate network to export metrics for.
*/
public static void appendCsvLine(final String lastVnr, final String csvPath, final SubstrateNetwork sNet) {
final String[] content = new String[22];
content[0] = String.valueOf(csvCounter++); // line counter
content[1] = String.valueOf(java.time.LocalDateTime.now()); // time stamp
content[2] = String.valueOf(lastVnr); // name of the last embedded virtual network
content[3] = String.valueOf(GlobalMetricsManager.getRuntime().getPmValue() / MetricConsts.NANO_TO_MILLI); // PM
content[4] = String.valueOf(GlobalMetricsManager.getRuntime().getIlpValue() / MetricConsts.NANO_TO_MILLI); // ILP
content[5] = String.valueOf(GlobalMetricsManager.getRuntime().getDeployValue() / MetricConsts.NANO_TO_MILLI); // Deploy
content[6] = String.valueOf(GlobalMetricsManager.getRuntime().getRestValue() / MetricConsts.NANO_TO_MILLI); // Rest
content[7] = String.valueOf((int) new AcceptedVnrMetric(sNet).getValue());
content[8] = String.valueOf(new TotalPathCostMetric(sNet).getValue());
content[9] = String.valueOf(new AveragePathLengthMetric(sNet).getValue());
content[10] = String.valueOf(new TotalCommunicationCostMetricA(sNet).getValue());
content[11] = String.valueOf(new TotalCommunicationCostMetricB(sNet).getValue());
content[12] = String.valueOf(new TotalCommunicationCostMetricC(sNet).getValue());
content[13] = String.valueOf(new TotalCommunicationCostMetricD(sNet).getValue());
content[14] = String.valueOf(new TotalCommunicationCostObjectiveC(sNet).getValue());
content[15] = String.valueOf(new TotalCommunicationCostObjectiveD(sNet).getValue());
content[16] = String.valueOf(new TotalTafCommunicationCostMetric(sNet).getValue());
content[17] = String.valueOf(new OperatingCostMetric(sNet).getValue());
if (GlobalMetricsManager.getMemory() != null) {
// Memory start execute
content[18] = String.valueOf(GlobalMetricsManager.getMemory().getValue(0));
// Memory before ILP
content[19] = String.valueOf(GlobalMetricsManager.getMemory().getValue(1));
// Memory end execute
content[20] = String.valueOf(GlobalMetricsManager.getMemory().getValue(2));
// Maximum amount of memory (RAM) consumed
content[21] = String.valueOf(GlobalMetricsManager.getMemoryPid());
} else {
for (int i = 18; i <= 21; i++) {
content[i] = String.valueOf(-1);
}
}
writeCsvLine(csvPath, formatNormal, content);
}
/**
* Loads the CSV file for the given path and parses its metric values to a list
* (lines) of double arrays (columns). Please notice that this method has
* hard-coded values for the specific metric implementation.
*
* @param csvPath Path for the metric CSV file to load.
* @return List of double arrays with the metrics.
*/
public static List<Double[]> loadCsvFile(final String csvPath) {
final List<Double[]> metrics = new LinkedList<>();
try {
final CSVParser parser = new CSVParser(new FileReader(csvPath), formatNormal);
final List<CSVRecord> recs = parser.getRecords();
for (int i = 1; i < recs.size(); i++) {
final Double[] val = new Double[20];
final CSVRecord rec = recs.get(i);
for (int j = 3; j <= 21; j++) {
val[j - 3] = Double.valueOf(rec.get(j));
}
// Sum time metrics up
val[19] = val[0] // time_pm
+ val[1] // time_ilp
+ val[2] // time_deploy
+ val[3]; // time_rest
metrics.add(val);
}
} catch (final IOException e) {
e.printStackTrace();
}
return metrics;
}
/**
* Appends the given line to the statistics CSV file.
*
* @param csvPath Path for the statistics CSV file.
* @param statsLine Line to add to the statistics CSV file.
*/
public static void appendCsvStatsLine(final String csvPath, final String[] statsLine) {
writeCsvLine(csvPath, formatStats, statsLine);
}
/**
* Creates a CSV file with the header and one line of the summed time values.
*
* @param csvPath Path for the time sum CSV file.
* @param statsLine Line to add to the time sume CSV file.
*/
public static void createCsvTimeSumFile(final String csvPath, final String[] statsLine) {
writeCsvLine(csvPath, formatTimeSum, statsLine);
}
/**
* Method that actually writes the CSV file.
*
* @param csvPath Path for the CSV file to create or append line.
* @param format Chosen header format of the CSV file.
* @param content Content of the line to add as string array.
*/
private static void writeCsvLine(final String csvPath, final CSVFormat format, final String[] content) {
// If file path is null, do not create a file at all
if (csvPath == null) {
return;
}
try {
BufferedWriter out;
// If file does not exist, write header to it
if (Files.notExists(Path.of(csvPath))) {
out = Files.newBufferedWriter(Paths.get(csvPath), StandardOpenOption.APPEND, StandardOpenOption.CREATE);
try (final CSVPrinter printer = new CSVPrinter(out, format)) {
printer.close();
}
}
out = Files.newBufferedWriter(Paths.get(csvPath), StandardOpenOption.APPEND, StandardOpenOption.CREATE);
try (final CSVPrinter printer = new CSVPrinter(out, CSVFormat.DEFAULT)) {
printer.printRecord((Object[]) content);
printer.close();
}
out.close();
} catch (final IOException e) {
// TODO: Error handling
e.printStackTrace();
}
}
}