KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jalisto > test > core > single > Bench


1 /*
2  * Jalisto - JAva LIght STOrage
3  * Copyright (C) 2000-2005 Xcalia http://www.xcalia.com
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * Xcalia
20  * 71, rue Desnouettes
21  * 75014 Paris - France
22  * http://www.xcalia.com
23  */

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 JavaDoc;
34 import java.io.IOException JavaDoc;
35 import java.io.RandomAccessFile JavaDoc;
36 import java.util.ArrayList JavaDoc;
37 import java.util.Iterator JavaDoc;
38 import java.util.Random JavaDoc;
39
40 public class Bench {
41     public static void main(String JavaDoc[] args) {
42         String JavaDoc 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 JavaDoc 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 JavaDoc();
56         random = new Random JavaDoc();
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 // session.closeSession();
68
// session.reorganize();
69
// session.openSession();
70
runCycle((new Float JavaDoc(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 JavaDoc out = new RandomAccessFile JavaDoc("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 JavaDoc e) {
84             e.printStackTrace();
85         }
86         session.closeSession();
87     }
88
89     public void printResults(DataOutput JavaDoc out, String JavaDoc message, float[] datas) throws IOException JavaDoc {
90         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
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 JavaDoc out) throws IOException JavaDoc {
103         JalistoProperties prop = session.getInternalSession().getProperties();
104         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
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 JavaDoc getRandomSubCollection(ArrayList JavaDoc list, int card, boolean remove) {
116         ArrayList JavaDoc subCollection = new ArrayList JavaDoc();
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 JavaDoc 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 // if ((i % 50) == 0) {
172
// session.closeSession();
173
// session.reorganize();
174
// session.openSession();
175
// }
176

177             JalistoTimer.timerStart(timerNameCreate);
178             tx.begin();
179             for (int j = 0; j < nbrAction; j++) {
180                 Object JavaDoc[] bookInArray = Book.newBook().toArray();
181                 Object JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc timerNameCreate;
218     private String JavaDoc timerNameRead;
219     private String JavaDoc 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 JavaDoc oids;
231
232     private Random JavaDoc random;
233
234
235 // private static final float[] cards = {1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000,
236
// 11000, 12000, 13000, 14000, 15000, 16000, 17000, 18000,
237
// 19000, 20000, 25000, 30000, 35000, 40000, 45000, 50000,
238
// 75000, 100000, 125000, 150000, 175000, 200000};
239
private static final float[] cards = {1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000};
240 // private static final float[] cards = {1000, 2000, 3000, 4000, 5000};
241

242 }
243
Popular Tags