KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > scenario > standalone > socket > UTFWithSocketTest


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Nicolas Modrzyk
22  * Contributor(s): ______________________.
23  */

24
25 package org.objectweb.cjdbc.scenario.standalone.socket;
26
27 import java.util.Enumeration JavaDoc;
28 import java.util.ResourceBundle JavaDoc;
29
30 import org.objectweb.cjdbc.scenario.templates.NoTemplate;
31
32 /**
33  * This class defines a UTFWithSocketTest class
34  *
35  * @author <a HREF="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
36  */

37 public class UTFWithSocketTest extends NoTemplate
38 {
39   
40   
41   /**
42    * Test with repeated transmission of an image, to compare the speed
43    * between writeObject and writeBytes
44    * @throws Exception if fails
45    */

46   public void testUTFOverSocketWithImage() throws Exception JavaDoc
47   {
48 // Get test properties
49
ResourceBundle JavaDoc rb = ResourceBundle.getBundle("socketTest");
50     Enumeration JavaDoc enu = rb.getKeys();
51     String JavaDoc key;
52     while (enu.hasMoreElements())
53     {
54       key = (String JavaDoc) enu.nextElement();
55       System.out.println("Using test property <" + key + "> with value <"
56           + rb.getObject(key) + ">");
57     }
58     int serverPort = Integer.parseInt(rb.getString("serverPort"));
59     String JavaDoc imageFile = rb.getString("imageFile");
60     int numberOfLoop = Integer.parseInt(rb.getString("numberOfLoop"));
61     String JavaDoc serverName = rb.getString("serverName");
62     boolean useBufferedStream = new Boolean JavaDoc(rb.getString("useBufferedStream"))
63         .booleanValue();
64     boolean useWriteObject = new Boolean JavaDoc(rb.getString("useWriteObject"))
65         .booleanValue();
66     boolean useSameObject = new Boolean JavaDoc(rb.getString("useSameObject"))
67         .booleanValue();
68     boolean useGC = new Boolean JavaDoc(rb.getString("useGC")).booleanValue();
69     boolean useReset = new Boolean JavaDoc(rb.getString("useReset")).booleanValue();
70
71     // Set properties of the client
72
UTFClient cutf = new UTFClient();
73     cutf.setServerPort(serverPort);
74     cutf.setLoop(numberOfLoop);
75     cutf.setImageFile(imageFile);
76     cutf.setServerName(serverName);
77     cutf.setUseBufferedStream(useBufferedStream);
78     cutf.setUseWriteObject(useWriteObject);
79     cutf.setUseGC(useGC);
80     cutf.setUseSameObject(useSameObject);
81     cutf.setUseReset(useReset);
82
83     // Set properties of the server
84
UTFServer sutf = new UTFServer();
85     sutf.setServerPort(serverPort);
86     
87     // Start test
88
long started = System.currentTimeMillis();
89     long freeStart = Runtime.getRuntime().freeMemory();
90     Thread JavaDoc server = new Thread JavaDoc(sutf);
91     Thread JavaDoc client = new Thread JavaDoc(cutf);
92     server.start();
93     client.start();
94     
95     // Wait
96
server.join();
97     client.join();
98     long end = System.currentTimeMillis();
99     long freeEnd = Runtime.getRuntime().freeMemory();
100     long usedMemory = (freeEnd - freeStart);
101     long last = end - started;
102     long time = last/1000;
103     float average = (numberOfLoop / time);
104     
105     System.out.println("The test lasted "+time+" s. ("+last+" ms.) for an average of "+average+" loop per second. ");
106     System.out.println("Used memory was:"+usedMemory/1024/1024+" Mb, "+usedMemory+" bytes.");
107     assertEquals(cutf.getHexa(),sutf.getHexa());
108   }
109 }
Popular Tags