1 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 30 public abstract class IntUserIdHelper 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 public class IUICtx { 48 public Object 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 toString() { 58 return "dbSize = " + dbSize; 59 } 60 } 61 62 public IntUserIdHelper(String s) { 63 super(s); 64 } 65 66 71 protected boolean keepOid() { 72 return true; 73 } 74 75 protected String [] getClassNamesToInit() { 76 return new String []{IntUserId.class.getName()}; 77 } 78 79 83 public void setUp() throws Exception { 84 logger.log(BasicLevel.DEBUG, "setUp."); 85 cleanup(); 86 initDataStructure(false); 87 } 88 89 protected void prepareTask(Task task, Object _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 [ctx.dbSize]; 97 logger.log(BasicLevel.INFO, "\tPreparing test..."); 98 PrepareTest prep = new PrepareTest(this); 100 try { 101 prep.setUp(); 102 } catch (Exception 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 oids = ctx.oids; 111 } else { 112 oids = new Object [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 [] getClassNamesToInit() { 132 return new String []{IntUserId.class.getName()}; 133 } 134 135 protected String getLoggerName() { 136 return STRESS_LOG_NAME + ".IntUserIdHelper"; 137 } 138 139 protected String getLogPrefix() { 140 return super.getLogPrefix() + "\t"; 141 } 142 143 147 public void setUp() throws Exception { 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 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 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 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 e) { 191 stopOnError(pm, e, res, task, threadId, txId); 192 } finally { 193 closePM(pm, threadId, txId, task, res); 194 } 195 } 196 197 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 |