KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencrx > demo > kernel > StressTester


1 /*
2  * ====================================================================
3  * Project: opencrx, http://www.opencrx.org/
4  * Name: $Id: StressTester.java,v 1.3 2006/03/27 14:08:47 wfro Exp $
5  * Description: openCRX simple client
6  * Revision: $Revision: 1.3 $
7  * Owner: CRIXP AG, Switzerland, http://www.crixp.com
8  * Date: $Date: 2006/03/27 14:08:47 $
9  * ====================================================================
10  *
11  * This software is published under the BSD license
12  * as listed below.
13  *
14  * Copyright (c) 2004, CRIXP Corp., Switzerland
15  * All rights reserved.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions
19  * are met:
20  *
21  * * Redistributions of source code must retain the above copyright
22  * notice, this list of conditions and the following disclaimer.
23  *
24  * * Redistributions in binary form must reproduce the above copyright
25  * notice, this list of conditions and the following disclaimer in
26  * the documentation and/or other materials provided with the
27  * distribution.
28  *
29  * * Neither the name of CRIXP Corp. nor the names of the contributors
30  * to openCRX may be used to endorse or promote products derived
31  * from this software without specific prior written permission
32  *
33  *
34  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
35  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
36  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
37  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
39  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
40  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
41  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
43  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
45  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
46  * POSSIBILITY OF SUCH DAMAGE.
47  *
48  * ------------------
49  *
50  * This product includes software developed by the Apache Software
51  * Foundation (http://www.apache.org/).
52  *
53  * This product includes software developed by contributors to
54  * openMDX (http://www.openmdx.org/)
55  */

