Jeg prøvede lige at lave et lidt mere omfattende test program:
package test;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class ReadTest {
final static String FILE_NAME = "
C://readtest.dat"; public static void main(String[] args) throws Exception {
createFile();
(new UnbufferedSmallReadTest()).doTest();
(new UnbufferedSmallReadTest()).doTest();
(new UnbufferedSmallReadTest()).doTest();
(new UnbufferedLargeReadTest()).doTest();
(new UnbufferedLargeReadTest()).doTest();
(new UnbufferedLargeReadTest()).doTest();
(new BufferedSmallReadTest()).doTest();
(new BufferedSmallReadTest()).doTest();
(new BufferedSmallReadTest()).doTest();
(new BufferedLargeReadTest()).doTest();
(new BufferedLargeReadTest()).doTest();
(new BufferedLargeReadTest()).doTest();
(new NioSmallReadTest()).doTest();
(new NioSmallReadTest()).doTest();
(new NioSmallReadTest()).doTest();
(new NioLargeReadTest()).doTest();
(new NioLargeReadTest()).doTest();
(new NioLargeReadTest()).doTest();
}
private static void createFile() {
try {
OutputStream f = new FileOutputStream(FILE_NAME);
for(int i = 0; i < 10000000; i++) {
f.write((i % 256));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
abstract class GenericReadTest {
GenericReadTest() {
}
void doTest() throws Exception {
long t1 = System.currentTimeMillis();
readAndCheck();
long t2 = System.currentTimeMillis();
System.out.println(getType() + " : " + (t2 - t1));
}
abstract void readAndCheck() throws Exception;
abstract String getType();
}
abstract class UnbufferedReadTest extends GenericReadTest {
void readAndCheck() throws Exception {
try {
InputStream f = new FileInputStream(ReadTest.FILE_NAME);
byte[] b = new byte[getReadSize()];
int n;
int ix = 0;
while((n = f.read(b)) >= 0) {
for(int i = 0; i < n; i++) {
if(b[i] != (byte)(ix % 256)) {
throw new Exception("Bad data read - offset " + ix + " expected " + (byte)(ix % 256) + " found " + b[i]);
}
ix++;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
abstract int getReadSize();
}
abstract class BufferedReadTest extends GenericReadTest {
void readAndCheck() throws Exception {
try {
BufferedInputStream f = new BufferedInputStream(new FileInputStream(ReadTest.FILE_NAME));
byte[] b = new byte[getReadSize()];
int n;
int ix = 0;
while((n = f.read(b)) >= 0) {
for(int i = 0; i < n; i++) {
if(b[i] != (byte)(ix % 256)) {
throw new Exception("Bad data read - offset " + ix + " expected " + (byte)(ix % 256) + " found " + b[i]);
}
ix++;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
abstract int getReadSize();
}
abstract class NioReadTest extends GenericReadTest {
void readAndCheck() throws Exception {
try {
FileChannel f2 = (new FileInputStream(ReadTest.FILE_NAME)).getChannel();
ByteBuffer f = f2.map(FileChannel.MapMode.READ_ONLY, 0, 10000000);
byte[] b = new byte[getReadSize()];
int ix = 0;
while(ix < 10000000) {
f.get(b);
for(int i = 0; i < b.length; i++) {
if(b[i] != (byte)(ix % 256)) {
throw new Exception("Bad data read - offset " + ix + " expected " + (byte)(ix % 256) + " found " + b[i]);
}
ix++;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
abstract int getReadSize();
}
class UnbufferedSmallReadTest extends UnbufferedReadTest {
int getReadSize() {
return 10;
}
String getType() {
return "Unbuffered small reads";
}
}
class UnbufferedLargeReadTest extends UnbufferedReadTest {
int getReadSize() {
return 1000000;
}
String getType() {
return "Unbuffered large reads";
}
}
class BufferedSmallReadTest extends BufferedReadTest {
int getReadSize() {
return 10;
}
String getType() {
return "Buffered small reads";
}
}
class BufferedLargeReadTest extends BufferedReadTest {
int getReadSize() {
return 1000000;
}
String getType() {
return "Buffered large reads";
}
}
class NioSmallReadTest extends NioReadTest {
int getReadSize() {
return 10;
}
String getType() {
return "Nio small reads";
}
}
class NioLargeReadTest extends NioReadTest {
int getReadSize() {
return 1000000;
}
String getType() {
return "Nio large reads";
}
}