KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > performance > serialize > SerializeTEST


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package test.performance.serialize;
23
24 import java.io.ByteArrayInputStream JavaDoc;
25 import java.io.ByteArrayOutputStream JavaDoc;
26 import java.io.ObjectInputStream JavaDoc;
27 import java.io.ObjectOutputStream JavaDoc;
28
29 import junit.framework.TestCase;
30 import test.performance.PerformanceSUITE;
31
32 /**
33  * Tests the speed of serialization
34  *
35  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
36  */

37 public class SerializeTEST
38    extends TestCase
39 {
40    // Attributes ----------------------------------------------------------------
41

42    /**
43     * The object to test
44     */

45    private Object JavaDoc obj;
46
47    /**
48     * The description of the test
49     */

50    private String JavaDoc desc;
51
52    // Constructor ---------------------------------------------------------------
53

54    /**
55     * Construct the test
56     */

57    public SerializeTEST(String JavaDoc s, Object JavaDoc obj, String JavaDoc desc)
58    {
59       super(s);
60       this.obj = obj;
61       this.desc = desc;
62    }
63
64    /**
65     * Test Serialization
66     */

67    public void testIt()
68    {
69       System.out.println("\n" + desc);
70       System.out.println(PerformanceSUITE.SERIALIZE_ITERATION_COUNT + " Serializations, Repeat: x" + PerformanceSUITE.REPEAT_COUNT);
71       System.out.println("(this may take a while...)");
72
73       long start = 0, end = 0;
74       float avg = 0l;
75       int size = 0;
76
77       try
78       {
79
80          Object JavaDoc result = null;
81          ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
82
83          // drop the first batch (+1)
84
for (int testIterations = 0; testIterations < PerformanceSUITE.REPEAT_COUNT + 1; ++testIterations)
85          {
86             start = System.currentTimeMillis();
87             for (int invocationIterations = 0; invocationIterations < PerformanceSUITE.SERIALIZE_ITERATION_COUNT; ++invocationIterations)
88             {
89                // Serialize it
90
baos.reset();
91                ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(baos);
92                oos.writeObject(obj);
93     
94                // Deserialize it
95
ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(baos.toByteArray());
96                ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(bais);
97                result = ois.readObject();
98             }
99             end = System.currentTimeMillis();
100
101             if (testIterations != 0)
102             {
103                long time = end - start;
104                System.out.print( time + " ");
105                avg += time;
106             }
107          }
108
109          System.out.print("\nAverage: " + (avg/PerformanceSUITE.REPEAT_COUNT));
110          System.out.println(" Size: " + baos.toByteArray().length);
111       }
112       catch (Exception JavaDoc e)
113       {
114          fail(e.toString());
115       }
116    }
117 }
118
Popular Tags