Browse Source

Wrote tests to compare the read time of files vs number of cpu threads used.

pull/18/head
jrtechs 5 years ago
parent
commit
f1ae532387
4 changed files with 142 additions and 7 deletions
  1. +71
    -0
      multiThreadedFileIO/DataCreator.java
  2. +40
    -0
      multiThreadedFileIO/MultiThreadedFileReadTest.java
  3. +18
    -7
      multiThreadedFileIO/TaskManager.java
  4. +13
    -0
      multiThreadedFileIO/graph.py

+ 71
- 0
multiThreadedFileIO/DataCreator.java View File

@ -0,0 +1,71 @@
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.math.*;
/**
* Simple class to generate random data for
* file IO tests.
*
* @author Jeffery Russell 1-31-19
*/
public class DataCreator
{
/**
* Generates an obscure random character
* @return
*/
private static char rndChar()
{
// or use Random or whatever
int rnd = (int) (Math.random() * 52);
char base = (rnd < 26) ? 'A' : 'a';
return (char) (base + rnd % 26);
}
/**
* Simple function to save contents to the disk
*
* @param s
* @param fileName
*/
private static void saveToDisk(String s, String fileName)
{
BufferedWriter writer;
try
{
File file = new File(fileName);
file.createNewFile();
writer = new BufferedWriter(new FileWriter(file));
writer.write(s);
writer.flush();
writer.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
/**
* Creates 5MB of random test data to use
*/
public static void main(String[] arguments)
{
System.out.println("Creating Test Files");
for(int i = 0; i < 100; i++)
{
//10k random characters for each file
//each file is 10kb * 500 files, total size of 5MB
String s = "";
for(int j = 0; j < 1000000; j++)
{
s = s + rndChar();
}
saveToDisk(s, "./testData/" + i + ".txt");
System.out.println(s);
}
}
}

+ 40
- 0
multiThreadedFileIO/MultiThreadedFileReadTest.java View File

@ -0,0 +1,40 @@
import java.util.*;
/**
* File to test the performance of multi threaded file
* io by reading a large quantity of files in parallel
* using a different amount of threads.
*
* @author Jeffery Russell 1-31-19
*/
public class MultiThreadedFileReadTest
{
public static void main(String[] args)
{
List<Integer> x = new ArrayList<>();
List<Double> y = new ArrayList<>();
for(int i = 1; i <= 64; i++)
{
long threadTotal = 0;
for(int w = 0; w < 20; w++)
{
TaskManager boss = new TaskManager(i);
for(int j = 0; j < 500; j++)
{
boss.addTask(new ReadTask("./testData/" + i + ".txt"));
}
long startTime = System.nanoTime();
boss.runTasks();
long endTime = System.nanoTime();
long durationMS = (endTime - startTime)/1000000;
threadTotal+= durationMS;
}
x.add(i);
y.add(threadTotal/20.0); //finds average
}
System.out.println(x);
System.out.println(y);
}
}

+ 18
- 7
multiThreadedFileIO/TaskManager.java View File

@ -44,18 +44,29 @@ public class TaskManager
Thread[] runners = new Thread[desiredThreads]; Thread[] runners = new Thread[desiredThreads];
System.out.println("Total Tasks: " + tasks.size());
System.out.println("Threads Used:" + desiredThreads);
for(int i = 0; i < desiredThreads; i++) for(int i = 0; i < desiredThreads; i++)
{ {
runners[i] = new Thread(()-> runners[i] = new Thread(()->
{ {
while(!tasks.isEmpty())
ReadTask t = null;
while(true)
{ {
ReadTask t = tasks.remove(0);
t.runTask();
//need synchronized block to prevent
//race condition
synchronized (tasks)
{
if(!tasks.isEmpty())
t = tasks.remove(0);
}
if(t == null)
{
break;
}
else
{
t.runTask();
t = null;
}
} }
}); });
runners[i].start(); runners[i].start();

+ 13
- 0
multiThreadedFileIO/graph.py View File

@ -0,0 +1,13 @@
import matplotlib.pyplot as plt
xList = [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]
yList = [230.5, 124.0, 84.65, 63.9, 51.6, 44.2, 39.15, 34.9, 31.9, 28.7, 27.2, 25.05, 24.25, 22.8, 21.25, 20.45, 19.8, 19.4, 18.6, 17.45, 17.0, 16.95, 16.4, 24.15, 15.2, 15.2, 15.0, 14.3, 14.7, 14.65, 14.65, 14.75, 13.85, 15.1, 14.35, 35.75, 15.3, 14.45, 14.25, 13.65, 14.45, 14.6, 13.95, 14.65, 14.5, 14.3, 14.85, 14.15, 15.0, 16.25, 14.25, 14.4, 14.15, 14.45, 14.25, 14.85, 14.3, 14.35, 15.35, 13.6, 14.3, 14.2, 14.3, 13.95]
plt.plot(xList, yList)
plt.xlabel('Number of Threads')
plt.ylabel('Execution Time (MS)')
plt.show()

Loading…
Cancel
Save