1 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 30 public abstract class AllCollectionHelper extends StressHelper { 31 32 43 protected static Object [] oids = null; 44 protected String DBSIZE = getLoggerName() + ".dbsize"; 45 protected String NO_DB_INIT = getLoggerName() + ".nodbinit"; 46 47 48 public AllCollectionHelper(String s) { 49 super(s); 50 } 51 52 protected String [] getClassNamesToInit() { 53 return new String []{AllCollection.class.getName()}; 54 } 55 56 protected boolean linkObject() { 57 return true; 58 } 59 60 protected boolean keepOid() { 61 return true; 62 } 63 64 68 public void setUp() throws Exception { 69 logger.log(BasicLevel.DEBUG, "setUp."); 70 cleanup(); 71 initDataStructure(false); 72 debug = logger.isLoggable(BasicLevel.DEBUG); 73 } 74 75 80 protected void prepareTask(Task task, Object _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 logger.log(BasicLevel.INFO, "\tPreparing test..."); 88 new PrepareTestAllCollection(this) 89 .prepare(ctx, keepOid()); 90 if (keepOid()) { 91 oids = ctx.oids; 93 } else { 94 oids = new Object [0]; 96 } 97 logger.log(BasicLevel.INFO, "\tTest Prepared."); 98 } 99 } 100 } 101 ctx.oids = oids; 102 } 103 104 107 public class ACCtx { 108 111 public Object oids[]; 112 115 public int dbSize; 116 119 public boolean keepOidOnPrepare; 120 121 public ACCtx(int dbSize) { 122 this.dbSize = dbSize; 123 oids = new Object [dbSize]; 124 } 125 126 public void initOnPrepare(int nbTx, boolean fetchOid) { 127 this.keepOidOnPrepare = fetchOid; 128 } 129 130 public String 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 [] getClassNamesToInit() { 146 return new String []{AllCollection.class.getName()}; 147 } 148 149 protected String getLoggerName() { 150 return STRESS_LOG_NAME + ".AllCollectionHelper"; 151 } 152 153 protected String getLogPrefix() { 154 return super.getLogPrefix() + "\t"; 155 } 156 157 protected void perform(StressHelper.Task task, 158 int threadId, 159 int txId, 160 Object 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 for (int oid = 0; txId == 0 && oid < plus; oid++) { 174 long oids = (oid * 6); 175 long[] ls = new long[]{oids, oids + 1}; 177 ac = new AllCollection("stressCollection" + oid); 178 ac.setLongs(ls); 179 ac.setRefs(new Object []{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 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}; 194 ac = new AllCollection("stressCollection" + oid); 195 ac.setLongs(ls); 196 ac.setRefs(new Object []{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 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 |