Skip to content

Instantly share code, notes, and snippets.

@toddlipcon
Created July 20, 2011 18:49
Show Gist options
  • Select an option

  • Save toddlipcon/1095630 to your computer and use it in GitHub Desktop.

Select an option

Save toddlipcon/1095630 to your computer and use it in GitHub Desktop.
import org.apache.hadoop.fs.*;
import org.apache.hadoop.conf.Configuration;
import java.util.Random;
import java.io.IOException;
public class RandomIO {
private static class Reader extends Thread {
Path path;
FileSystem fs;
volatile boolean done = false;
int count = 0;
public Reader(FileSystem fs, Path p) {
this.fs = fs;
this.path = p;
}
public void run() {
try {
internalRun();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private void internalRun() throws Exception {
long st = System.currentTimeMillis();
long len = fs.getLength(path);
FSDataInputStream dis = fs.open(path);
Random r = new Random();
System.out.println("Took " + (System.currentTimeMillis() - st) + " ms to open");
byte buf[] = new byte[64*1024];
while (!done) {
long pos = r.nextInt((int)(len - buf.length));
dis.readFully(pos, buf, 0, buf.length);
count++;
}
System.out.println("Read " + count);
dis.close();
}
}
public static void main(String args[]) throws Exception {
String file = args[0];
Configuration conf = new Configuration();
conf.setInt("io.file.buffer.size", 64*1024);
conf.setInt("dfs.client.socketcache.capacity", 20);
Path path = new Path(file);
FileSystem fs = path.getFileSystem(conf);
Reader r[] = new Reader[16];
for (int i = 0; i < r.length; i++) {
r[i] = new Reader(fs, path);
r[i].start();
}
Thread.sleep(20000);
for (Reader rdr : r) {
rdr.done = true;
}
for (Reader rdr : r) {
rdr.join();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment