KickJava   Java API By Example, From Geeks To Geeks.

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


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.relations.C;
21 import org.objectweb.speedo.pobjects.relations.D;
22 import org.objectweb.util.monolog.api.BasicLevel;
23 import org.objectweb.speedo.Alea;
24
25 import javax.jdo.PersistenceManager;
26 import javax.jdo.JDOFatalException;
27
28 /**
29  *
30  * @author M. Guillemin
31  */

32 public abstract class CDRelationHelper 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     
46     protected static Object JavaDoc[] oidsC = null;
47     protected static Object JavaDoc[] oidsD = null;
48     protected String JavaDoc DBSIZE = getLoggerName() + ".dbsize";
49     protected String JavaDoc NO_DB_INIT = getLoggerName() + ".nodbinit";
50
51
52     public CDRelationHelper(String JavaDoc s) {
53         super(s);
54     }
55
56     protected String JavaDoc[] getClassNamesToInit() {
57         return new String JavaDoc[]{
58                 org.objectweb.speedo.pobjects.relations.C.class.getName(),
59                 org.objectweb.speedo.pobjects.relations.D.class.getName()
60         };
61     }
62
63     protected boolean keepOid() {
64         return true;
65     }
66
67     protected abstract int getNbSucc();
68     
69     /**
70      * IMPORTANT: dot not removed data on support in order to avoid next
71      * creations
72      */

73     public void setUp() throws Exception JavaDoc {
74         logger.log(BasicLevel.DEBUG, "setUp.");
75         cleanup();
76         initDataStructure(false);
77         debug = logger.isLoggable(BasicLevel.DEBUG);
78     }
79
80     protected void prepareTest(TaskManager tm, Object JavaDoc ctx) {
81         super.prepareTest(tm, ctx);
82         Task task = tm.tasks[0];
83         CDCtx cdctx = (CDCtx) ctx;
84  
85         if (task.txToExecute.length * getNbSucc() > cdctx.dbSize) {
86             fail("Adjust parameter: tx <= "+cdctx.dbSize/getNbSucc() + " OR dbSize >= "+task.txToExecute.length * getNbSucc());
87         }
88     }
89
90     /**
91      * Creates the persistent object if it is not already done.
92      * @param task the task to prepare
93      * @param _ctx the context of the test.
94      */

95     protected void prepareTask(Task task, Object JavaDoc _ctx) {
96         super.prepareTask(task, _ctx);
97         CDCtx ctx = (CDCtx) _ctx;
98         if (oidsC == null) {
99             synchronized (getClass()) {
100                 if (oidsC == null && !Boolean.getBoolean(NO_DB_INIT)) {
101                     //Initialisation the database
102
logger.log(BasicLevel.INFO, "\tPreparing test...");
103                     new PrepareTestCDRelation(this)
104                             .prepare(ctx, keepOid());
105                     if (keepOid()) {
106                         //keep oids in the static variable
107
oidsC = ctx.oidsC;
108                         oidsD = ctx.oidsD;
109                     } else {
110                         //db initialized without oids
111
oidsC = new Object JavaDoc[0];
112                         oidsD = new Object JavaDoc[0];
113                     }
114                     logger.log(BasicLevel.INFO, "\tTest Prepared.");
115                 }
116             }
117         }
118         ctx.oidsC = oidsC;
119         ctx.oidsD = oidsD;
120     }
121
122     protected int getOid(CDCtx cdctx, boolean useTwiceTheSameOid, int nbSucc) {
123         int oid = Alea.rand(0, cdctx.dbSize - nbSucc);
124         if (!useTwiceTheSameOid) {
125             synchronized (cdctx.usedOids) {
126                 while (oid % nbSucc != 0) {
127                     oid++;
128                 }
129                 while (cdctx.usedOids[oid]) {
130                     oid = Alea.rand(0, cdctx.dbSize - nbSucc);
131                     while (oid % nbSucc != 0) {
132                         oid++;
133                     }
134                 }
135                 for (int i=0; i<nbSucc; i++) {
136                     cdctx.usedOids[oid+i] = true;
137                 }
138             }
139         }
140         return oid;
141     }
142     
143     /**
144      * The context to use for the CDRelation object
145      */

