Application: The Impact of I/O on Application Performance In previous assignment
ID: 3812779 • Letter: A
Question
Application: The Impact of I/O on Application Performance
In previous assignments, you used threads to improve the performance of a sort algorithm. Threads were initially applied in order to achieve processing concurrency in order to reduce the time required to sort data. This week’s readings highlighted various ways that I/O performance can impact the overall performance of an application or a system. The readings also identified specific principles that can be applied to improve the efficiency of I/O.
For this Assignment, you will consider the I/O performed in the threaded sort Assignment from Week 2 and how the I/O contributes to the performance of the threaded sort. Applying the principles identified in this week’s reading, along with the concurrency control mechanisms from Week 3, you will revise the threaded sort application in an effort to improve the I/O performance in order to affect an overall performance improvement.
To prepare:
Evaluate the manner by which the threaded sort application performs I/O to retrieve the data to be sorted.
Propose a strategy to improve the performance by applying one or more of the principles to improve the efficiency of I/O that were identified in this week’s reading.
By Day 7, implement your strategy by modifying your solution to the threaded sort Assignment from Week 2.
In addition, write a 2- to 3-page paper that evaluates how I/O performance impacts overall program performance. Make sure to include the following:
A description of your I/O strategy
An explanation on how you expected your strategy to improve performance
A summary of the actual change in performance observed when running the updated threaded sort Submit a zip archive of your NetBeans project, implementing your strategy, to the Assignment Part 1 -
Week 5 submission link and your 2- to 3-page paper to the Assignment Turnitin Part 2 - Week 5 submission link.
Source Code Below
***MegaSort.java***
public class MergeSort {
// The mergeSort method returns a sorted copy of the
// String objects contained in the String array data.
/**
* Sorts the String objects using the merge sort algorithm.
*
* @param data the String objects to be sorted
* @return the String objects sorted in ascending order
*/
public static String[] mergeSort(String[] data) {
if (data.length > 1) {
String[] left = new String[data.length / 2];
String[] right = new String[data.length - left.length];
System.arraycopy(data, 0, left, 0, left.length);
System.arraycopy(data, left.length, right, 0, right.length);
left = mergeSort(left);
right = mergeSort(right);
return merge(left, right);
}
else {
return data;
}
}
/**
* The merge method accepts two String arrays that are assumed
* to be sorted in ascending order. The method will return a
* sorted array of String objects containing all String objects
* from the two input collections.
*
* @param left a sorted collection of String objects
* @param right a sorted collection of String objects
* @return a sorted collection of String objects
*/
public static String[] merge(String[] left, String[] right) {
String[] data = new String[left.length + right.length];
int lIndex = 0;
int rIndex = 0;
for (int i=0; i<data.length; i++) {
if (lIndex == left.length) {
data[i] = right[rIndex];
rIndex++;
}
else if (rIndex == right.length) {
data[i] = left[lIndex];
lIndex++;
}
else if (left[lIndex].compareTo(right[rIndex]) < 0) {
data[i] = left[lIndex];
lIndex++;
}
else {
data[i] = right[rIndex];
rIndex++;
}
}
return data;
}
}
***Sort.java***
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class Sort {
/**
* You are to implement this method. The method should invoke one or
* more threads to read and sort the data from the collection of Files.
* The method should return a sorted list of all of the String data
* contained in the files.
*
* @param files
* @return
* @throws IOException
*/
public static String[] threadedSort(File[] files) throws IOException {
String sortedData[] = new String[0]; // To be returned from this method
int counter = 0;
boolean allThreadsTerminated = false;
// Create array of SortingThread with length same as number of files
SortingThread[] threadList = new SortingThread[files.length];
// Instantiate each thread and pass the Data to it, start each of them in parallel
for (File file : files) {
String[] data = getData(file);
threadList[counter] = new SortingThread(data);
threadList[counter].start();
counter++;
}
// Wait until all threads are not terminated.
// You must keep the data preserved until all threads are completed.
while(!allThreadsTerminated) {
allThreadsTerminated = true;
for(counter=0; counter<files.length; counter++) {
if(threadList[counter].getState() != Thread.State.TERMINATED) {
allThreadsTerminated = false;
}
}
}
// data in all threads is sorted now. start merging them
for(counter=0; counter<files.length; counter++) {
sortedData = MergeSort.merge(sortedData, threadList[counter].data);
}
//return sorted Data
return sortedData;
}
/**
* Given an array of files, this method will return a sorted
* list of the String data contained in each of the files.
*
* @param files the files to be read
* @return the sorted data
* @throws IOException thrown if any errors occur reading the file
*/
public static String[] sort(File[] files) throws IOException {
String[] sortedData = new String[0];
for (File file : files) {
String[] data = getData(file);
data = MergeSort.mergeSort(data);
sortedData = MergeSort.merge(sortedData, data);
}
return sortedData;
}
/**
* This method will read in the string data from the specified
* file and return the data as an array of String objects.
*
* @param file the file containing the String data
* @return String array containing the String data
* @throws IOException thrown if any errors occur reading the file
*/
private static String[] getData(File file) throws IOException {
ArrayList<String> data = new ArrayList<String>();
BufferedReader in = new BufferedReader(new FileReader(file));
// Read the data from the file until the end of file is reached
while (true) {
String line = in.readLine();
if (line == null) {
// the end of file was reached
break;
}
else {
data.add(line);
}
}
//Close the input stream and return the data
in.close();
return data.toArray(new String[0]);
}
}
***SortingThread.java***
import java.io.File;
import java.io.IOException;
/**
* This class extends thread and is used for performing the sorting in separate thread.
*
* @author
*
*/
public class SortingThread extends Thread {
String[] data;
/**
* Constructor to get the files list
* @param files
*/
SortingThread(String[] data) {
this.data = data;
}
/**
* Thread run method
*/
public void run() {
data = MergeSort.mergeSort(data);
}
}
***SortTest.Java***
import java.io.File;
import java.io.IOException;
/**
* The class SortTest is used to test the threaded and non-threaded
* sort methods. This program will call each method to sort the data
* contained in the four test files. This program will then test the
* results to ensure that the results are sorted in ascending order.
*
* Simply run this program to verify that you have correctly implemented
* the threaded sort method. The program will not verify if your sort
* uses threads, but will verify if your implementation correctly
* sorted the data contained in the four files.
*
* There should be no reason to make modifications to this class.
*/
public class SortTest {
public static void main(String[] args) throws IOException {
File[] files = {new File("enable1.txt"), new File("enable2k.txt"), new File("lower.txt"), new File("mixed.txt")};
// Run Sort.sort on the files
long startTime = System.nanoTime();
String[] sortedData = Sort.sort(files);
long stopTime = System.nanoTime();
double elapsedTime = (stopTime - startTime) / 1000000000.0;
// Test to ensure the data is sorted
for (int i=0; i<sortedData.length-1; i++) {
if (sortedData[i].compareTo(sortedData[i+1]) > 0) {
System.out.println("The data returned by Sort.sort is not sorted.");
throw new java.lang.IllegalStateException("The data returned by Sort.sort is not sorted");
}
}
System.out.println("The data returned by Sort.sort is sorted.");
System.out.println("Sort.sort took " + elapsedTime + " seconds to read and sort the data.");
// Run Sort.threadedSort on the files and test to ensure the data is sorted
startTime = System.nanoTime();
String[] threadSortedData = Sort.threadedSort(files);
stopTime = System.nanoTime();
double threadedElapsedTime = (stopTime - startTime)/ 1000000000.0;
// Test to ensure the data is sorted
if (sortedData.length != threadSortedData.length) {
System.out.println("The data return by Sort.threadedSort is missing data");
throw new java.lang.IllegalStateException("The data returned by Sort.threadedSort is not sorted");
}
for (int i=0; i<threadSortedData.length-1; i++) {
if (threadSortedData[i].compareTo(threadSortedData[i+1]) > 0) {
System.out.println("The data return by Sort.threadedSort is not sorted");
throw new java.lang.IllegalStateException("The data returned by Sort.threadedSort is not sorted");
}
}
System.out.println("The data returned by Sort.threadedSort is sorted.");
System.out.println("Sort.threadedSort took " + threadedElapsedTime + " seconds to read and sort the data.");
}
}
Explanation / Answer
In the above program,we have used multiple threads to sort the file data.So our multithreaded application takes very less time compared to the non-threaded application.
In multithreading application, the task is divided among all the threads and each thread is independent of other thread, so that they can perform the operation independently and each of the threads run parallely.
In non-threaded code, only one thread is responsible to read the data from the file and then sort the data. All the operations are performed in the sequential manner.
The next step of operation is to wait until the current operation is performed.
In our code, each thread is given the file to sort its file data while the main thread to wait for all the thread until all the threads finished their respective operation.
When all the threads finish their job then the main thread combine all their respective sorted data and sort the combined result and return the main sorted data.
The non-threaded application takes 1.740986572 seconds to sort the file data.
While the threaded code takes only 1.139259012 seconds to read and sort the file data.
Hence we can say that multithreaded code is more efficient than the non-threaded code in the utilization of the resources and in terms of the time complexity.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.