import java.io.*; //import library.io.*; //import java.io.IOException; public class TestRead { private static File file; // File private static int fileSize; // file size, bytes private static int blockSize; // file size, bytes private static byte[] buffer; // a byte buffer to read data into private static char[] charBuffer; // a char buffer to read data into private static String fileName = "test_read.txt"; private static int round; private static float[] results; private static int step; private static RandomAccessFile rf; public static void main(String[] arg){ fileSize = Integer.parseInt(arg[0]); round = Integer.parseInt(arg[1]); charBuffer = new char[fileSize]; buffer = new byte[fileSize]; step = Integer.parseInt(arg[2]); results = new float[step]; try{ rf = new RandomAccessFile("test_read_data", "rw"); file = new File(fileName); boolean writeFile = false; if ( ! file.exists() ) writeFile = true; if ( writeFile ) { boolean nBuff = false; if ( buffer == null ) { nBuff = true; buffer = new byte[ fileSize ]; } FileOutputStream fo = new FileOutputStream(file); fo.write( buffer ); fo.close(); } TestRead o = new TestRead(); o.test(); o.cleanup(); rf.close(); } catch ( IOException e ) { System.out.println("Create file error\n"); } } /* * calculates the overhead time */ public long overhead(int blockSize) { int size = 0, n; long start = 0, end = 0; if ( buffer != null ) { start = System.currentTimeMillis(); while ( size < fileSize ) { n = dummy( buffer, size, blockSize ); if ( n < blockSize ) break; size += n; } end = System.currentTimeMillis(); } return (start - end); } public int dummy( byte [] buffer, int size, int blockSize ) { return blockSize; } public void test() { try { int n = 0; // file input stream int m = 0; while(m++ < step){ blockSize = 32*(int)(Math.pow(2.0,m)); float avg1 = 0, avg2 = 0; long overhead_block = 0;//overhead(blockSize); for(int i = 0; i< round ; i++){ long start1, start2, end; start1 = System.currentTimeMillis(); FileInputStream f = new FileInputStream(file); int size = 0; start2 = System.currentTimeMillis(); long total_overhead = 0; n = 0; while (size <= fileSize) { n = f.read(buffer,size,blockSize); if ( n < blockSize ) break; size += n; total_overhead += overhead_block ; if(size + blockSize >= fileSize) break; } end = System.currentTimeMillis(); f.close(); // including open time avg1 += ((end - start1) - total_overhead); avg2 += ((end - start2) - total_overhead); } avg2 /= round; avg1 /= round; rf.writeBytes("block_size= " + blockSize + " , " + avg1+ " , " + avg2+"\n"); } }catch(Exception e){ e.printStackTrace(); } } public void cleanup() { file.delete(); } }