KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > stress > LinkedIntUserIdHelper


1 /**
2  * Copyright (C) 2001-2004 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package org.objectweb.speedo.stress;
19
20 import org.objectweb.speedo.pobjects.userid.LinkedIntUserId;
21 import org.objectweb.util.monolog.api.BasicLevel;
22
23 import junit.framework.Assert;
24
25 import javax.jdo.PersistenceManager;
26 import javax.jdo.JDOFatalException;
27
28 /**
29  *
30  * @author S.Chassande-Barrioz
31  */

32 public abstract class LinkedIntUserIdHelper extends StressHelper {
33
34     /**
35      * is the lists of object identifier prepared before the transaction
36      * execution.
37      * if (oids == null) {
38      * db is not initialised
39      * } else if (oids != null && oids.length==0) {
40      * db initialised and keepOid = false
41      * } else {
42      * db initialised and keepOid == true
43      * }
44      */

45     protected static Object JavaDoc[] oids = null;
46     protected String JavaDoc DBSIZE = getLoggerName() + ".dbsize";
47     protected String JavaDoc NO_DB_INIT = getLoggerName() + ".nodbinit";
48
49
50     public LinkedIntUserIdHelper(String JavaDoc s) {
51         super(s);
52     }
53
54     protected String JavaDoc[] getClassNamesToInit() {
55         return new String JavaDoc[]{LinkedIntUserId.class.getName()};
56     }
57
58     protected boolean linkObject() {
59         return true;
60     }
61
62     protected boolean keepOid() {
63         return true;
64     }
65
66     /**
67      * IMPORTANT: dot not removed data on support in order to avoid next
68      * creations
69      */

70     public void setUp() throws Exception JavaDoc {
71         logger.log(BasicLevel.DEBUG, "LinkedIntUserIdHelper.setUp");
72         cleanup();
73         initDataStructure(false);
74     }
75
76     /**
77      * Creates the persistent object if it is not already done.
78      * @param task the task to prepare
79      * @param _ctx the context of the test.
80      */

81     protected void prepareTask(Task task, Object JavaDoc _ctx) {
82         debug = logger.isLoggable(BasicLevel.DEBUG);
83         logger.log(BasicLevel.INFO, "LinkedIntUserIdHelper.debug="+debug);
84         super.prepareTask(task, _ctx);
85         LIUICtx ctx = (LIUICtx) _ctx;
86         if (oids == null) {
87             synchronized (TestGetObject.class) {
88                 if (oids == null && !Boolean.getBoolean(NO_DB_INIT)) {
89                     //Initialisation the database
90
logger.log(BasicLevel.INFO, "\tPreparing test...");
91                     new PrepareTestLinked(this)
92                             .prepare(ctx, keepOid(), linkObject());
93                     if (keepOid()) {
94                         //keep oids in the static variable
95
oids = ctx.oids;
96                     } else {
97                         //db initialized without oids
98
oids = new Object JavaDoc[0];
99                     }
100                     logger.log(BasicLevel.INFO, "\tTest Prepared.");
101                 }
102             }
103         }
104         ctx.oids = oids;
105         
106     }
107
108     
109     /**
110      * The context to use for the LinkedIntUserId object
111      */

112     public class LIUICtx {
113         /**
114          * The identifier of the created object (see keepOid method)
115          */

116         public Object JavaDoc oids[];
117         /**
118          * The number of persistent object
119          */

120         public int dbSize;
121         /**
122          * indicates if the oid must be kept during the preparation of the task
123          */

124         public boolean keepOidOnPrepare;
125         public boolean linkOnPrepare;
126         public boolean endPrepare;
127         public Object JavaDoc[] currentsOnPrepare;
128         public Object JavaDoc[] nextsOnPrepare;
129
130         public LIUICtx(int dbSize) {
131             this.dbSize = dbSize;
132             oids = new Object JavaDoc[dbSize];
133         }
134
135         public void initOnPrepare(int nbTx, boolean fetchOid, boolean linkOnPrepare) {
136             this.keepOidOnPrepare = fetchOid;
137             this.linkOnPrepare = linkOnPrepare;
138             this.endPrepare = false;
139             nextsOnPrepare = new Object JavaDoc[nbTx];
140             currentsOnPrepare = new Object JavaDoc[nbTx];
141         }
142
143         public void endOnPrepare(int nbTx, boolean fetchOid, boolean linkOnPrepare) {
144             this.endPrepare = true;
145         }
146
147         public String JavaDoc toString() {
148             return "dbSize = " + dbSize;
149         }
150     }
151 }
152
153 class PrepareTestLinked extends StressHelper {
154
155     private final static int NB_CREATION = 1000;
156     private final static int NB_THREAD = 4;
157
158     public PrepareTestLinked(LinkedIntUserIdHelper helper) {
159         super(helper.getName());
160     }
161
162     protected String JavaDoc[] getClassNamesToInit() {
163         return new String JavaDoc[]{LinkedIntUserId.class.getName()};
164     }
165
166     protected String JavaDoc getLoggerName() {
167         return STRESS_LOG_NAME + ".LinkedIntUserIdHelper";
168     }
169
170     protected String JavaDoc getLogPrefix() {
171         return super.getLogPrefix() + "\t";
172     }
173
174     protected void perform(StressHelper.Task task,
175                            int threadId,
176                            int txId,
177                            Object JavaDoc ctx,
178                            PerformResult res) {
179         LinkedIntUserIdHelper.LIUICtx pctx = (LinkedIntUserIdHelper.LIUICtx) ctx;
180         int plus = pctx.dbSize % NB_CREATION;
181         int nbTx = task.txToExecute.length;
182         PersistenceManager pm = getPM(task, threadId, txId);
183         try {
184             res.beginTest();
185             beginTx(pm, task, threadId, txId);
186             LinkedIntUserId current=null, next=null;
187             
188             if (!pctx.linkOnPrepare) {
189                 if (plus > 0) {
190                     //The first transaction creates the additional object
191
for (int oid = 0; txId == 0 && oid < plus; oid++) {
192                         current = new LinkedIntUserId(oid, "Obj No " + oid, null);
193                         pm.makePersistent(current);
194                         if (pctx.keepOidOnPrepare) {
195                             pctx.oids[oid] = pm.getObjectId(current);
196                         }
197                     }
198                 }
199                 // The other transactions create 'nbCreation' objects
200
for (int no = 0; no < NB_CREATION; no++) {
201                     int oid = (txId * NB_CREATION) + no + plus;
202                     if (oid < pctx.dbSize) {
203                         current = new LinkedIntUserId(oid, "Obj No " + oid, null);
204                         pm.makePersistent(current);
205                         if (pctx.keepOidOnPrepare) {
206                             pctx.oids[oid] = pm.getObjectId(current);
207                         }
208                     }
209                 }
210             } else if (!pctx.endPrepare) {
211                     
212                 /*
213                  * First : save the oid of objects to be linked between transactions
214                  */

215                 
216                 int oid = 0;
217                 if (plus > 0 && txId == 0) {
218                     current = new LinkedIntUserId(oid, "Obj No " + oid, null);
219                     pm.makePersistent(current);
220                     if (pctx.keepOidOnPrepare) {
221                         pctx.oids[oid] = pm.getObjectId(current);
222                     }
223                     
224                     oid++;
225                     next = new LinkedIntUserId(oid, "Obj No " + oid, null);
226                     pm.makePersistent(next);
227                     if (pctx.keepOidOnPrepare) {
228                         pctx.oids[oid] = pm.getObjectId(next);
229                     }
230                     
231                     current.setNext(next);
232                     pctx.currentsOnPrepare[txId] = pm.getObjectId(current);
233                     
234                     //The first transaction creates the additional object
235
for (oid = 2; oid < plus; oid++) {
236                         current = new LinkedIntUserId(oid, "Obj No " + oid, null);
237                         pm.makePersistent(current);
238                         if (pctx.keepOidOnPrepare) {
239                             pctx.oids[oid] = pm.getObjectId(current);
240                         }
241                         next.setNext(current);
242                         pm.makePersistent(next);
243                         next = current;
244                     }
245                 }
246                 
247                 // The other transactions create 'nbCreation' objects
248
for (int no = 0; no < NB_CREATION; no++) {
249                     oid = (txId * NB_CREATION) + no + plus;
250                     if (oid < pctx.dbSize) {
251                         current = new LinkedIntUserId(oid, "Obj No " + oid, null);
252                         pm.makePersistent(current);
253                         if (no == 0 && pctx.currentsOnPrepare[txId] == null) {
254                             pctx.currentsOnPrepare[txId] = pm.getObjectId(current);
255                             
256                         }
257                         if (pctx.keepOidOnPrepare) {
258                             pctx.oids[oid] = pm.getObjectId(current);
259                         }
260                         if (next != null) {
261                             next.setNext(current);
262                             pm.makePersistent(next);
263                         }
264                         next = current;
265                     }
266                 }
267                 
268                 // save the oid of objects to be linked between transactions
269
pctx.nextsOnPrepare[txId] = pm.getObjectId(next);
270                 
271             } else if (txId == nbTx - 1){ // do it once only
272

273                 /*
274                  * Second : link the oid
275                  */

276                 if (debug) {
277                     for (int i=0; i < nbTx; i++) {
278                         logger.log(BasicLevel.DEBUG, "currentsOnPrepare["+i+"]="+ pctx.currentsOnPrepare[i]);
279                         logger.log(BasicLevel.DEBUG, "nextsOnPrepare["+i+"]="+ pctx.nextsOnPrepare[i]);
280                     }
281                 }
282                 
283                 // make circular list
284
next = (LinkedIntUserId) pm.getObjectById(pctx.currentsOnPrepare[0], false);
285                 current = (LinkedIntUserId) pm.getObjectById(pctx.nextsOnPrepare[txId], false);
286                 
287                 if (plus != 0) {
288                     if (txId == 0) {
289                         current = (LinkedIntUserId) pm.getObjectById(pctx.nextsOnPrepare[txId], false);
290                     } else {
291                         current = (LinkedIntUserId) pm.getObjectById(pctx.nextsOnPrepare[txId-1], false);
292                     }
293                 }
294                 
295                 current.setNext(next);
296                 pm.makePersistent(current);
297                 if (pctx.keepOidOnPrepare) {
298                     int indice = (nbTx * NB_CREATION) - 1;
299                     if (plus != 0) {
300                         indice = (txId * NB_CREATION) + plus - 1;
301                     }
302                     pctx.oids[indice] = pm.getObjectId(current);
303                 }
304                 
305                 
306                 // link objects between transactions
307
for (int no = 0; no < nbTx - 1; no++) {
308                     if (pctx.currentsOnPrepare[no+1] != null) {
309                         next = (LinkedIntUserId) pm.getObjectById(pctx.currentsOnPrepare[no+1], false);
310                         current = (LinkedIntUserId) pm.getObjectById(pctx.nextsOnPrepare[no], false);
311                         current.setNext(next);
312                     }
313                 }
314                 
315                 // verify all
316
for (int i=0; i<pctx.oids.length-1; i++) {
317                     Assert.assertNotNull("i="+i, pctx.oids[i]);
318                     Assert.assertNotNull("i="+i+1, pctx.oids[i+1]);
319                     current = (LinkedIntUserId) pm.getObjectById(pctx.oids[i], false);
320                     next = (LinkedIntUserId) pm.getObjectById(pctx.oids[i+1], false);
321                     Assert.assertEquals("name="+current.getName(), current.getNext().getName(), next.getName());
322                 }
323                 next = (LinkedIntUserId) pm.getObjectById(pctx.oids[0], false);
324                 current = (LinkedIntUserId) pm.getObjectById(pctx.oids[pctx.oids.length-1], false);
325                 Assert.assertEquals("name="+current.getName(), current.getNext().getName(), next.getName());
326                 
327                 
328             }
329             commitTx(pm, task, threadId, txId);
330             res.endTest();
331         } catch (JDOFatalException e) {
332             rollbackOnException(pm, e, res, task, threadId, txId);
333         } catch (Throwable JavaDoc e) {
334             stopOnError(pm, e, res, task, threadId, txId);
335         } finally {
336             closePM(pm, threadId, txId, task, res);
337         }
338
339     }
340
341     public void prepare(LinkedIntUserIdHelper.LIUICtx ctx, boolean fetchOid, boolean link) {
342         int plus = ctx.dbSize % NB_CREATION;
343         int nbTx = (ctx.dbSize / NB_CREATION) + (plus > 0 ? 1 : 0);
344         ctx.initOnPrepare(nbTx, fetchOid, link);
345         perform(NB_THREAD, nbTx, Integer.getInteger(TIMEOUT, 200000).intValue(), ctx);
346         ctx.endOnPrepare(nbTx, fetchOid, link);
347         perform(NB_THREAD, nbTx, Integer.getInteger(TIMEOUT, 200000).intValue(), ctx);
348         
349     }
350 }
351
352
Popular Tags