1 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 32 public abstract class CDRelationHelper extends StressHelper { 33 34 45 46 protected static Object [] oidsC = null; 47 protected static Object [] oidsD = null; 48 protected String DBSIZE = getLoggerName() + ".dbsize"; 49 protected String NO_DB_INIT = getLoggerName() + ".nodbinit"; 50 51 52 public CDRelationHelper(String s) { 53 super(s); 54 } 55 56 protected String [] getClassNamesToInit() { 57 return new String []{ 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 73 public void setUp() throws Exception { 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 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 95 protected void prepareTask(Task task, Object _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 logger.log(BasicLevel.INFO, "\tPreparing test..."); 103 new PrepareTestCDRelation(this) 104 .prepare(ctx, keepOid()); 105 if (keepOid()) { 106 oidsC = ctx.oidsC; 108 oidsD = ctx.oidsD; 109 } else { 110 oidsC = new Object [0]; 112 oidsD = new Object [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 146 public class CDCtx { 147 150 public Object oidsC[]; 151 public Object oidsD[]; 152 public boolean usedOids[]; 153 156 public int dbSize; 157 160 public boolean keepOid; 161 162 163 public CDCtx(int dbSize) { 164 this.dbSize = dbSize; 165 oidsC = new Object [dbSize]; 166 oidsD = new Object [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 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 [] getClassNamesToInit() { 192 return new String []{ 193 org.objectweb.speedo.pobjects.relations.C.class.getName(), 194 org.objectweb.speedo.pobjects.relations.D.class.getName() 195 }; 196 } 197 198 protected String getLoggerName() { 199 return STRESS_LOG_NAME + ".CDRelationHelper"; 200 } 201 202 protected String getLogPrefix() { 203 return super.getLogPrefix() + "\t"; 204 } 205 206 protected void perform(StressHelper.Task task, 207 int threadId, 208 int txId, 209 Object 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 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 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 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 |