public void test_001() throws IOException {
  final int pageSize=8 * KiloByte;
  System.err.println("pageSize=" + nf.format(pageSize) + " bytes");
  final int diskCacheSize=8 * MegaByte;
  System.err.println("diskCacheSize=" + nf.format(diskCacheSize) + " bytes");
  final int pagesInDiskCache=diskCacheSize / pageSize;
  System.err.println("pagesInDiskCache=" + nf.format(pagesInDiskCache));
  final int pagesToWrite=10 * pagesInDiskCache;
  System.err.println("pagesToWrite=" + nf.format(pagesToWrite));
  final int maxPages=pagesToWrite * 10;
  System.err.println("maxPages=" + nf.format(maxPages) + ", maxLength="+ nf.format(((long)maxPages * pageSize)));
  final boolean writeThrough=true;
  final boolean synchAfterTest=true;
  final Random r=new Random();
  final File file=File.createTempFile("test",".dbCache");
  System.err.println("file=" + file);
  final RandomAccessFile raf=new RandomAccessFile(file,(writeThrough ? "rwd" : "rw"));
  try {
    final FileChannel fileChannel=raf.getChannel();
    final ByteBuffer buf=ByteBuffer.allocateDirect(pageSize);
    assert buf.limit() == buf.capacity();
    long maxOffset=(long)maxPages * pageSize - 1;
    buf.limit(1);
    fileChannel.write(buf,maxOffset);
    buf.limit(buf.capacity());
    long startNanos=System.nanoTime();
    for (int i=0; i < pagesToWrite; i++) {
      final long pos=r.nextInt(maxPages) * (long)pageSize;
      assert pos <= maxOffset;
      buf.position(0);
      final int nwritten;
      try {
        nwritten=fileChannel.write(buf,pos);
      }
 catch (      IllegalArgumentException ex) {
        System.err.println("pos=" + pos);
        throw ex;
      }
      assertEquals("iteration=" + i + ", nwritten",pageSize,nwritten);
    }
    if (synchAfterTest) {
      fileChannel.force(false);
    }
    final long endNanos=System.nanoTime();
    final long elapsedNanos=endNanos - startNanos;
    System.err.println("Wrote " + nf.format(pagesToWrite) + " pages of "+ pageSize+ " bytes in "+ TimeUnit.NANOSECONDS.toSeconds(elapsedNanos)+ " secs");
    System.err.println("" + nf.format(getUnitsPerSecond(pagesToWrite,elapsedNanos)) + " pages per second");
    final long megabytesWritten=pagesToWrite * pageSize / MegaByte;
    System.err.println("" + fpf.format(megabytesWritten) + " megabytes written");
    System.err.println("" + fpf.format(getUnitsPerSecond(megabytesWritten,elapsedNanos)) + " megabytes per second");
    System.err.println("" + fpf.format(getUnitsPerSecond(megabytesWritten * 8,elapsedNanos)) + " megabits per second");
    System.err.println("writeThroughIOs=" + writeThrough);
    System.err.println("synchAfterTest=" + synchAfterTest);
    System.err.println("bytes on disk: " + nf.format(raf.length()));
  }
  finally {
    raf.close();
    if (!file.delete()) {
      throw new RuntimeException("Could not delete file: " + file);
    }
    if (log.isInfoEnabled())     log.info("deleted: " + file);
  }
}

public void test_001 ( ) throws IOException { final int pageSize = 8 * KiloByte ; System . err . println ( "pageSize=" + nf . format ( pageSize ) + " bytes" ) ; final int diskCacheSize = 8 * MegaByte ; System . err . println ( "diskCacheSize=" + nf . format ( diskCacheSize ) + " bytes" ) ; final int pagesInDiskCache = diskCacheSize / pageSize ; System . err . println ( "pagesInDiskCache=" + nf . format ( pagesInDiskCache ) ) ; final int pagesToWrite = 10 * pagesInDiskCache ; System . err . println ( "pagesToWrite=" + nf . format ( pagesToWrite ) ) ; final int maxPages = pagesToWrite * 10 ; System . err . println ( "maxPages=" + nf . format ( maxPages ) + ", maxLength=" + nf . format ( ( ( long ) maxPages * pageSize ) ) ) ; final boolean writeThrough = true ; final boolean synchAfterTest = true ; final Random r = new Random ( ) ; final File file = File . createTempFile ( "test" , ".dbCache" ) ; System . err . println ( "file=" + file ) ; final RandomAccessFile raf = new RandomAccessFile ( file , ( writeThrough ? "rwd" : "rw" ) ) ; try { final FileChannel fileChannel = raf . getChannel ( ) ; final ByteBuffer buf = ByteBuffer . allocateDirect ( pageSize ) ; assert buf . limit ( ) == buf . capacity ( ) ; long maxOffset = ( long ) maxPages * pageSize - 1 ; buf . limit ( 1 ) ; fileChannel . write ( buf , maxOffset ) ; buf . limit ( buf . capacity ( ) ) ; long startNanos = System . nanoTime ( ) ; for ( int i = 0 ; i < pagesToWrite ; i ++ ) { final long pos = r . nextInt ( maxPages ) * ( long ) pageSize ; assert pos <= maxOffset ; buf . position ( 0 ) ; final int nwritten ; try { nwritten = fileChannel . write ( buf , pos ) ; } catch ( IllegalArgumentException ex ) { System . err . println ( "pos=" + pos ) ; throw ex ; } assertEquals ( "iteration=" + i + ", nwritten" , pageSize , nwritten ) ; } if ( synchAfterTest ) { fileChannel . force ( false ) ; } final long endNanos = System . nanoTime ( ) ; final long elapsedNanos = endNanos - startNanos ; System . err . println ( "Wrote " + nf . format ( pagesToWrite ) + " pages of " + pageSize + " bytes in " + TimeUnit . NANOSECONDS . toSeconds ( elapsedNanos ) + " secs" ) ; System . err . println ( "" + nf . format ( getUnitsPerSecond ( pagesToWrite , elapsedNanos ) ) + " pages per second" ) ; final long megabytesWritten = pagesToWrite * pageSize / MegaByte ; System . err . println ( "" + fpf . format ( megabytesWritten ) + " megabytes written" ) ; System . err . println ( "" + fpf . format ( getUnitsPerSecond ( megabytesWritten , elapsedNanos ) ) + " megabytes per second" ) ; System . err . println ( "" + fpf . format ( getUnitsPerSecond ( megabytesWritten * 8 , elapsedNanos ) ) + " megabits per second" ) ; System . err . println ( "writeThroughIOs=" + writeThrough ) ; System . err . println ( "synchAfterTest=" + synchAfterTest ) ; System . err . println ( "bytes on disk: " + nf . format ( raf . length ( ) ) ) ; } finally { raf . close ( ) ; if ( ! file . delete ( ) ) { throw new RuntimeException ( "Could not delete file: " + file ) ; } if ( log . isInfoEnabled ( ) ) log . info ( "deleted: " + file ) ; } }
test of raw io performance for random writes .
