import java.io.RandomAccessFile; import java.io.IOException; import java.nio.channels.FileChannel; import java.nio.channels.Channel; import java.nio.channels.WritableByteChannel; import java.nio.ByteBuffer; import java.lang.reflect.Constructor; import java.util.Random; import java.util.LinkedList; import java.util.ArrayList; /** * @author yonik * @version $Id$ */ public class FileReadTest { static int poolsize=2; public static void main(String[] args) throws Exception { int argpos=0; final String filename = args[argpos++]; final String implementation = args[argpos++]; final boolean serial = args[argpos++].toLowerCase().equals("true"); final int nThreads = Integer.parseInt(args[argpos++]); final int iterations = Integer.parseInt(args[argpos++]); final int bufsize=argpos=nPages) pg-=nPages; long pos = (serial ? pg : rn) * bufsize; try { val += reader.read(pos); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException(e); } } } synchronized (answer) { answer[0]+=val; } times[seed] = System.currentTimeMillis() - start; } }; } for (int i=0;i>1]; } }; } } class PooledPread extends MyFile { FileChannel[] all; int[] useCount; public PooledPread(RandomAccessFile f, String filename) throws Exception { super(f); all = new FileChannel[FileReadTest.poolsize]; all[0]=f.getChannel(); for (int i=1; i 1) { synchronized(PooledPread.this) { int minCount = useCount[0]; for (int i=1; i 1) { synchronized(PooledPread.this) { useCount[idx]--; } } return b[0]+b[len>>1]; } }; } } class ChannelFile extends MyFile { final FileChannel channel; public ChannelFile(RandomAccessFile f, String filename) { super(f); channel = f.getChannel(); } MyReader getReader() { return new MyReader() { byte[] b = new byte[BUFFER_SIZE]; ByteBuffer bb = ByteBuffer.wrap(b); public int read(long pos) throws IOException { bb.clear(); int len; synchronized(channel) { if (filepos != pos) channel.position(pos); len = channel.read(bb); filepos = pos + len; } return b[0]+b[len>>1]; } }; } } class ChannelPread extends MyFile { final FileChannel channel; public ChannelPread(RandomAccessFile f, String filename) { super(f); channel = f.getChannel(); } MyReader getReader() { return new MyReader() { byte[] b = new byte[BUFFER_SIZE]; ByteBuffer bb = ByteBuffer.wrap(b); public int read(long pos) throws IOException { bb.clear(); int len = channel.read(bb, pos); return b[0]+b[len>>1]; } }; } } class ChannelPreadDirect extends MyFile { final FileChannel channel; public ChannelPreadDirect(RandomAccessFile f, String filename) { super(f); channel = f.getChannel(); } MyReader getReader() { return new MyReader() { ByteBuffer bb = ByteBuffer.allocateDirect(BUFFER_SIZE); public int read(long pos) throws IOException { bb.clear(); int len = channel.read(bb, pos); return bb.get(0)+bb.get(len>>1); } }; } } class ChannelTransfer extends MyFile { final FileChannel channel; public ChannelTransfer(RandomAccessFile f, String filename) { super(f); channel = f.getChannel(); } MyReader getReader() { return new MyReader() { final byte[] b = new byte[BUFFER_SIZE]; int posInBuffer=0; final ByteBuffer bb = ByteBuffer.wrap(b); final WritableByteChannel sink = new WritableByteChannel() { public int write(ByteBuffer src) throws IOException { int remaining = src.remaining(); src.get(b,posInBuffer,remaining); // may be called multiple times, so we need to keep track posInBuffer += remaining; return remaining; } public boolean isOpen() {return true;} public void close() throws IOException {} }; public int read(long pos) throws IOException { posInBuffer=0; int len = (int)channel.transferTo(pos, BUFFER_SIZE, sink); return b[0]+b[len>>1]; } }; } }