Skip to content

Instantly share code, notes, and snippets.

@andreyvit
Created August 28, 2009 12:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreyvit/176930 to your computer and use it in GitHub Desktop.
Save andreyvit/176930 to your computer and use it in GitHub Desktop.
package com.instantiations.eclipse.analisys.indexer.storage.diskmapped;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import com.instantiations.eclipse.analisys.indexer.storage.db.PagedMemoryException;
import com.instantiations.eclipse.analisys.indexer.storage.reusable.filesystem.FileObject;
import com.instantiations.eclipse.analisys.indexer.storage.reusable.filesystem.FileSystem;
import com.instantiations.eclipse.analisys.indexer.storage.reusable.filesystem.DiskFileSystem;
import com.instantiations.eclipse.analisys.indexer.storage.reusable.filesystem.NioFileSystem;
import com.instantiations.eclipse.analisys.indexer.storage.reusable.filesystem.MemoryMappedFileSystem;
import com.instantiations.eclipse.analisys.indexer.storage.reusable.filesystem.InMemoryFileSystem;
public class SpeedTest {
public static void main(String[] args) throws Exception {
File file = new File("/tmp/test.foo");
int fileSize = 1024 * 1024 * 100;
int readAttemps = 1000000;
int readSize = 1024;
Random random = new Random();
byte[] buf = new byte[readSize];
FileSystem[] fileSystems = new FileSystem[] {
InMemoryFileSystem.getInstance(),
MemoryMappedFileSystem.getInstance(),
DiskFileSystem.getInstance(),
NioFileSystem.getInstance(),
};
for (int i = 0; i < fileSystems.length; i++) {
FileSystem fileSystem = fileSystems[i];
if (!fileSystem.exists(file.getPath()))
createFile(fileSystem, fileSize, file, random);
FileObject fileObject = fileSystem.openFileObject(file.getPath(), "rw");
System.out.print("Running read test with " + fileSystem.getClass().getName() + "... ");
long start = System.currentTimeMillis();
for (int attemp = 0;attemp < readAttemps; attemp++) {
long offset = random.nextInt(fileSize - readSize);
fileObject.seek(offset);
fileObject.readFully(buf, 0, buf.length);
}
fileObject.close();
long end = System.currentTimeMillis();
System.out.println((end - start) + " ms.");
}
}
private static void createFile(FileSystem fileSystem, int fileSize, File file, Random random)
throws FileNotFoundException, IOException, PagedMemoryException {
OutputStream os = fileSystem.openFileOutputStream(file.getPath(), false);
byte[] buf = new byte[1024];
random.nextBytes(buf);
int rows = fileSize/buf.length;
for (int i = 0; i < rows; i++)
os.write(buf);
os.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment