TASK_COUNT = Integer.parseInt(args[0]);
REPS = Integer.parseInt(args[1]);
}
/* Set up Threadr. */
Threadr threadr = new Threadr();
threadr.enableLogging();
threadr.setOutputLevel(Level.FINEST);
threadr.setSequentialHandler(new PostSequentialHandler());
for (int i = 1; i <= TASK_COUNT; i++) {
MemoryMonitor monitor = MemoryMonitor.getInstance();
List<Long> seqs = new ArrayList<>();
List<Long> pars = new ArrayList<>();
List<Long> thrs = new ArrayList<>();
List<Float> impsPar = new ArrayList<>();
List<Float> impsThr = new ArrayList<>();
List<Double> seqMemUse = new ArrayList<>();
List<Double> thrMemUse = new ArrayList<>();
List<Double> parMemUse = new ArrayList<>();
ThreadrTest[] test = new ThreadrTest[i];
for (int j = 0; j < i; j++) {
test[j] = new ThreadrTest();
}
Test2[] test2 = new Test2[i];
for (int j = 0; j < i; j++) {
test2[j] = new Test2();
}
ThreadrTest[] test3 = new ThreadrTest[i];
for (int j = 0; j < i; j++) {
test3[j] = new ThreadrTest();
}
/* Save the generated tests. */
saveTest("test1", test);
saveTest("test2", test2);
saveTest("test3", test3);
System.out.println("\n------------------------------");
System.out.printf("Test number - %d;\n", i);
System.out.printf("Repetitions - %d;\n", REPS);
System.out.printf("Tasks per test - %d;\n", i);
for (int j = 0; j < REPS;) {
/* Load the saved state of the parallelism test. */
test = (ThreadrTest[]) loadTests("test1");
/* Load the saved state of the threading test. */
test2 = (Test2[]) loadTests("test2");
/* Load the saved state of the sequential test. */
test3 = (ThreadrTest[]) loadTests("test3");
long startTime, endTime, diff1, diff2, diff3;
/* Start the memory monitor. */
Thread monitorThread = new Thread(monitor);
monitorThread.start();
System.gc();
/* Run the sequential tests. */
startTime = System.currentTimeMillis();
for (ThreadrTest t : test3) {
Threadr.logger.finer(t.toString());
AbstractReturnType<?> result = t.call();
Threadr.logger.finer(result.toString());
}
endTime = System.currentTimeMillis();
/* Stop the memory monitor. */
try {
monitorThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
seqMemUse.add(monitor.getMaxUsedMemory(MemoryUnits.MEGABYTE));
diff1 = endTime - startTime;
seqs.add(diff1);
Threadr.logger.fine(String.format(
"\tNon-threaded tasks execution time %dms\n", diff1));
/* Test parallel executions. */
System.gc();
threadr.reset();
threadr.addTasks(test);
for (ThreadrTest t : test) {
Threadr.logger.finer(t.toString());
}
startTime = System.currentTimeMillis();
ThreadrResult results = threadr.execute();
endTime = System.currentTimeMillis();
diff2 = endTime - startTime;
pars.add(diff2);
double totalMem = threadr.getMemoryMonitor().getMaxUsedMemory(MemoryUnits.MEGABYTE);
/* Get all the MemoryMonitors out from the results. */
for (AbstractReturnType<?> result : results.getResults()) {
/*
* If the monitors are different, we must be looking at
* another VM.
*/
MemoryMonitor resultMon = (MemoryMonitor) result.getResult();
if (!result.getResult().equals(threadr.getMemoryMonitor())) {
totalMem += resultMon.getMaxUsedMemory(MemoryUnits.MEGABYTE);
}
}
parMemUse.add(totalMem);
Threadr.logger
.fine(String
.format("\nVirtual Machine tasks execution time %dms\n",
diff2));
for (AbstractReturnType<?> result : results.getResults()) {
Threadr.logger.finer(result.toString());
}
/* Work out the percentage increase in execution speed. */
float percentage = (((float) (diff1 - diff2) / (float) diff1) * 100.0f);
impsPar.add(percentage);
Threadr.logger.fine(String.format(
"\tImprovement: %03.02f%% \n", percentage));
/* Set up and test threaded executions. */
System.gc();
threadr.reset();
threadr.addTasks(test2);
for (Test2 t : test2) {
Threadr.logger.finer(t.toString());
}
startTime = System.currentTimeMillis();
results = threadr.execute();
endTime = System.currentTimeMillis();
diff3 = endTime - startTime;
thrs.add(diff3);
totalMem = threadr.getMemoryMonitor().getMaxUsedMemory(MemoryUnits.MEGABYTE);
for (AbstractReturnType<?> result : results.getResults()) {
MemoryMonitor resultMon = (MemoryMonitor) result
.getResult();
if (!result.getResult().equals(threadr.getMemoryMonitor())) {
totalMem += resultMon.getMaxUsedMemory(MemoryUnits.MEGABYTE);
}
}
thrMemUse.add(totalMem);
Threadr.logger.fine(String.format(