1 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 32 public abstract class LinkedIntUserIdHelper extends StressHelper { 33 34 45 protected static Object [] oids = null; 46 protected String DBSIZE = getLoggerName() + ".dbsize"; 47 protected String NO_DB_INIT = getLoggerName() + ".nodbinit"; 48 49 50 public LinkedIntUserIdHelper(String s) { 51 super(s); 52 } 53 54 protected String [] getClassNamesToInit() { 55 return new String []{LinkedIntUserId.class.getName()}; 56 } 57 58 protected boolean linkObject() { 59 return true; 60 } 61 62 protected boolean keepOid() { 63 return true; 64 } 65 66 70 public void setUp() throws Exception { 71 logger.log(BasicLevel.DEBUG, "LinkedIntUserIdHelper.setUp"); 72 cleanup(); 73 initDataStructure(false); 74 } 75 76 81 protected void prepareTask(Task task, Object _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 logger.log(BasicLevel.INFO, "\tPreparing test..."); 91 new PrepareTestLinked(this) 92 .prepare(ctx, keepOid(), linkObject()); 93 if (keepOid()) { 94 oids = ctx.oids; 96 } else { 97 oids = new Object [0]; 99 } 100 logger.log(BasicLevel.INFO, "\tTest Prepared."); 101 } 102 } 103 } 104 ctx.oids = oids; 105 106 } 107 108 109 112 public class LIUICtx { 113 116 public Object oids[]; 117 120 public int dbSize; 121 124 public boolean keepOidOnPrepare; 125 public boolean linkOnPrepare; 126 public boolean endPrepare; 127 public Object [] currentsOnPrepare; 128 public Object [] nextsOnPrepare; 129 130 public LIUICtx(int dbSize) { 131 this.dbSize = dbSize; 132 oids = new Object [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 [nbTx]; 140 currentsOnPrepare = new Object [nbTx]; 141 } 142 143 public void endOnPrepare(int nbTx, boolean fetchOid, boolean linkOnPrepare) { 144 this.endPrepare = true; 145 } 146 147 public String 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 [] getClassNamesToInit() { 163 return new String []{LinkedIntUserId.class.getName()}; 164 } 165 166 protected String getLoggerName() { 167 return STRESS_LOG_NAME + ".LinkedIntUserIdHelper"; 168 } 169 170 protected String getLogPrefix() { 171 return super.getLogPrefix() + "\t"; 172 } 173 174 protected void perform(StressHelper.Task task, 175 int threadId, 176 int txId, 177 Object 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 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 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 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 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 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 pctx.nextsOnPrepare[txId] = pm.getObjectId(next); 270 271 } else if (txId == nbTx - 1){ 273 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 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 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 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 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 |