import java.util.Random;
import java.io.*;
import nu.dll.fia.*;

class ObjTest {

    static int MAX = 10;
    static long start = System.currentTimeMillis();
    static Random random = new Random();
    static Integer[] data = new Integer[0];

    static OutputStream dummyOut = null;
    static ObjectOutputStream objOut = null;
    static SpyOutputStream spyOut = null;
    static {

    }
    static void initData() {
	print("(init)");
	//int size = random.nextInt(1000000)+500000;
	//MAX = size;
	data = new Integer[MAX];
	for (int i=0; i < MAX; i++) {
	    data[i] = new Integer(i);
	}

	try {
	    dummyOut = new DummyOutputStream();
	    spyOut = new SpyOutputStream(dummyOut);
	    objOut = new ObjectOutputStream(spyOut);
	    print("init: wrote " + spyOut.getCountOff());
	} catch (IOException ex1) {
	    throw new InternalError();
	}

	print("(init done (" + MAX + "))");
    }

    static void clearData() {
	print("(clear)");
	data = new Integer[0];
	print("(clear done)");
    }


    public static void main(String[] argv) throws Exception {
	int k=0;
	int maxIterations = 3;
	while (k < 5) {
	    k++;
	    int c=0;
	    clearData();
	    System.gc();
	    Thread.sleep(500);
	    initData();
	    while (c < maxIterations) {
		c++;
		print("Start.");
		print("array referencing");
		for (int i=0; i < MAX; i++) {
		    dummy(data[i]);
		}
		print("wrote " + spyOut.getCountOff() + " bytes");
		print("array referencing done");
		print("object creation");
		for (int i=0; i < MAX; i++) {
		    dummy(new Integer(i));
		}
		print("wrote " + spyOut.getCountOff() + " bytes");
		print("object creation done");
		
		print("End.");
	    }
	}
    }

    static long lastp = 0;
    static void print(String s) {
	long now = System.currentTimeMillis();
	long nowp = now-start;
	System.out.println((nowp) + "\t" + 
			   (nowp-lastp) + "\t" + s);
	lastp = now-start;
    }

    static boolean dummy(Object i) throws IOException {
	objOut.writeObject(i);
	return true;
    }
}
