KickJava   Java API By Example, From Geeks To Geeks.

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


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.collection.AllCollection;
21 import org.objectweb.util.monolog.api.BasicLevel;
22
23 import javax.jdo.JDOFatalException;
24 import javax.jdo.PersistenceManager;
25
26 /**
27  *
28  * @author M. Guillemin
29  */

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

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

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

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

107     public class ACCtx {
108         /**
109          * The identifier of the created object (see keepOid method)
110          */

111         public Object JavaDoc oids[];
112         /**
113          * The number of persistent object
114          */

115         public int dbSize;
116         /**
117          * indicates if the oid must be kept during the preparation of the task
118          */

119         public boolean keepOidOnPrepare;
120
121         public ACCtx(int dbSize) {
122             this.dbSize = dbSize;
123             oids = new Object JavaDoc[dbSize];
124         }
125
126         public void initOnPrepare(int nbTx, boolean fetchOid) {
127             this.keepOidOnPrepare = fetchOid;
128         }
129
130         public String JavaDoc toString() {
131             return "dbSize = " + dbSize;
132         }
133     }
134 }
135
136 class PrepareTestAllCollection extends StressHelper {
137
138     private final static int NB_CREATION = 100;
139     private final static int NB_THREAD = 4;
140
141     public PrepareTestAllCollection(AllCollectionHelper helper) {
142         super(helper.getName());
143     }
144
145     protected String JavaDoc[] getClassNamesToInit() {
146         return new String JavaDoc[]{AllCollection.class.getName()};
147     }
148
149     protected String JavaDoc getLoggerName() {
150         return STRESS_LOG_NAME + ".AllCollectionHelper";
151     }
152
153     protected String JavaDoc getLogPrefix() {
154         return super.getLogPrefix() + "\t";
155     }
156
157     protected void perform(StressHelper.Task task,
158                            int threadId,
159                            int txId,
160                            Object JavaDoc ctx,
161                            PerformResult res) {
162         AllCollectionHelper.ACCtx pctx = (AllCollectionHelper.ACCtx) ctx;
163         int plus = pctx.dbSize % NB_CREATION;
164         int nbTx = task.txToExecute.length;
165         PersistenceManager pm = getPM(task, threadId, txId);
166         try {
167             res.beginTest();
168             beginTx(pm, task, threadId, txId);
169             AllCollection ac=null;
170             
171             if (plus > 0) {
172                 //The first transaction creates the additional object
173
for (int oid = 0; txId == 0 && oid < plus; oid++) {
174                     long oids = (oid * 6);
175                     //long[] ls = new long[]{oids, oids + 1, oids + 2, oids + 3, oids + 4, oids + 5};
176
long[] ls = new long[]{oids, oids + 1};
177                     ac = new AllCollection("stressCollection" + oid);
178                     ac.setLongs(ls);
179                     ac.setRefs(new Object JavaDoc[]{ac});
180                     logger.log(BasicLevel.DEBUG, "make persistent ac="+ac.getId());
181                     pm.makePersistent(ac);
182                     if (pctx.keepOidOnPrepare) {
183                         pctx.oids[oid] = pm.getObjectId(ac);
184                     }
185                 }
186             }
187             // The other transactions create 'nbCreation' objects
188
for (int no = 0; no < NB_CREATION; no++) {
189                 int oid = (txId * NB_CREATION) + no + plus;
190                 if (oid < pctx.dbSize) {
191                     long oids = (oid * 6);
192                     //long[] ls = new long[]{oids, oids + 1, oids + 2, oids + 3, oids + 4, oids + 5};
193
long[] ls = new long[]{oids, oids + 1};
194                     ac = new AllCollection("stressCollection" + oid);
195                     ac.setLongs(ls);
196                     ac.setRefs(new Object JavaDoc[]{ac});
197                     logger.log(BasicLevel.DEBUG, "make persistent ac="+ac.getId());
198                     pm.makePersistent(ac);
199                     if (pctx.keepOidOnPrepare) {
200                         pctx.oids[oid] = pm.getObjectId(ac);
201                     }
202                 }
203             }
204
205             commitTx(pm, task, threadId, txId);
206             res.endTest();
207         } catch (JDOFatalException e) {
208             rollbackOnException(pm, e, res, task, threadId, txId);
209         } catch (Throwable JavaDoc e) {
210             stopOnError(pm, e, res, task, threadId, txId);
211         } finally {
212             closePM(pm, threadId, txId, task, res);
213         }
214
215     }
216
217     public void prepare(AllCollectionHelper.ACCtx ctx, boolean fetchOid) {
218         int plus = ctx.dbSize % NB_CREATION;
219         int nbTx = (ctx.dbSize / NB_CREATION) + (plus > 0 ? 1 : 0);
220         ctx.initOnPrepare(nbTx, fetchOid);
221         perform(NB_THREAD, nbTx, Integer.getInteger(TIMEOUT, 200000).intValue(), ctx);
222     }
223 }
224
225
Popular Tags