1 24 package org.objectweb.jalisto.test.core.single; 25 26 import org.objectweb.jalisto.se.JalistoFactory; 27 import org.objectweb.jalisto.se.api.Transaction; 28 import org.objectweb.jalisto.se.api.Session; 29 import org.objectweb.jalisto.se.api.*; 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 BenchRemote { 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 int nbrMeasure = Integer.parseInt(args[3]); 46 47 BenchRemote bench = new BenchRemote(jalistoPropertiesFilename, nbrAction, nbrExec, nbrMeasure); 48 bench.runAll(); 49 } 50 51 public BenchRemote(String propertiesPath, int nbrAction, int nbrExec, int nbrMeasure) { 52 session = JalistoFactory.getSession(propertiesPath); 53 session.openSession(); 54 tx = session.currentTransaction(); 55 session.defineClass(Book.getMetaDescription()); 56 oids = new ArrayList (); 57 random = new Random (); 58 createTimes = new float[nbrMeasure]; 59 deleteTimes = new float[nbrMeasure]; 60 readTimes = new float[nbrMeasure]; 61 this.nbrAction = nbrAction; 62 this.nbrExec = nbrExec; 63 } 64 65 public void runAll() { 66 readOids(); 67 for (int i = 0; i < createTimes.length; i++) { 68 runCycle(); 69 JalistoTimer.summary(); 70 createTimes[i] = JalistoTimer.getAverage(timerNameCreate) / nbrAction; 71 readTimes[i] = JalistoTimer.getAverage(timerNameRead) / nbrAction; 72 deleteTimes[i] = JalistoTimer.getAverage(timerNameDelete) / nbrAction; 73 } 74 try { 75 RandomAccessFile out = new RandomAccessFile ("bench-out-" + System.currentTimeMillis() + ".txt", "rw"); 76 printExecutionInfos(out); 77 printResults(out, "create", createTimes); 78 printResults(out, "read", readTimes); 79 printResults(out, "delete", deleteTimes); 80 } catch (IOException e) { 81 e.printStackTrace(); 82 } 83 session.closeSession(); 84 } 85 86 public void printResults(DataOutput out, String message, float[] datas) throws IOException { 87 StringBuffer sb = new StringBuffer (); 88 sb.append(message).append(";"); 89 for (int i = 0; i < datas.length; i++) { 90 sb.append(datas[i]); 91 if (i != datas.length - 1) { 92 sb.append(";"); 93 } 94 } 95 sb.append("\n"); 96 out.writeBytes(sb.toString()); 97 } 98 99 public void printExecutionInfos(DataOutput out) throws IOException { 100 JalistoProperties prop = session.getInternalSession().getProperties(); 101 StringBuffer sb = new StringBuffer (); 102 sb.append("nbrAction;").append(nbrAction).append("\n"); 103 sb.append("nbrExec;").append(nbrExec).append("\n"); 104 sb.append(JalistoProperties.PAGE_CACHE_SIZE_KEY).append(";"); 105 sb.append(prop.getPageCacheSize()).append("\n"); 106 sb.append(JalistoProperties.OBJECT_INST_PAGE_SIZE_KEY).append(";"); 107 sb.append(prop.getInstancePageSize()).append("\n"); 108 sb.append("\n"); 109 out.writeBytes(sb.toString()); 110 } 111 112 public ArrayList getRandomSubCollection(ArrayList list, int card, boolean remove) { 113 ArrayList subCollection = new ArrayList (); 114 int size = list.size(); 115 for (int i = 0; i < card; i++) { 116 if (remove) { 117 subCollection.add(list.remove(random.nextInt(list.size()))); 118 } else { 119 subCollection.add(list.get(random.nextInt(size))); 120 } 121 } 122 return subCollection; 123 } 124 125 126 public void readOids() { 127 tx.begin(); 128 Iterator extent = session.getExtent(Book.class).readFully().iterator(); 129 while (extent.hasNext()) { 130 oids.add(extent.next()); 131 } 132 tx.commit(); 133 } 134 135 public void runCycle() { 136 System.out.println(""); 137 JalistoTimer.deleteAllTimers(); 138 139 System.out.println("oids collection size = " + oids.size()); 140 141 timerNameCreate = "execution cycle (" + nbrAction + "," + nbrExec + ") - create : "; 142 timerNameRead = "execution cycle (" + nbrAction + "," + nbrExec + ") - read : "; 143 timerNameDelete = "execution cycle (" + nbrAction + "," + nbrExec + ") - delete : "; 144 JalistoTimer.createTimer(timerNameCreate); 145 JalistoTimer.createTimer(timerNameRead); 146 JalistoTimer.createTimer(timerNameDelete); 147 148 for (int i = 0; i < nbrExec; i++) { 149 JalistoTimer.timerStart(timerNameCreate); 150 tx.begin(); 151 for (int j = 0; j < nbrAction; j++) { 152 Object [] bookInArray = Book.newBook().toArray(); 153 Object oid = session.createObject(bookInArray, Book.class); 154 oids.add(oid); 155 } 156 tx.commit(); 157 tx.begin(); 158 tx.commit(); 159 JalistoTimer.timerStop(timerNameCreate, false); 160 161 ArrayList subCollectionRead = getRandomSubCollection(oids, nbrAction, false); 162 JalistoTimer.timerStart(timerNameRead); 163 tx.begin(); 164 session.readObjectsByOids(subCollectionRead); 165 tx.commit(); 166 tx.begin(); 167 tx.commit(); 168 JalistoTimer.timerStop(timerNameRead, false); 169 170 ArrayList subCollectionDelete = getRandomSubCollection(oids, nbrAction, true); 171 JalistoTimer.timerStart(timerNameDelete); 172 tx.begin(); 173 for (int j = 0; j < nbrAction; j++) { 174 session.deleteObjectByOid(subCollectionDelete.get(j)); 175 } 176 tx.commit(); 177 tx.begin(); 178 tx.commit(); 179 JalistoTimer.timerStop(timerNameDelete, false); 180 } 181 } 182 183 184 private String timerNameCreate; 185 private String timerNameRead; 186 private String timerNameDelete; 187 188 private int nbrAction; 189 private int nbrExec; 190 191 private float[] createTimes; 192 private float[] readTimes; 193 private float[] deleteTimes; 194 195 private Session session; 196 private Transaction tx; 197 private ArrayList oids; 198 199 private Random random; 200 } 201 | Popular Tags |