Using the following code as a benchmark, the system can write 10,000 rows to disk in a fraction of a second:
void withSync() { int f = open( "/tmp/t8" , O_RDWR | O_CREAT ); lseek (f, 0, SEEK_SET ); int records = 10*1000; clock_t ustart = clock(); for(int i = 0; i < records; i++) { write(f, "012345678901234567890123456789" , 30); fsync(f); } clock_t uend = clock(); close (f); printf(" sync() seconds:%lf writes per second:%lf\n", ((double)(uend-ustart))/(CLOCKS_PER_SEC), ((double)records)/((double)(uend-ustart))/(CLOCKS_PER_SEC)); } In the above code, 10,000 records can be written and flushed out to disk in a fraction of a second, output below:
sync() seconds:0.006268 writes per second:0.000002 In the Java version, it takes over 4 seconds to write 10,000 records. Is this just a limitation of Java, or am I missing something?
public void testFileChannel() throws IOException { RandomAccessFile raf = new RandomAccessFile(new File("/tmp/t5"),"rw"); FileChannel c = raf.getChannel(); c.force(true); ByteBuffer b = ByteBuffer.allocateDirect(64*1024); long s = System.currentTimeMillis(); for(int i=0;i<10000;i++){ b.clear(); b.put("012345678901234567890123456789".getBytes()); b.flip(); c.write(b); c.force(false); } long e=System.currentTimeMillis(); raf.close(); System.out.println("With flush "+(e-s)); } Returns this:
With flush 4263 Please help me understand what is the correct/fastest way to write records to disk in Java.
Note: I am using the RandomAccessFile class in combination with a ByteBuffer as ultimately we need random read/write access on this file.