146     public class CDCtx {
147         /**
148          * The identifier of the created object (see keepOid method)
149          */

150         public Object JavaDoc oidsC[];
151         public Object JavaDoc oidsD[];
152         public boolean usedOids[];
153         /**
154          * The number of persistent object
155          */

156         public int dbSize;
157         /**
158          * indicates if the oid must be kept during the preparation of the task
159          */

160         public boolean keepOid;
161         
162         
163         public CDCtx(int dbSize) {
164             this.dbSize = dbSize;
165             oidsC = new Object JavaDoc[dbSize];
166             oidsD = new Object JavaDoc[dbSize];
167             usedOids = new boolean[dbSize];
168         }
169
170         public void initOnPrepare(int nbTx, boolean fetchOid) {
171             this.keepOid = fetchOid;
172             for (int i=0; i<dbSize; i++)
173                 usedOids[i] = false;
174         }
175
176         public String JavaDoc toString() {
177             return "dbSize = " + dbSize;
178         }
179     }
180 }
181
182 class PrepareTestCDRelation extends StressHelper {
183
184     private final static int NB_CREATION = 100;
185     private final static int NB_THREAD = 4;
186
187     public PrepareTestCDRelation(CDRelationHelper helper) {
188         super(helper.getName());
189     }
190
191     protected String JavaDoc[] getClassNamesToInit() {
192         return new String JavaDoc[]{
193                 org.objectweb.speedo.pobjects.relations.C.class.getName(),
194                 org.objectweb.speedo.pobjects.relations.D.class.getName()
195         };
196     }
197
198     protected String JavaDoc getLoggerName() {
199         return STRESS_LOG_NAME + ".CDRelationHelper";
200     }
201
202     protected String JavaDoc getLogPrefix() {
203         return super.getLogPrefix() + "\t";
204     }
205
206     protected void perform(StressHelper.Task task,
207                            int threadId,
208                            int txId,
209                            Object JavaDoc ctx,
210                            PerformResult res) {
211         CDRelationHelper.CDCtx pctx = (CDRelationHelper.CDCtx) ctx;
212         int plus = pctx.dbSize % NB_CREATION;
213         int nbTx = task.txToExecute.length;
214         PersistenceManager pm = getPM(task, threadId, txId);
215         try {
216             res.beginTest();
217             beginTx(pm, task, threadId, txId);
218             if (plus > 0) {
219                 //The first transaction creates the additional object
220
for (int oid = 0; txId == 0 && oid < plus; oid++) {
221                     C c = new C("c" + oid);
222                     D d = new D("d" + oid);
223                     pm.makePersistent(c);
224                     pm.makePersistent(d);
225                     if (pctx.keepOid) {
226                         pctx.oidsC[oid] = pm.getObjectId(c);
227                         pctx.oidsD[oid] = pm.getObjectId(d);
228                     }
229                 }
230             }
231             // The other transactions create 'nbCreation' objects
232
for (int no = 0; no < NB_CREATION; no++) {
233                 int oid = (txId * NB_CREATION) + no + plus;
234                 if (oid < pctx.dbSize) {
235                     C c = new C("c" + oid);
236                     D d = new D("d" + oid);
237                     pm.makePersistent(c);
238                     pm.makePersistent(d);
239                     if (pctx.keepOid) {
240                         pctx.oidsC[oid] = pm.getObjectId(c);
241                         pctx.oidsD[oid] = pm.getObjectId(d);
242                     }
243                 }
244             }
245             
246             commitTx(pm, task, threadId, txId);
247             res.endTest();
248         } catch (JDOFatalException e) {
249             rollbackOnException(pm, e, res, task, threadId, txId);
250         } catch (Throwable JavaDoc e) {
251             stopOnError(pm, e, res, task, threadId, txId);
252         } finally {
253             closePM(pm, threadId, txId, task, res);
254         }
255
256     }
257
258     public void prepare(CDRelationHelper.CDCtx ctx, boolean fetchOid) {
259         int plus = ctx.dbSize % NB_CREATION;
260         int nbTx = (ctx.dbSize / NB_CREATION) + (plus > 0 ? 1 : 0);
261         ctx.initOnPrepare(nbTx, fetchOid);
262         perform(NB_THREAD, nbTx, Integer.getInteger(TIMEOUT, 200000).intValue(), ctx);
263     }
264 }
265
266
Popular Tags