KickJava   Java API By Example, From Geeks To Geeks.

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


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.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 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 BenchRemote {
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         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 JavaDoc 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 JavaDoc();
57         random = new Random JavaDoc();
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 JavaDoc out = new RandomAccessFile JavaDoc("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 JavaDoc e) {
81             e.printStackTrace();
82         }
83         session.closeSession();
84     }
85
86     public void printResults(DataOutput JavaDoc out, String JavaDoc message, float[] datas) throws IOException JavaDoc {
87         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
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 JavaDoc out) throws IOException JavaDoc {
100         JalistoProperties prop = session.getInternalSession().getProperties();
101         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
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 JavaDoc getRandomSubCollection(ArrayList JavaDoc list, int card, boolean remove) {
113         ArrayList JavaDoc subCollection = new ArrayList JavaDoc();
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 JavaDoc 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 JavaDoc[] bookInArray = Book.newBook().toArray();
153                 Object JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc timerNameCreate;
185     private String JavaDoc timerNameRead;
186     private String JavaDoc 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 JavaDoc oids;
198
199     private Random JavaDoc random;
200 }
201
Popular Tags