| 1 | import java.io.*;
|
|---|
| 2 | import java.util.*;
|
|---|
| 3 |
|
|---|
| 4 | class SerTest implements Serializable {
|
|---|
| 5 | SerTest[] ref;
|
|---|
| 6 |
|
|---|
| 7 | public static void main(String[] args) throws Exception {
|
|---|
| 8 | int objCount = Integer.parseInt(args[0]);
|
|---|
| 9 | int refsPerObj = Integer.parseInt(args[1]);
|
|---|
| 10 | long randomSeed = Long.parseLong(args[2]);
|
|---|
| 11 |
|
|---|
| 12 | Random r = new Random(randomSeed);
|
|---|
| 13 | File file = new File("test.ser");
|
|---|
| 14 |
|
|---|
| 15 | //Create an array of objects that refer to random other objects and write it to a file
|
|---|
| 16 | write(create(objCount, refsPerObj, r), file);
|
|---|
| 17 |
|
|---|
| 18 | //Read the array back from the file
|
|---|
| 19 | Object o = read(file);
|
|---|
| 20 |
|
|---|
| 21 | //Calculate checksum to ensure that the JVM has not taken any shortcuts
|
|---|
| 22 | //(a really clever JVM would understand that it does not have to
|
|---|
| 23 | //actually read the file if the result is never used)
|
|---|
| 24 | System.out.println("Checksum: "+Arrays.hashCode((Object[])o));
|
|---|
| 25 | }
|
|---|
| 26 |
|
|---|
| 27 | static Object create(int objCount, int refsPerObj, Random r) {
|
|---|
| 28 | System.out.print("Creating "+objCount+" objects... ");
|
|---|
| 29 | long t0 = System.nanoTime();
|
|---|
| 30 | SerTest[] a = new SerTest[objCount];
|
|---|
| 31 | for (int i = 0; i < objCount; i++) {
|
|---|
| 32 | a[i] = new SerTest();
|
|---|
| 33 | a[i].ref = new SerTest[refsPerObj];
|
|---|
| 34 | }
|
|---|
| 35 | for (int i = 0; i < objCount; i++) {
|
|---|
| 36 | for (int j = 0; j < refsPerObj; j++) {
|
|---|
| 37 | int rmin = 0;
|
|---|
| 38 | int rmax = i+1;
|
|---|
| 39 | int k = r.nextInt(rmax-rmin)+rmin;
|
|---|
| 40 | a[i].ref[j] = a[k];
|
|---|
| 41 | }
|
|---|
| 42 | }
|
|---|
| 43 | System.out.println("Done in "+(System.nanoTime()-t0)*1E-9+" seconds.");
|
|---|
| 44 | return a;
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | static void write(Object obj, File outFile) throws Exception {
|
|---|
| 48 | System.out.print("Writing to "+outFile+"... ");
|
|---|
| 49 | long t0 = System.nanoTime();
|
|---|
| 50 | ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(outFile));
|
|---|
| 51 | oos.writeObject(obj);
|
|---|
| 52 | oos.close();
|
|---|
| 53 | System.out.println("Done in "+(System.nanoTime()-t0)*1E-9+" seconds.");
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | static Object read(File inFile) throws Exception {
|
|---|
| 57 | System.out.print("Reading from "+inFile+"... ");
|
|---|
| 58 | long t0 = System.nanoTime();
|
|---|
| 59 | ObjectInputStream oos = new ObjectInputStream(new FileInputStream(inFile));
|
|---|
| 60 | Object o = oos.readObject();
|
|---|
| 61 | oos.close();
|
|---|
| 62 | System.out.println("Done in "+(System.nanoTime()-t0)*1E-9+" seconds.");
|
|---|
| 63 | return o;
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | public int hashCode() {
|
|---|
| 67 | int hash = ref.hashCode();
|
|---|
| 68 | for (SerTest o : ref) {
|
|---|
| 69 | hash += System.identityHashCode(o);
|
|---|
| 70 | }
|
|---|
| 71 | return hash;
|
|---|
| 72 | }
|
|---|
| 73 | }
|
|---|