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: Mac OS X File IO Performance Issue?



I'm trying to nail down a reasonable micro-benchmark we can use internally at Apple for tracking issues related to File IO Performance.
Note that this test requires junit.jar somewhere on your classpath. You can get it at www.junit.org
Here's approximately where I'm at just now.



Comments and suggestions welcome.


/** * Track performance numbers for reading / writing to a file. * **/

//package com.apple.java.junit.tests.performance;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Arrays;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;


public class FileIO extends TestCase {

    public static Test suite() {
        return new TestSuite(FileIO.class);
    }

    public static void main (String[] args) {
        junit.textui.TestRunner.run(suite());
    }

// Constants (twiddled to make the test take about 20 secs on a fast G5)
static int FILE_LOOPS = 51;
static int IO_LOOPS = 128;
static int SIZE = 1024 * 64;
static int DATA_MEGS = (SIZE * IO_LOOPS) / (1024 * 1024);


    // IO Buffers
    byte[]    w_buffer = new byte[SIZE];
    byte[]    r_buffer = new byte[SIZE];


// Cache several test times, log average and mean long starttime; long writetimes[] = new long[ FILE_LOOPS]; long readtimes[] = new long[ FILE_LOOPS];

    //
    // fill a buffer with data for later check
    //
    void doFill(int tag) {
        byte data = (byte) (tag % 16);
        Arrays.fill(w_buffer, data);
    }

    //
    // check that the file is intact (assuming doFill above was used)
    //

    void doCheck(int tag) {
        byte data = (byte) (tag % 16);
        for (int i = 0; i < w_buffer.length; i+=1) {

if ( r_buffer[i] != (byte) data) {
System.out.println("i " + i + " expected " + data + " actual " + r_buffer[i]);
}
assertTrue( r_buffer[i] == (byte) data);
}
}


//
// Loop, writing and then re-reading several test files withh data.
// Note that this loop doesn't check the contents in any meaningful way
//


    public void testRawSpeed() throws Exception {

// Repeatedly write out buffers to testfiles. Then re-read said buffers.
for (int j = 0; j < FILE_LOOPS; j+=1) {
File testfile = File.createTempFile("speedcheck", ".dat");
try {
// Check time to fill with data
starttime = System.currentTimeMillis();
FileOutputStream os = new FileOutputStream(testfile);
for( int i = 0; i < IO_LOOPS; i++) {
os.write( w_buffer);
}
os.flush();
writetimes[j] = System.currentTimeMillis() - starttime;



// Check time to pull all data starttime = System.currentTimeMillis(); FileInputStream is = new FileInputStream(testfile); while ( is.read( r_buffer) != -1) { // do nothing... } readtimes[j] = System.currentTimeMillis() - starttime; } finally { testfile.delete(); } } doRawSpeedReporting(); }


void doRawSpeedReporting() {
// Test is somewhat variable, so run on several files, reporting average and mean times.
Arrays.sort( writetimes );
Arrays.sort( readtimes );


long net = 0;
for (int j = 0; j < FILE_LOOPS; j+=1) {
net += writetimes[j];
// System.out.println( "time to write " + SIZE * IO_LOOPS + " was " + writetimes[j]);
}


System.out.println( "\n");
System.out.println( "FileOutputStream write " + DATA_MEGS + "M (avg) " + net / FILE_LOOPS);
System.out.println( "FileOutputStream write " + DATA_MEGS + "M (mean) " + writetimes[ (FILE_LOOPS/2) + 1] );


net = 0;
for (int j = 0; j < FILE_LOOPS; j+=1) {
net += readtimes[j];
// System.out.println( "time to read " + SIZE * IO_LOOPS + " was " + readtimes[j]);
}


System.out.println( "\n");
System.out.println( "FileInputStream read " + DATA_MEGS + "M (avg) " + net / FILE_LOOPS);
System.out.println( "FileInputStream read " + DATA_MEGS + "M (mean) " + readtimes[ (FILE_LOOPS/2) + 1] );
}



public void testCheckedSpeed() throws Exception {
// Repeatedly write out buffers to testfiles. Then re-read said buffers.
for (int j = 0; j < FILE_LOOPS; j+=1) {
File testfile = File.createTempFile("speedcheck", ".dat");


try {
// Check time to fill with data
starttime = System.currentTimeMillis();
FileOutputStream os = new FileOutputStream(testfile);
for( int i = 0; i < IO_LOOPS; i++) {
doFill(i); // <-- not free, time is included in test
os.write( w_buffer);
}
os.flush();
writetimes[j] = System.currentTimeMillis() - starttime;



// Check time to pull all data
starttime = System.currentTimeMillis();
FileInputStream is = new FileInputStream(testfile);
long netCount = 0;
int currCount = 0;
while ( (currCount = is.read( r_buffer)) != -1) {
netCount += currCount;
int i = (int) (netCount/SIZE) -1;
doCheck(i); // <-- not free, time is included in test
}
readtimes[j] = System.currentTimeMillis() - starttime;
}
finally {
testfile.delete();
}
}


        doCheckedSpeedReporting();
    }

void doCheckedSpeedReporting() {
// Test is somewhat variable, so run on several files, reporting average and mean times.
Arrays.sort( writetimes );
Arrays.sort( readtimes );


long net = 0;
for (int j = 0; j < FILE_LOOPS; j+=1) {
net += writetimes[j];
// System.out.println( "time to write " + SIZE * IO_LOOPS + " was " + writetimes[j]);
}


System.out.println( "\n");
System.out.println( "FileOutputStream fill and write " + DATA_MEGS + "K avg " + net / FILE_LOOPS);
System.out.println( "FileOutputStream fill and write " + DATA_MEGS + "K mean " + writetimes[ (FILE_LOOPS/2) + 1] );


net = 0;
for (int j = 0; j < FILE_LOOPS; j+=1) {
net += readtimes[j];
// System.out.println( "time to read " + SIZE * IO_LOOPS + " was " + readtimes[j]);
}


System.out.println( "\n");
System.out.println( "FileInputStream read and check " + DATA_MEGS + "K avg " + net / FILE_LOOPS);
System.out.println( "FileInputStream read and check " + DATA_MEGS + "K mean " + readtimes[ (FILE_LOOPS/2) + 1] );
}


}

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Java-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/java-dev/email@hidden

This email sent to email@hidden
References: 
 >RE: Mac OS X File IO Performance Issue? (From: "Rick Genter" <email@hidden>)
 >Re: Mac OS X File IO Performance Issue? (From: Brendon McLean <email@hidden>)



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.