KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > ubik > rmi > server > perf > SerPerf


1 package org.sapia.ubik.rmi.server.perf;
2
3 import org.sapia.ubik.net.*;
4 import org.sapia.ubik.rmi.server.*;
5
6 import java.io.*;
7
8 import java.util.*;
9
10
11 /**
12  * @author Yanick Duchesne
13  * <dl>
14  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
15  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
16  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
17  * </dl>
18  */

19 public class SerPerf {
20   public static void main(String JavaDoc[] args) {
21     try {
22       List objects = new ArrayList();
23
24       for (int k = 0; k < 2; k++) {
25         for (int i = 0; i < 2000; i++) {
26           if (k == 0) {
27             objects.add(newRemoteRef(i));
28           } else {
29             objects.add(newString(i));
30           }
31         }
32
33         if (k == 0) {
34           System.out.println(" ==== results for remote refs ====");
35         } else {
36           System.out.println(" ==== results for strings ====");
37         }
38
39         long start = System.currentTimeMillis();
40         InputStream is = serialize(objects.toArray());
41         long delay = System.currentTimeMillis() - start;
42         System.out.println("serialization delay: " + delay);
43         start = System.currentTimeMillis();
44         deserialize(is);
45         delay = System.currentTimeMillis() - start;
46         System.out.println("deserialization delay: " + delay);
47         objects.clear();
48       }
49     } catch (Throwable JavaDoc t) {
50       t.printStackTrace();
51     }
52   }
53
54   public static RemoteRef newRemoteRef(int i) {
55     return new RemoteRefEx(new OID(i), new TCPAddress("localhost", 8080));
56   }
57
58   public static String JavaDoc newString(int i) {
59     return "" + i;
60   }
61
62   public static InputStream serialize(Object JavaDoc[] objects)
63     throws Throwable JavaDoc {
64     ByteArrayOutputStream bos = new ByteArrayOutputStream();
65     ObjectOutputStream out = new ObjectOutputStream(bos);
66     out.writeObject(objects);
67     out.flush();
68     out.close();
69
70     return new ByteArrayInputStream(bos.toByteArray());
71   }
72
73   public static Object JavaDoc[] deserialize(InputStream is) throws Throwable JavaDoc {
74     ObjectInputStream ois = new ObjectInputStream(is);
75
76     return (Object JavaDoc[]) ois.readObject();
77   }
78 }
79
Popular Tags