import java.io.*;
import java.util.Random;

public class SerializationThroughput {

	static Random rand = new Random();

	static public void main(String[] args) throws Exception {
		System.out.println("record size\trecords written\tbytes per record\trecords per sec\tintegers per sec");

		for (int n=1; n<50000; n=(int)(1.2*n)+1) {
			File file = new File ("tmp.tmp");
			FileOutputStream fos = new FileOutputStream(file);
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			Thread.sleep(1000); //Wait for any disk activity to stop.
			Integer d[] = new Integer[n];
			for (int k=0; k<n; k++) {
				d[k] = new Integer((int)(rand.nextDouble()*k));
			}

			start();
			int max = 1000;
			int i = 0;
			while (i<max && elapsed()<10) {
				oos.writeObject(d);
				oos.reset();
				oos.flush();
				fos.getFD().sync(); //Forces flushing to disk. :)
				i++;
			}

			double t=elapsed();
			long f = file.length();
			System.out.println(
				n +"\t"+
				i + "\t"+
				f / i +"\t"+
				i / t +"\t"+
				n * i / t);
		}
	}

	static long t0;
	static void start() {t0 = System.currentTimeMillis();}
	static double elapsed () {return (System.currentTimeMillis() - t0)/1000.0;}

}
