1 24 package org.objectweb.jalisto.test.core.single; 25 26 import org.objectweb.jalisto.se.JalistoFactory; 27 import org.objectweb.jalisto.se.api.Session; 28 import org.objectweb.jalisto.se.api.Transaction; 29 import org.objectweb.jalisto.se.api.JalistoProperties; 30 import org.objectweb.jalisto.se.test.data.Book; 31 import org.objectweb.jalisto.se.test.workbench.JalistoTimer; 32 33 import java.io.DataOutput ; 34 import java.io.IOException ; 35 import java.io.RandomAccessFile ; 36 import java.util.ArrayList ; 37 import java.util.Iterator ; 38 import java.util.Random ; 39 40 public class Bench { 41 public static void main(String [] args) { 42 String jalistoPropertiesFilename = args[0]; 43 int nbrAction = Integer.parseInt(args[1]); 44 int nbrExec = Integer.parseInt(args[2]); 45 46 Bench bench = new Bench(jalistoPropertiesFilename, nbrAction, nbrExec); 47 bench.runAll(); 48 } 49 50 public Bench(String propertiesPath, int nbrAction, int nbrExec) { 51 session = JalistoFactory.getSession(propertiesPath); 52 session.openSession(); 53 tx = session.currentTransaction(); 54 session.defineClass(Book.getMetaDescription()); 55 oids = new ArrayList (); 56 random = new Random (); 57 createTimes = new float[cards.length]; 58 deleteTimes = new float[cards.length]; 59 readTimes = new float[cards.length]; 60 this.nbrAction = nbrAction; 61 this.nbrExec = nbrExec; 62 } 63 64 public void runAll() { 65 cleanUp(); 66 for (int i = 0; i < cards.length; i++) { 67 runCycle((new Float (cards[i])).intValue(), nbrAction, nbrExec); 71 JalistoTimer.summary(); 72 createTimes[i] = JalistoTimer.getAverage(timerNameCreate) / nbrAction; 73 readTimes[i] = JalistoTimer.getAverage(timerNameRead) / nbrAction; 74 deleteTimes[i] = JalistoTimer.getAverage(timerNameDelete) / nbrAction; 75 } 76 try { 77 RandomAccessFile out = new RandomAccessFile ("bench-out-" + System.currentTimeMillis() + ".txt", "rw"); 78 printExecutionInfos(out); 79 printResults(out, " ", cards); 80 printResults(out, "create", createTimes); 81 printResults(out, "read", readTimes); 82 printResults(out, "delete", deleteTimes); 83 } catch (IOException e) { 84 e.printStackTrace(); 85 } 86 session.closeSession(); 87 } 88 89 public void printResults(DataOutput out, String message, float[] datas) throws IOException { 90 StringBuffer sb = new StringBuffer (); 91 sb.append(message).append(";"); 92 for (int i = 0; i < datas.length; i++) { 93 sb.append(datas[i]); 94 if (i != datas.length - 1) { 95 sb.append(";"); 96 } 97 } 98 sb.append("\n"); 99 out.writeBytes(sb.toString()); 100 } 101 102 public void printExecutionInfos(DataOutput out) throws IOException { 103 JalistoProperties prop = session.getInternalSession().getProperties(); 104 StringBuffer sb = new StringBuffer (); 105 sb.append("nbrAction;").append(nbrAction).append("\n"); 106 sb.append("nbrExec;").append(nbrExec).append("\n"); 107 sb.append(JalistoProperties.PAGE_CACHE_SIZE_KEY).append(";"); 108 sb.append(prop.getPageCacheSize()).append("\n"); 109 sb.append(JalistoProperties.OBJECT_INST_PAGE_SIZE_KEY).append(";"); 110 sb.append(prop.getInstancePageSize()).append("\n"); 111 sb.append("\n"); 112 out.writeBytes(sb.toString()); 113 } 114 115 public ArrayList getRandomSubCollection(ArrayList list, int card, boolean remove) { 116 ArrayList subCollection = new ArrayList (); 117 int size = list.size(); 118 for (int i = 0; i < card; i++) { 119 if (remove) { 120 subCollection.add(list.remove(random.nextInt(list.size()))); 121 } else { 122 subCollection.add(list.get(random.nextInt(size))); 123 } 124 } 125 return subCollection; 126 } 127 128 129 public void cleanUp() { 130 tx.begin(); 131 Iterator extent = session.getExtent(Book.class).readFully().iterator(); 132 while (extent.hasNext()) { 133 session.deleteObjectByOid(extent.next()); 134 } 135 tx.commit(); 136 } 137 138 public void populate(int nbrBook) { 139 int nbrToCreate = nbrBook - oids.size(); 140 long startTime = System.currentTimeMillis(); 141 tx.begin(); 142 for (int i = 0; i < nbrToCreate; i++) { 143 oids.add(session.createObject(Book.newBook().toArray(), Book.class)); 144 if ((i % 1000) == 0) { 145 tx.commit(); 146 tx.begin(); 147 } 148 } 149 tx.commit(); 150 tx.begin(); 151 tx.commit(); 152 System.out.println("populate time to create " + nbrToCreate + " books : " + 153 (System.currentTimeMillis() - startTime)); 154 } 155 156 public void runCycle(int nbrData, int nbrAction, int nbrExec) { 157 System.out.println(""); 158 JalistoTimer.deleteAllTimers(); 159 160 populate(nbrData); 161 System.out.println("oids collection size = " + oids.size()); 162 163 timerNameCreate = "execution cycle (" + nbrData + "," + nbrAction + "," + nbrExec + ") - create : "; 164 timerNameRead = "execution cycle (" + nbrData + "," + nbrAction + "," + nbrExec + ") - read : "; 165 timerNameDelete = "execution cycle (" + nbrData + "," + nbrAction + "," + nbrExec + ") - delete : "; 166 JalistoTimer.createTimer(timerNameCreate); 167 JalistoTimer.createTimer(timerNameRead); 168 JalistoTimer.createTimer(timerNameDelete); 169 170 for (int i = 0; i < nbrExec; i++) { 171 177 JalistoTimer.timerStart(timerNameCreate); 178 tx.begin(); 179 for (int j = 0; j < nbrAction; j++) { 180 Object [] bookInArray = Book.newBook().toArray(); 181 Object oid = session.createObject(bookInArray, Book.class); 182 oids.add(oid); 183 } 184 tx.commit(); 185 tx.begin(); 186 tx.commit(); 187 JalistoTimer.timerStop(timerNameCreate, false); 188 189 ArrayList subCollectionRead = getRandomSubCollection(oids, nbrAction, false); 190 JalistoTimer.timerStart(timerNameRead); 191 tx.begin(); 192 for (int j = 0; j < nbrAction; j++) { 193 session.readObjectByOid(subCollectionRead.get(j)); 194 if ((i == nbrExec - 1) && (j == nbrAction - 1)) { 195 System.out.println("POUET"); 196 } 197 } 198 tx.commit(); 199 tx.begin(); 200 tx.commit(); 201 JalistoTimer.timerStop(timerNameRead, false); 202 203 ArrayList subCollectionDelete = getRandomSubCollection(oids, nbrAction, true); 204 JalistoTimer.timerStart(timerNameDelete); 205 tx.begin(); 206 for (int j = 0; j < nbrAction; j++) { 207 session.deleteObjectByOid(subCollectionDelete.get(j)); 208 } 209 tx.commit(); 210 tx.begin(); 211 tx.commit(); 212 JalistoTimer.timerStop(timerNameDelete, false); 213 } 214 } 215 216 217 private String timerNameCreate; 218 private String timerNameRead; 219 private String timerNameDelete; 220 221 private int nbrAction; 222 private int nbrExec; 223 224 private float[] createTimes; 225 private float[] readTimes; 226 private float[] deleteTimes; 227 228 private Session session; 229 private Transaction tx; 230 private ArrayList oids; 231 232 private Random random; 233 234 235 private static final float[] cards = {1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000}; 240 242 } 243 | Popular Tags |