-
Notifications
You must be signed in to change notification settings - Fork 0
/
COSbenchWorkloadGenerator.java
570 lines (469 loc) · 18 KB
/
COSbenchWorkloadGenerator.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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import java.text.NumberFormat;
import java.util.List;
public class COSbenchWorkloadGenerator {
private Document doc;
private String num_of_workers;
private String runtime;
private String output_path;
private String delay;
private String auth_url;
private String rampup;
private String num_of_drivers;
public static void main(String argv[]) {
if (argv.length < 1) {
System.out.println("Usage: java " + System.getProperty("sun.java.command") + " <test_matrix_input_txt>");
System.exit(1);
}
String test_matrix_filename = argv[0];
COSbenchWorkloadGenerator xGen = new COSbenchWorkloadGenerator();
BufferedReader br = null;
Properties prop = new Properties();
try {
prop.load(new FileInputStream("config.properties"));
xGen.num_of_workers = prop.getProperty("num_of_workers_per_stage");
xGen.runtime = prop.getProperty("runtime");
xGen.output_path = prop.getProperty("output_path")
+ "/" + test_matrix_filename.substring(0, test_matrix_filename.lastIndexOf('.'))
+ "/";
xGen.delay = prop.getProperty("delay_between_main_stages");
xGen.auth_url = prop.getProperty("auth_url");
xGen.rampup = prop.getProperty("rampup");
xGen.num_of_drivers = prop.getProperty("num_of_drivers");
} catch (FileNotFoundException e1) {
e1.printStackTrace();
return;
} catch (IOException e1) {
e1.printStackTrace();
return;
}
try {
String sCurrentLine;
br = new BufferedReader(new FileReader(test_matrix_filename));
while ((sCurrentLine = br.readLine()) != null) {
String fields[] = sCurrentLine.split("\\t");
String sizes[];
int sizelength;
boolean isRange;
String unit = "KB";
if (fields[1].contains("-")) {
sizes = fields[1].substring(fields[1].indexOf("(") + 1,
fields[1].indexOf(")")).split("-");
unit = fields[1].substring(fields[1].indexOf(")") + 1);
isRange = true;
} else {
sizes = fields[1].substring(fields[1].indexOf("(") + 1,
fields[1].indexOf(")")).split(",");
isRange = false;
}
String containers[] = fields[3].split(",");
String readWriteDeletePercent[] = { "5,90,5", "45,50,5",
"85,10,5", "98,1,1", "64,32,4" };
String objects = fields[2];
// isRange then set sized to just 1
if (isRange)
sizelength = 1;
else
sizelength = sizes.length;
xGen.createXML(fields, sizes, containers, objects,
readWriteDeletePercent, isRange, sizelength, unit);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
private static int roundUp(final int val)
{
double exponent = (int) Math.ceil(Math.log10(val));
return (int) Math.pow(10, exponent);
}
public void createXML(String[] fields, String[] sizes, String[] containers,
String objects, String[] readWriteDeletePercent, boolean isRange,
int sizelength, String unit) {
try {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
doc = docBuilder.newDocument();
// workload element
Element workload = doc.createElement("workload");
doc.appendChild(workload);
// workload attributes
Attr attr = doc.createAttribute("name");
attr.setValue(fields[0]);
workload.setAttributeNode(attr);
attr = doc.createAttribute("description");
attr.setValue(fields[1]);
workload.setAttributeNode(attr);
// storage element
Element storage = doc.createElement("storage");
workload.appendChild(storage);
// storage attributes
attr = doc.createAttribute("type");
attr.setValue("swift");
storage.setAttributeNode(attr);
// auth element
Element auth = doc.createElement("auth");
workload.appendChild(auth);
// auth attributes
attr = doc.createAttribute("type");
attr.setValue("swauth");
auth.setAttributeNode(attr);
attr = doc.createAttribute("config");
attr.setValue(auth_url);
auth.setAttributeNode(attr);
// workflow element
Element workflow = doc.createElement("workflow");
workload.appendChild(workflow);
// multiple init and prepare stages
int numPrepWorkers = (Integer.valueOf(num_of_drivers) == 0) ? 1
: Integer.valueOf(objects) / Integer.valueOf(num_of_drivers);
int nextPowerOf2 = (numPrepWorkers == 0) ? 0
: 32 - Integer.numberOfLeadingZeros(numPrepWorkers - 1);
// limit at 11 (2048 workers max)
numPrepWorkers = (nextPowerOf2 < 12) ? (1 << nextPowerOf2) : (1 << 11);
numPrepWorkers = (numPrepWorkers < 4) ? 4 : numPrepWorkers;
// create init stages
int previousContainerValue = 0;
for (int i = 0; i < sizelength; i++) {
for (int j = 0; j < containers.length; j++) {
String containerString = containers[j];
String from_container = previousContainerValue
+ Integer.valueOf(containerString) + "";
String to_container = Integer.valueOf(from_container)
+ Integer.valueOf(containerString) - 1 + "";
previousContainerValue = Integer.valueOf(to_container);
createWorkstage(workflow, "init", num_of_drivers,
"containers=r(" + from_container + ","
+ to_container + ")", -1);
}
}
// Determine the number of read/write/delete combinations
int numRWDCombinations = 0;
for (int k = 0; k < readWriteDeletePercent.length; k++) {
if ((k + 4) > fields.length - 1
|| fields[k + 4].length() == 0) {
continue;
}
++numRWDCombinations;
}
// create prepare stages
previousContainerValue = 0;
int previousObjectValue = 0;
int num_containers = 0;
int num_objects = 0;
int num_objects_per_container = 0;
for (int i = 0; i < sizelength; i++) {
for (int j = 0; j < containers.length; j++) {
String sizeString;
if (isRange)
sizeString = "(" + sizes[0] + "," + sizes[1] + ")"
+ unit;
else
sizeString = "("
+ sizes[i].substring(0, sizes[i].length() - 2)
+ ")"
+ sizes[i].substring(sizes[i].length() - 2);
String containerString = containers[j];
num_containers = Integer.valueOf(containerString);
num_objects = Integer.valueOf(objects);
num_objects_per_container = num_objects;
String from_container = previousContainerValue
+ num_containers + "";
String to_container = Integer.valueOf(from_container)
+ num_containers - 1 + "";
previousContainerValue = Integer.valueOf(to_container);
// 1 set of objects for read/writes
// n sets of objects for deletes
String from_object = previousObjectValue + 1 + "";
String to_object = Integer.valueOf(from_object)
+ ((num_objects_per_container) *
(numRWDCombinations + 1)) - 1 + ""; // 1+n times the number of objects
previousObjectValue = Integer.valueOf(to_object);
if (isRange)
createWorkstage(
workflow,
"prepare",
numPrepWorkers + "",
"containers=r(" + from_container + ","
+ to_container + ");objects=r("
+ from_object + "," + to_object
+ ");sizes=u" + sizeString,
num_objects_per_container * num_containers);
else
createWorkstage(
workflow,
"prepare",
numPrepWorkers + "",
"containers=r(" + from_container + ","
+ to_container + ");objects=r("
+ from_object + "," + to_object
+ ");sizes=c" + sizeString,
num_objects_per_container * num_containers);
}
}
// multiple main stages
previousContainerValue = 0;
previousObjectValue = 0;
int previousDeleteObjectValue = 0;
for (int i = 0; i < sizelength; i++) {
for (int j = 0; j < containers.length; j++) {
String sizeString;
if (isRange)
sizeString = "(" + sizes[0] + "," + sizes[1] + ")"
+ unit;
else
sizeString = "("
+ sizes[i].substring(0, sizes[i].length() - 2)
+ ")"
+ sizes[i].substring(sizes[i].length() - 2);
String containerString = containers[j];
String from_container = previousContainerValue
+ Integer.valueOf(containerString) + "";
String to_container = Integer.valueOf(from_container)
+ Integer.valueOf(containerString) - 1 + "";
previousContainerValue = Integer.valueOf(to_container);
num_containers = Integer.valueOf(to_container) - Integer.valueOf(from_container) + 1;
num_objects = Integer.valueOf(objects);
num_objects_per_container = num_objects;
String from_object = previousObjectValue + 1 + "";
String to_object = Integer.valueOf(from_object)
+ ((num_objects_per_container) - 1) + "";
for (int k = 0; k < readWriteDeletePercent.length; k++) {
if ((k + 4) > fields.length - 1
|| fields[k + 4].length() == 0) {
continue;
}
String readRatio = readWriteDeletePercent[k].split(",")[0];
String writeRatio = readWriteDeletePercent[k]
.split(",")[1];
String deleteRatio = readWriteDeletePercent[k]
.split(",")[2];
if (previousDeleteObjectValue == 0)
previousDeleteObjectValue = Integer.valueOf(to_object);
else
previousDeleteObjectValue += num_objects_per_container;
createNormalWorkstage(fields[1].substring(0, fields[1].indexOf("(")),
workflow, num_of_workers, runtime, readRatio,
writeRatio, deleteRatio, isRange,
from_container, to_container, num_objects_per_container,
from_object, to_object, previousDeleteObjectValue + 1, sizeString);
}
previousObjectValue = previousDeleteObjectValue + (num_objects_per_container);
previousDeleteObjectValue = 0;
}
}
/*
// multiple cleanup stages
previousContainerValue = 0;
previousObjectValue = 0;
for (int i = 0; i < sizelength; i++) {
for (int j = 0; j < containers.length; j++) {
String containerString = containers[j];
num_containers = Integer.valueOf(containerString);
num_objects = Integer.valueOf(objects);
num_objects_per_container = num_objects;
String from_container = previousContainerValue
+ Integer.valueOf(containerString) + "";
String to_container = Integer.valueOf(from_container)
+ (Integer.valueOf(containerString) - 1) + "";
previousContainerValue = Integer.valueOf(to_container);
String from_object = (previousObjectValue + 1) + "";
String to_object = Integer.valueOf(from_object)
+ (((num_objects_per_container) * (numRWDCombinations + 1)) - 1) + "";
previousObjectValue = Integer.valueOf(to_object);
createWorkstage(workflow, "cleanup", numPrepWorkers + "", "containers=r("
+ from_container + "," + to_container
+ ");objects=r(" + from_object + "," + to_object
+ ")", -1);
}
}
// multiple dispose stages
previousContainerValue = 0;
for (int i = 0; i < sizelength; i++) {
for (int j = 0; j < containers.length; j++) {
String containerString = containers[j];
String from_container = previousContainerValue
+ Integer.valueOf(containerString) + "";
String to_container = Integer.valueOf(from_container)
+ Integer.valueOf(containerString) - 1 + "";
previousContainerValue = Integer.valueOf(to_container);
createWorkstage(workflow, "dispose", Integer.valueOf(num_of_drivers) + "", "containers=r("
+ from_container + "," + to_container + ")", -1);
}
}
*/
// write the content into xml file
TransformerFactory transformerFactory = TransformerFactory
.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(
"{http://xml.apache.org/xslt}indent-amount", "10");
DOMSource source = new DOMSource(doc);
// StreamResult result = new StreamResult(new
// File(output_path+fields[0].replaceAll("\\s+",
// "")+"-"+fields[1].substring(0,fields[1].indexOf("(")).replaceAll("\\s+",
// "")+"-"+sizeString.replaceAll("[()]",
// "")+"-"+objectString+"-"+containerString+"-template"+".xml"));
File f = new File(output_path);
f.mkdirs();
f = new File(output_path + fields[0].replaceAll("\\s+", "") + ".xml");
StreamResult result = new StreamResult(f);
// Output to console for testing
// StreamResult result = new StreamResult(System.out);
transformer.transform(source, result);
} catch (ParserConfigurationException pce) {
pce.printStackTrace();
} catch (TransformerException tfe) {
tfe.printStackTrace();
}
}
private void createNormalWorkstage(String workloadType, Element workflow,
String workers, String runtime, String readRatio,
String writeRatio, String deleteRatio, boolean isRange,
String from_container, String to_container, int objects,
String from_object, String to_object, int previousDeleteObjectValue,
String sizeString) {
Attr attr;
Element workstage = doc.createElement("workstage");
workflow.appendChild(workstage);
String stageName = "main-"
+ workloadType.replaceAll("\\s+", "")
+ sizeString
+ "_c"
+ (Integer.valueOf(to_container)
- Integer.valueOf(from_container) + 1)
+ "_o"
+ (Integer.valueOf(to_object) - Integer.valueOf(from_object) + 1)
+ "_r" + readRatio + "w" + writeRatio + "d" + deleteRatio
+ "_NUMWORKERS";
attr = doc.createAttribute("name");
attr.setValue(stageName);
workstage.setAttributeNode(attr);
attr = doc.createAttribute("closuredelay");
attr.setValue(delay);
workstage.setAttributeNode(attr);
Element work = doc.createElement("work");
workstage.appendChild(work);
attr = doc.createAttribute("name");
attr.setValue(stageName);
work.setAttributeNode(attr);
attr = doc.createAttribute("workers");
attr.setValue(workers);
work.setAttributeNode(attr);
attr = doc.createAttribute("rampup");
attr.setValue(rampup);
work.setAttributeNode(attr);
attr = doc.createAttribute("runtime");
attr.setValue(runtime);
work.setAttributeNode(attr);
// Read operation
Element operation = doc.createElement("operation");
work.appendChild(operation);
attr = doc.createAttribute("type");
attr.setValue("read");
operation.setAttributeNode(attr);
attr = doc.createAttribute("ratio");
attr.setValue(readRatio);
operation.setAttributeNode(attr);
attr = doc.createAttribute("config");
attr.setValue("containers=u(" + from_container + "," + to_container
+ ");objects=u(" + from_object + "," + to_object + ")");
operation.setAttributeNode(attr);
// Write operation
operation = doc.createElement("operation");
work.appendChild(operation);
attr = doc.createAttribute("type");
attr.setValue("write");
operation.setAttributeNode(attr);
attr = doc.createAttribute("ratio");
attr.setValue(writeRatio);
operation.setAttributeNode(attr);
if (isRange) {
attr = doc.createAttribute("config");
attr.setValue("containers=u(" + from_container + "," + to_container
+ ");objects=u(" + from_object + "," + to_object
+ ");sizes=u" + sizeString);
operation.setAttributeNode(attr);
} else {
attr = doc.createAttribute("config");
attr.setValue("containers=u(" + from_container + "," + to_container
+ ");objects=u(" + from_object + "," + to_object
+ ");sizes=c" + sizeString);
operation.setAttributeNode(attr);
}
// Delete operation
from_object = previousDeleteObjectValue + "";
to_object = previousDeleteObjectValue + (objects - 1) + "";
operation = doc.createElement("operation");
work.appendChild(operation);
attr = doc.createAttribute("type");
attr.setValue("delete");
operation.setAttributeNode(attr);
attr = doc.createAttribute("ratio");
attr.setValue(deleteRatio);
operation.setAttributeNode(attr);
attr = doc.createAttribute("config");
attr.setValue("containers=u(" + from_container + "," + to_container
+ ");objects=u(" + from_object + "," + to_object + ")");
operation.setAttributeNode(attr);
}
public void createWorkstage(Element workflow, String name, String workers,
String config, int totalOps) {
Attr attr;
String configStr = config;
String stageName = name
+ configStr.replaceAll("containers=", "-c")
.replaceAll(";objects=", "-o")
.replaceAll(";sizes=", "-s")
+ "-" + workers;
Element workstage = doc.createElement("workstage");
workflow.appendChild(workstage);
attr = doc.createAttribute("name");
attr.setValue(stageName);
workstage.setAttributeNode(attr);
Element work = doc.createElement("work");
workstage.appendChild(work);
attr = doc.createAttribute("type");
attr.setValue(name);
work.setAttributeNode(attr);
attr = doc.createAttribute("workers");
if (Integer.valueOf(workers) > 4096)
workers = "4096";
attr.setValue(workers);
work.setAttributeNode(attr);
if (totalOps > 0) {
attr = doc.createAttribute("totalOps");
attr.setValue(Integer.toString(totalOps));
work.setAttributeNode(attr);
}
attr = doc.createAttribute("config");
attr.setValue(config);
work.setAttributeNode(attr);
}
}