KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > benchmark > ejb > latency > Client


1 /*====================================================================
2
3 OpenCCM: The Open CORBA Component Model Platform
4 Copyright (C) 2000-2004 INRIA - USTL - LIFL - GOAL
5 Contact: openccm@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library 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 library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Christophe Demarey.
23 Contributor(s): Gael Harbonnier.
24
25 ====================================================================*/

26
27 package org.objectweb.benchmark.ejb.latency;
28
29 //Package dependencies.
30
import java.rmi.RemoteException JavaDoc;
31
32 import javax.naming.InitialContext JavaDoc;
33 import javax.rmi.PortableRemoteObject JavaDoc;
34 import org.objectweb.benchmark.util.ResultPrinter;
35
36
37 /**
38  * This client performs EJB latency Bencmark by calling the ping method.
39  *
40  * @author <a HREF="mailto:Christophe.Demarey@lifl.fr">Christophe Demarey</a>
41  *
42  * @version 0.1
43  */

44 public class Client
45 {
46     // ==================================================================
47
//
48
// Internal states.
49
//
50
// ==================================================================
51

52     /** Number of steps*/
53     private int nb_step;
54
55     /** Number of invocations per step*/
56     private int nb_step_invocation;
57     
58     /** Beginning of the first step*/
59     private int step_start;
60
61     /** Results file name */
62     private String JavaDoc result_file;
63     
64     /** Ping interface reference */
65     private PingRemoteInterface pingBean = null;
66
67     // ==================================================================
68
//
69
// Constructors.
70
//
71
// ==================================================================
72

73     /**
74      * The default constructor.
75      *
76      * @param n - Number of invocations to do.
77      * @param name - Results file name.
78      */

79     public Client(String JavaDoc name)
80     {
81         result_file = name;
82         java.util.Properties JavaDoc properties = new java.util.Properties JavaDoc();
83
84
85         try {
86             java.io.InputStream JavaDoc propStream = null;
87         
88             propStream = getClass().getClassLoader().getResourceAsStream("benchmark.properties");
89             properties.load(propStream);
90         
91             nb_step = Integer.parseInt( properties.getProperty("step.number") );
92             nb_step_invocation = Integer.parseInt( properties.getProperty("step.invocation.number") );
93             step_start = Integer.parseInt( properties.getProperty("step.start") );
94         }catch (Exception JavaDoc e) {
95             e.printStackTrace();
96         }
97
98     }
99
100     // ==================================================================
101
//
102
// Internal methods.
103
//
104
// ==================================================================
105

106     // ==================================================================
107
//
108
// Public methods.
109
//
110
// ==================================================================
111

112    /**
113      * The bootstrap method.
114      *
115      * @param args - Application arguments
116      */

117     public static void main(String JavaDoc[] args)
118                 throws RemoteException JavaDoc
119     {
120         if (args.length != 1)
121         {
122             System.out.println("Usage: Client <dest_file>");
123             System.exit(0);
124         }
125
126         Client c = new Client(args[0]);
127         c.run();
128     }
129
130    /**
131      * Runs n invocations on the EJB client.
132      */

133     public void run()
134          throws RemoteException JavaDoc
135     {
136         PingRemoteHome pingHome = null;
137         
138         // Obtains the Ping Itf reference
139
try {
140             InitialContext JavaDoc ctx = new InitialContext JavaDoc() ;
141             Object JavaDoc obj= ctx.lookup("MyPingBean") ;
142             
143             pingHome = (PingRemoteHome) PortableRemoteObject.narrow(obj, PingRemoteHome.class) ;
144             pingBean = pingHome.create() ;
145         }catch(Exception JavaDoc ex){
146             ex.printStackTrace();
147         }
148         
149         // Runs benchmark
150
test();
151     }
152     
153     /**
154      * Runs the latency benchmark.
155      */

156     public void test()
157     throws RemoteException JavaDoc
158     {
159         // Don't include the first step_start invocations
160
for (int i=0; i<step_start; i++)
161            pingBean.ping();
162
163         // Runs test
164
System.out.println("Running EJB Benchmark with " + nb_step + " step(s) of " + nb_step_invocation +" method calls.");
165         try{
166             for(int j=0; j<nb_step; j++)
167             {
168                 long timer = System.currentTimeMillis();
169                 for (int i=0; i<nb_step_invocation; i++)
170                     pingBean.ping();
171                 timer = (long) ( ( System.currentTimeMillis() - timer ) * 1000 / (float)nb_step_invocation );
172                 
173                 // write results
174
ResultPrinter p = new ResultPrinter(result_file);
175                 p.write(timer);
176                 p.close();
177             }
178         }catch(Exception JavaDoc ex){
179             ex.printStackTrace();
180         }
181     }
182 }
183
Popular Tags