// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Random;

public class DiskPerf {
    public final static int NTHREADS = 10;
    public final static int NREADS = 10000;
    public final static int SIZ = 10000;
    public static void testOneSynch() throws IOException {
        FALSynchronized fa = new FALSynchronized("C:\\big.dat");
        Test[] tt = new Test[NTHREADS];
        for(int i = 0; i < tt.length; i++) {
            tt[i] = new Test(fa);
        }
        long t1 = System.currentTimeMillis();
        for(int i = 0; i < tt.length; i++) {
            tt[i].start();
        }
        for(int i = 0; i < tt.length; i++) {
            try {
                tt[i].join();
            } catch (InterruptedException e) {
                e.printStackTrace();
                System.exit(1);
            }
        }
        long t2 = System.currentTimeMillis();
        System.out.println("One synchronized RAF: " + (t2 - t1));
    }
    public static void testMultiple() throws IOException {
        Test[] tt = new Test[NTHREADS];
        for(int i = 0; i < tt.length; i++) {
            tt[i] = new Test(new FAL("C:\\big.dat"));
        }
        long t1 = System.currentTimeMillis();
        for(int i = 0; i < tt.length; i++) {
            tt[i].start();
        }
        for(int i = 0; i < tt.length; i++) {
            try {
                tt[i].join();
            } catch (InterruptedException e) {
                e.printStackTrace();
                System.exit(1);
            }
        }
        long t2 = System.currentTimeMillis();
        System.out.println("Multiple RAF: " + (t2 - t1));
    }
    public static void main(String[] args) throws IOException {
        testOneSynch();
        testMultiple();
        testOneSynch();
        testMultiple();
        testOneSynch();
        testMultiple();
    }
}

class Test extends Thread {
    private FAL fa;
    public Test(FAL fa) {
        this.fa = fa;
    }
    public void run()
    {
        try {
            Random rng = new Random(Thread.currentThread().getId());
            int len = (int)fa.length();
            for(int i = 0; i < DiskPerf.NREADS; i++) {
                fa.read(rng.nextInt(len - DiskPerf.SIZ), DiskPerf.SIZ);
            }
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(1);
        }
    }
}

class FAL {
    private RandomAccessFile raf;
    public FAL(String fnm) throws IOException {
        raf = new RandomAccessFile(fnm, "r");
    }
    public byte[] read(int ix, int n) throws IOException {
        raf.seek(ix);
        byte[] buf = new byte[n];
        raf.read(buf);
        return buf;
    }
    public long length() throws IOException {
        return raf.length();
    }
}

class FALSynchronized extends FAL {
    public FALSynchronized(String fnm) throws IOException {
        super(fnm);
    }
    public synchronized byte[] read(int ix, int n) throws IOException {
        return super.read(ix, n);
    }
}
