KickJava   Java API By Example, From Geeks To Geeks.

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


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

30 public abstract class IntUserIdHelper 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     public class IUICtx {
48         public Object JavaDoc oids[];
49         public int dbSize;
50         public boolean keepOid;
51
52         public IUICtx(int dbSize) {
53             this.dbSize = dbSize;
54             keepOid = false;
55         }
56
57         public String JavaDoc toString() {
58             return "dbSize = " + dbSize;
59         }
60     }
61
62     public IntUserIdHelper(String JavaDoc s) {
63         super(s);
64     }
65
66     /**
67      * Default implementation return true.
68      * @return true if the oid of the created object must be kept into the
69      * context
70      */

71     protected boolean keepOid() {
72         return true;
73     }
74
75     protected String JavaDoc[] getClassNamesToInit() {
76         return new String JavaDoc[]{IntUserId.class.getName()};
77     }
78
79     /**
80      * IMPORTANT: dot not removed data on support in order to avoid next
81      * creations
82      */

83     public void setUp() throws Exception JavaDoc {
84         logger.log(BasicLevel.DEBUG, "setUp.");
85         cleanup();
86         initDataStructure(false);
87     }
88
89     protected void prepareTask(Task task, Object JavaDoc _ctx) {
90         debug = logger.isLoggable(BasicLevel.DEBUG);
91         super.prepareTask(task, _ctx);
92         IUICtx ctx = (IUICtx) _ctx;
93         if (oids == null) {
94             synchronized (getClass()) {
95                 if (oids == null && !Boolean.getBoolean(NO_DB_INIT)) {
96                     ctx.oids = new Object JavaDoc[ctx.dbSize];
97                     logger.log(BasicLevel.INFO, "\tPreparing test...");
98                     //Initialisation the database
99
PrepareTest prep = new PrepareTest(this);
100                     try {
101                         prep.setUp();
102                     } catch (Exception JavaDoc e) {
103                         fail(e.getMessage());
104                     }
105                     prep.prepare(ctx, keepOid());
106                     prep.tearDown();
107                     logger.log(BasicLevel.INFO, "\ttest Prepared");
108                     if (keepOid()) {
109                         //keep oids in the static variable
110
oids = ctx.oids;
111                     } else {
112                         //db initialized without oids
113
oids = new Object JavaDoc[0];
114                     }
115                 }
116             }
117         }
118         ctx.oids = oids;
119     }
120 }
121
122 class PrepareTest extends StressHelper {
123
124     private final static int NB_CREATION = 100;
125     private final static int NB_THREAD = 4;
126
127     public PrepareTest(IntUserIdHelper helper) {
128         super(helper.getName());
129     }
130
131     protected String JavaDoc[] getClassNamesToInit() {
132         return new String JavaDoc[]{IntUserId.class.getName()};
133     }
134
135     protected String JavaDoc getLoggerName() {
136         return STRESS_LOG_NAME + ".IntUserIdHelper";
137     }
138
139     protected String JavaDoc getLogPrefix() {
140         return super.getLogPrefix() + "\t";
141     }
142
143     /**
144      * IMPORTANT: dot not removed data on support in order to avoid next
145      * creations
146      */

147     public void setUp() throws Exception JavaDoc {
148         logger.log(BasicLevel.DEBUG, "setUp.");
149         cleanup();
150         initDataStructure(false);
151     }
152
153     protected void perform(StressHelper.Task task,
154                            int threadId,
155                            int txId,
156                            Object JavaDoc ctx,
157                            PerformResult res) {
158         IntUserIdHelper.IUICtx gctx = (IntUserIdHelper.IUICtx) ctx;
159         final int plus = gctx.dbSize % NB_CREATION;
160         PersistenceManager pm = getPM(task, threadId, txId);
161         try {
162             res.beginTest();
163             beginTx(pm, task, threadId, txId);
164             if (plus > 0) {
165                 //The first transaction creates the additional object
166
for (int oid = 0; txId == 0 && oid < plus; oid++) {
167                     IntUserId iui = new IntUserId(oid, "Obj No " + oid);
168                     pm.makePersistent(iui);
169                     if (gctx.keepOid) {
170                         gctx.oids[oid] = pm.getObjectId(iui);
171                     }
172                 }
173             }
174             // The other transactions create 'nbCreation' objects
175
for (int no = 0; no < NB_CREATION; no++) {
176                 int oid = (txId * NB_CREATION) + no + plus;
177                 if (oid < gctx.dbSize) {
178                     IntUserId iui = new IntUserId(oid, "Obj No " + oid);
179                     pm.makePersistent(iui);
180                     if (gctx.keepOid) {
181                         gctx.oids[oid] = pm.getObjectId(iui);
182                     }
183                 }
184             }
185
186             commitTx(pm, task, threadId, txId);
187             res.endTest();
188         } catch (JDOFatalException e) {
189             rollbackOnException(pm, e, res, task, threadId, txId);
190         } catch (Throwable JavaDoc e) {
191             stopOnError(pm, e, res, task, threadId, txId);
192         } finally {
193             closePM(pm, threadId, txId, task, res);
194         }
195     }
196
197     /**
198      * Tests the concurrency of several writer on a same persistent object.
199      * (500 users)
200      */

201     public void prepare(IntUserIdHelper.IUICtx ctx, boolean keepOid) {
202         int plus = ctx.dbSize % NB_CREATION;
203         int nbTx = (ctx.dbSize / NB_CREATION) + (plus > 0 ? 1 : 0);
204         ctx.keepOid = keepOid;
205         perform(NB_THREAD, nbTx, Integer.getInteger(TIMEOUT, 200000).intValue(), ctx);
206     }
207 }
208
209
Popular Tags