Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: DataOutputStream performance under OS X



Here's another test you can try. This code creates two threads; one simply
writes 1MB, 1 byte at a time, to a file; the second thread just continuously
checks the size of the file being written. Since the threads are
asynchronous I try to coordinate the times both threads are printing how
many bytes they have written/read.

The test actually runs two times, first with unbuffered IO, then with
Buffered IO. The first test should take longer on most platforms. (On my PC
it was about 10 times slower than the buffered test). During the unbuffered
test, you should (in theory) see the same (or very close) values for bytes
written and read, as the bytes are supposed to be going directly to the file
after each write.

The Buffered test uses a 2000 byte buffer and runs so quick on my machine
that 1MB is written in less time than it takes to reach the first reporting
"quanta." But I do not close the file right away. You'll see that the reader
keeps reporting the file is still 2K short of being finished (the exact size
of the buffer). When I finally close the file in the writer, the buffer is
flushed and the reader reports the full file size then quits.


Rob

---------------------------------------------------------------------




import java.io.*;

/**
* Created by IntelliJ IDEA.
* Rob Ross
* Date: Jul 30, 2003
* Time: 1:45:51 PM
*
*/
public class FileWritingTest
{
private static final String FILENAME = "test.txt";
private static long startTime; //for simple synchronization of thread
task start time
private static int timeIncrement = 1000; //we'll output status every
timeIncrement ms
private static int FILE_SIZE = 1000000; //number of bytes we write


public void startWriterAndReader(final boolean useBuffer)
{
Thread writer = new Thread(new Runnable()
{
public void run()
{
try
{
writeFile(useBuffer);
}
catch (IOException e)
{
e.printStackTrace(); //To change body of catch
statement use Options | File Templates.
}
catch (InterruptedException e)
{
e.printStackTrace(); //To change body of catch
statement use Options | File Templates.
}
}
});

Thread reader = new Thread(new Runnable()
{

public void run()
{
try
{
readFile();
}
catch (IOException e)
{
e.printStackTrace(); //To change body of catch
statement use Options | File Templates.
}

}
});
startTime = System.currentTimeMillis() + 2000; //gives us 2 seconds
to start both threads
writer.start();
reader.start();

//wait for both threads to finish before returning

try
{
reader.join();
writer.join();
}
catch (InterruptedException e)
{
e.printStackTrace(); //To change body of catch statement use
Options | File Templates.
}
}

public void readFile() throws IOException
{
long nextPrintTime = startTime + timeIncrement;
while (System.currentTimeMillis() < startTime)
{
//we start on our about the startTime
Thread.yield();
}
File file = new File(FILENAME);
while (!file.exists())
{
//the other thread may have not created the file yet
Thread.yield();
}
long fileSize;
RandomAccessFile raf = new RandomAccessFile(file, "r"); //open for
reading
fileSize = raf.length();
raf.close();
long now;
while (fileSize < FILE_SIZE)
{
now = System.currentTimeMillis();
if (now >= nextPrintTime)
{
System.out.println("READER: time=" + now / 10 + "s, FILE
SIZE: " + fileSize);
nextPrintTime += timeIncrement;
}
raf = new RandomAccessFile(file, "r"); //open for reading
fileSize = raf.length();
raf.close();

}
file.delete();
}

public void writeFile(boolean useBuffer) throws IOException,
InterruptedException
{
long nextPrintTime = startTime + timeIncrement;
File file = new File(FILENAME);
DataOutputStream dos;
if (useBuffer)
{
//buffer size = 2K
BufferedOutputStream bof = new BufferedOutputStream(new
FileOutputStream(file),2000);

dos = new DataOutputStream(bof);
}
else
{
dos = new DataOutputStream(new FileOutputStream(file));
}

while (System.currentTimeMillis() < startTime)
{
//we start on our about the startTime
Thread.yield();
}
int bytesWritten = 0;
long now = 0;
byte value = 0;
long writeStartTime = System.currentTimeMillis();
while (bytesWritten < FILE_SIZE)
{
now = System.currentTimeMillis();
if (now >= nextPrintTime)
{
System.out.println("WRITER: time=" + now / 10 + "s, BYTES
WRITTEN: " + bytesWritten);
nextPrintTime += timeIncrement;
Thread.yield();
}
dos.write(value);
bytesWritten++;

}
long finisedTime = System.currentTimeMillis();
if (useBuffer)
{
System.out.println("WRITER: FINISHED time=" +
System.currentTimeMillis() / 10 + "s");
//wait for 10 seconds before we close the file
Thread.sleep(10000);
}
System.out.println("Closing WRITER file at time=" +
System.currentTimeMillis());
dos.close();
System.out.println("WRITER closed at time=" +
System.currentTimeMillis());
System.out.println("Elapsed time to WRITE file:
"+(finisedTime-writeStartTime)+"ms");

}

public static void main(String[] args)
{
FileWritingTest frt = new FileWritingTest();
System.out.println("Starting unbuffered write test:");
frt.startWriterAndReader(false);//first time, no buffer
System.out.println("Starting buffered write test");
frt.startWriterAndReader(true);//second time, use buffer

}
}
_______________________________________________
java-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/java-dev
Be sure to read the FAQ http://developer.apple.com/java/faq/ before posting
Do not post admin requests to the list. They will be ignored.



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.