56 package org.opencrx.demo.kernel;
57
58 import java.util.Collection JavaDoc;
59 import java.util.Iterator JavaDoc;
60
61 import javax.naming.Context JavaDoc;
62 import javax.naming.InitialContext JavaDoc;
63 import javax.naming.NamingException JavaDoc;
64
65 import org.opencrx.kernel.client.ClientHelper;
66 import org.openmdx.base.accessor.generic.cci.ObjectFactory_1_0;
67 import org.openmdx.base.accessor.jmi.cci.JmiServiceException;
68 import org.openmdx.base.accessor.jmi.cci.RefPackage_1_0;
69 import org.openmdx.base.accessor.jmi.spi.RefRootPackage_1;
70 import org.openmdx.base.exception.ServiceException;
71
72 public class StressTester {
73
74     //-----------------------------------------------------------------------
75
public static void main(
76         String JavaDoc[] args
77     ) {
78         try {
79             Context JavaDoc componentEnvironment = (Context JavaDoc)new InitialContext JavaDoc().lookup("java:comp/env");
80             
81             // Get openMDX object factory
82
ObjectFactory_1_0 of = ClientHelper.createObjectFactory(
83                 componentEnvironment,
84                 "openmdx/data"
85             );
86             
87             // Get JMI root package. For more information about
88
// 'Programming a JMI client for openMDX' see
89
// http://www.openmdx.org/openmdx/1.3/example-lab/htmlsingle/Example-Lab.html
90
RefPackage_1_0 rootPkg = new RefRootPackage_1(
91                 of,
92                 null, // impls
93
null, // context
94
"cci",
95                 false // throw NOT_FOUND
96
);
97
98             // Load models required by client
99
ClientHelper.loadModels(
100                 componentEnvironment,
101                 rootPkg
102             );
103
104             // get account JMI package
105
org.opencrx.kernel.account1.cci.account1Package accountPkg =
106                 (org.opencrx.kernel.account1.cci.account1Package)rootPkg.refPackage(
107                     org.opencrx.kernel.account1.cci.account1Package.class.getName()
108                 );
109             
110             // Get provider and segment name
111
String JavaDoc providerName = (String JavaDoc)componentEnvironment.lookup("providerName");
112             String JavaDoc segmentName = (String JavaDoc)componentEnvironment.lookup("segmentName");
113
114             // get account segment
115
org.opencrx.kernel.account1.cci.Segment accountSegment =
116                 (org.opencrx.kernel.account1.cci.Segment)rootPkg.refObject(
117                     "xri:@openmdx:org.opencrx.kernel.account1/provider/" + providerName + "/segment/" + segmentName
118                 );
119
120             // get home segment
121
org.opencrx.kernel.home1.cci.Segment homeSegment =
122                 (org.opencrx.kernel.home1.cci.Segment)rootPkg.refObject(
123                     "xri:@openmdx:org.opencrx.kernel.home1/provider/" + providerName + "/segment/" + segmentName
124                 );
125             
126             // get product segment
127
org.opencrx.kernel.product1.cci.Segment productSegment =
128                 (org.opencrx.kernel.product1.cci.Segment)rootPkg.refObject(
129                     "xri:@openmdx:org.opencrx.kernel.product1/provider/" + providerName + "/segment/" + segmentName
130                 );
131
132             // get contract segment
133
org.opencrx.kernel.contract1.cci.Segment contractSegment =
134                 (org.opencrx.kernel.contract1.cci.Segment)rootPkg.refObject(
135                     "xri:@openmdx:org.opencrx.kernel.contract1/provider/" + providerName + "/segment/" + segmentName
136                 );
137
138             // Creating accounts
139
long startedAt = System.currentTimeMillis();
140             System.out.println("Creating/Removing accounts");
141             for(
142                 int i = 0;
143                 i < MAX_ITERATIONS;
144                 i++
145             ) {
146                 for(
147                     int j = 0;
148                     j < 100;
149                     j++
150                 ) {
151                     if(j % 100 == 0) {
152                         System.out.println(j + " accounts");
153                     }
154                     org.opencrx.kernel.account1.cci.Contact contact = accountPkg.getContactClass().createContact();
155                     contact.refInitialize(
156                         false,
157                         false
158                     );
159                     contact.setLastName("TEST");
160                     accountPkg.refBegin();
161                     accountSegment.addAccount("TEST", contact);
162                     accountPkg.refCommit();
163                     accountPkg.refBegin();
164                     accountSegment.removeAccount("TEST");
165                     accountPkg.refCommit();
166                 }
167             }
168             System.out.println("Creating/removing accounts (ms): " + (System.currentTimeMillis() - startedAt));
169             
170             // Get all accounts and addresses
171
startedAt = System.currentTimeMillis();
172             System.out.println("Retrieving accounts");
173             int limit = 1000000;
174             Runtime JavaDoc runtime = Runtime.getRuntime();
175             long initialMemoryUsage = runtime.totalMemory() - runtime.freeMemory();
176             System.out.println("Initial memory usage " + initialMemoryUsage);
177             for(
178                 int i = 0;
179                 i < MAX_ITERATIONS;
180                 i++
181             ) {
182                 System.out.println(" Step " + i);
183                 Collection JavaDoc accounts = accountSegment.getAccount();
184                 int jj = 0;
185                 for(
186                     Iterator JavaDoc j = accounts.iterator();
187                     j.hasNext();
188                     jj++
189                 ) {
190                     if(jj % 100 == 0) {
191                         long currentMemoryUsage = runtime.totalMemory() - runtime.freeMemory();
192                         System.out.println(jj + " accounts retrieved. Current memory usage " + currentMemoryUsage);
193                         long additionalMemoryUsage = currentMemoryUsage - initialMemoryUsage;
194                         if(additionalMemoryUsage > limit) {
195                             runtime.gc();
196                             currentMemoryUsage = runtime.totalMemory() - runtime.freeMemory();
197                             additionalMemoryUsage = currentMemoryUsage - initialMemoryUsage;
198                         }
199                     }
200                     org.opencrx.kernel.account1.cci.Account account = (org.opencrx.kernel.account1.cci.Account)j.next();
201                     account.refRefresh();
202                     // Addresses
203
for(
204                         Iterator JavaDoc k = account.getAddress().iterator();
205                         k.hasNext();
206                     ) {
207                         org.opencrx.kernel.account1.cci.AccountAddress address = (org.opencrx.kernel.account1.cci.AccountAddress)k.next();
208                         address.refRefresh();
209                     }
210                 }
211                 System.out.println("Retrieving " + jj + " accounts (ms): " + (System.currentTimeMillis() - startedAt));
212             }
213
214             // Get all homes, alerts, assigned contracts, assigned activities
215
startedAt = System.currentTimeMillis();
216             System.out.println("Retrieving homes");
217             for(
218                 int i = 0;
219                 i < MAX_ITERATIONS;
220                 i++
221             ) {
222                 System.out.println(" Step " + i);
223                 Collection JavaDoc homes = homeSegment.getUserHome();
224                 int jj = 0;
225                 for(
226                     Iterator JavaDoc j = homes.iterator();
227                     j.hasNext();
228                     jj++
229                 ) {
230                     if(jj % 100 == 0) {
231                         System.out.println(jj + " homes");
232                     }
233                     org.opencrx.kernel.home1.cci.UserHome home = (org.opencrx.kernel.home1.cci.UserHome)j.next();
234                     home.refRefresh();
235                     // Alerts
236
for(
237                         Iterator JavaDoc k = home.getAlert().iterator();
238                         k.hasNext();
239                     ) {
240                         org.opencrx.kernel.home1.cci.Alert alert = (org.opencrx.kernel.home1.cci.Alert)k.next();
241                         alert.refRefresh();
242                     }
243                     // Assigned contracts
244
for(
245                         Iterator JavaDoc k = home.getAssignedContract().iterator();
246                         k.hasNext();
247                     ) {
248                         org.opencrx.kernel.contract1.cci.AbstractContract contract = (org.opencrx.kernel.contract1.cci.AbstractContract)k.next();
249                         contract.refRefresh();
250                     }
251                     // Assigned activities
252
for(
253                         Iterator JavaDoc k = home.getAssignedActivity().iterator();
254                         k.hasNext();
255                     ) {
256                         org.opencrx.kernel.activity1.cci.Activity activity = (org.opencrx.kernel.activity1.cci.Activity)k.next();
257                         activity.refRefresh();
258                     }
259                 }
260                 System.out.println("Retrieving " + jj + " homes (ms): " + (System.currentTimeMillis() - startedAt));
261             }
262             
263             // Get all contracts and positions
264
startedAt = System.currentTimeMillis();
265             System.out.println("Retrieving contracts");
266             for(
267                 int i = 0;
268                 i < MAX_ITERATIONS;
269                 i++
270             ) {
271                 System.out.println(" Step " + i);
272                 // Leads
273
Collection JavaDoc leads = contractSegment.getLead();
274                 int jj = 0;
275                 for(
276                     Iterator JavaDoc j = leads.iterator();
277                     j.hasNext();
278                     jj++
279                 ) {
280                     if(jj % 100 == 0) {
281                         System.out.println(jj + " leads");
282                     }
283                     org.opencrx.kernel.contract1.cci.Lead lead = (org.opencrx.kernel.contract1.cci.Lead)j.next();
284                     lead.refRefresh();
285                 }
286                 System.out.println("Retrieving " + jj + " leads (ms): " + (System.currentTimeMillis() - startedAt));
287                 
288                 // Opportunities
289
Collection JavaDoc opportunities = contractSegment.getOpportunity();
290                 jj = 0;
291                 for(
292                     Iterator JavaDoc j = opportunities.iterator();
293                     j.hasNext();
294                 ) {
295                     if(jj % 100 == 0) {
296                         System.out.println(jj + " opportunities");
297                     }
298                     org.opencrx.kernel.contract1.cci.Opportunity opportunity = (org.opencrx.kernel.contract1.cci.Opportunity)j.next();
299                     opportunity.refRefresh();
300                     // Positions
301
for(
302                         Iterator JavaDoc k = opportunity.getPosition().iterator();
303                         k.hasNext();
304                     ) {
305                         org.opencrx.kernel.contract1.cci.ContractPosition position = (org.opencrx.kernel.contract1.cci.ContractPosition)k.next();
306                         position.refRefresh();
307                     }
308                 }
309                 System.out.println("Retrieving " + jj + " opportunities (ms): " + (System.currentTimeMillis() - startedAt));
310
311                 // Quotes
312
Collection JavaDoc quotes = contractSegment.getQuote();
313                 jj = 0;
314                 for(
315                     Iterator JavaDoc j = quotes.iterator();
316                     j.hasNext();
317                 ) {
318                     if(jj % 100 == 0) {
319                         System.out.println(jj + " quotes");
320                     }
321                     org.opencrx.kernel.contract1.cci.Quote quote = (org.opencrx.kernel.contract1.cci.Quote)j.next();
322                     quote.refRefresh();
323                     // Positions
324
for(
325                         Iterator JavaDoc k = quote.getPosition().iterator();
326                         k.hasNext();
327                     ) {
328                         org.opencrx.kernel.contract1.cci.ContractPosition position = (org.opencrx.kernel.contract1.cci.ContractPosition)k.next();
329                         position.refRefresh();
330                     }
331                 }
332                 System.out.println("Retrieving " + jj + " quotes (ms): " + (System.currentTimeMillis() - startedAt));
333
334                 // SalesOrders
335
Collection JavaDoc salesOrders = contractSegment.getSalesOrder();
336                 jj = 0;
337                 for(
338                     Iterator JavaDoc j = salesOrders.iterator();
339                     j.hasNext();
340                 ) {
341                     if(jj % 100 == 0) {
342                         System.out.println(jj + " sales orders");
343                     }
344                     org.opencrx.kernel.contract1.cci.SalesOrder salesOrder = (org.opencrx.kernel.contract1.cci.SalesOrder)j.next();
345                     salesOrder.refRefresh();
346                     // Positions
347
for(
348                         Iterator JavaDoc k = salesOrder.getPosition().iterator();
349                         k.hasNext();
350                     ) {
351                         org.opencrx.kernel.contract1.cci.ContractPosition position = (org.opencrx.kernel.contract1.cci.ContractPosition)k.next();
352                         position.refRefresh();
353                     }
354                 }
355                 System.out.println("Retrieving " + jj + " sales orders (ms): " + (System.currentTimeMillis() - startedAt));
356
357                 // Invoices
358
Collection JavaDoc invoices = contractSegment.getInvoice();
359                 jj = 0;
360                 for(
361                     Iterator JavaDoc j = invoices.iterator();
362                     j.hasNext();
363                 ) {
364                     if(jj % 100 == 0) {
365                         System.out.println(jj + " invoices");
366                     }
367                     org.opencrx.kernel.contract1.cci.Invoice invoice = (org.opencrx.kernel.contract1.cci.Invoice)j.next();
368                     invoice.refRefresh();
369                     // Positions
370
for(
371                         Iterator JavaDoc k = invoice.getPosition().iterator();
372                         k.hasNext();
373                     ) {
374                         org.opencrx.kernel.contract1.cci.ContractPosition position = (org.opencrx.kernel.contract1.cci.ContractPosition)k.next();
375                         position.refRefresh();
376                     }
377                 }
378                 System.out.println("Retrieving " + jj + " invoices (ms): " + (System.currentTimeMillis() - startedAt));
379             }
380         }
381         catch (NamingException JavaDoc e) {
382             e.printStackTrace();
383         }
384         catch (ServiceException e) {
385             e.printStackTrace();
386         }
387         catch (JmiServiceException e) {
388             e.printStackTrace();
389         }
390     }
391     
392     //-----------------------------------------------------------------------
393
// Members
394
//-----------------------------------------------------------------------
395
private static final int MAX_ITERATIONS = 2;
396         
397 }
398
399 //--- End of File -----------------------------------------------------------
400
Popular Tags