KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jalisto > se > test > threads > BookClientBasic


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.se.test.threads;
25
26 import org.objectweb.jalisto.se.test.data.Book;
27 import org.objectweb.jalisto.se.test.workbench.JalistoTimer;
28
29 import java.util.ArrayList JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Random JavaDoc;
32
33 public class BookClientBasic extends BookClientThread {
34
35     public BookClientBasic(String JavaDoc jalistoPropertiesFilename, int numClient,
36                            int nbrBook, int nbrAction, int nbrIteration) {
37         super(jalistoPropertiesFilename, numClient, nbrBook, nbrAction, nbrIteration);
38     }
39
40
41     public void stabilityTest() {
42         System.out.println("BookClientBasic " + numClient + " starts stability test");
43
44         String JavaDoc timerNameCreate = "BookClientBasic - create " + nbrCreate + " - " + numClient;
45         String JavaDoc timerNameRead = "BookClientBasic - read " + nbrRead + " - " + numClient;
46         String JavaDoc timerNameUpdate = "BookClientBasic - update " + nbrUpdate + " - " + numClient;
47         String JavaDoc timerNameDelete = "BookClientBasic - delete " + nbrDelete + " - " + numClient;
48         if (withTimer) {
49             if (nbrCreate > 0) {
50                 JalistoTimer.createTimer(timerNameCreate);
51             }
52             if (nbrDelete > 0) {
53                 JalistoTimer.createTimer(timerNameDelete);
54             }
55             if (nbrRead > 0) {
56                 JalistoTimer.createTimer(timerNameRead);
57             }
58             if (nbrUpdate > 0) {
59                 JalistoTimer.createTimer(timerNameUpdate);
60             }
61         }
62
63         Random JavaDoc random = new Random JavaDoc();
64
65         for (int c = 0; ((nbrIteration < 0) || (c < nbrIteration)) && continueWorking; c++) {
66             if (withTimer && (c != 0) && ((c % 50) == 0)) {
67                 System.out.println("");
68                 if (nbrCreate > 0) {
69                     JalistoTimer.summary(timerNameCreate);
70                     JalistoTimer.clean(timerNameCreate);
71                 }
72                 if (nbrRead > 0) {
73                     JalistoTimer.summary(timerNameRead);
74                     JalistoTimer.clean(timerNameRead);
75                 }
76                 if (nbrUpdate > 0) {
77                     JalistoTimer.summary(timerNameUpdate);
78                     JalistoTimer.clean(timerNameUpdate);
79                 }
80                 if (nbrDelete > 0) {
81                     JalistoTimer.summary(timerNameDelete);
82                     JalistoTimer.clean(timerNameDelete);
83                 }
84             }
85
86             if (withExtent) {
87                 tx.begin();
88                 oids = new ArrayList JavaDoc();
89                 oids.addAll(session.getExtent(Book.class).readFully().collection());
90                 tx.commit();
91             }
92
93             if (nbrCreate > 0) {
94                 if (withTimer) {
95                     JalistoTimer.timerStart(timerNameCreate);
96                 }
97                 List JavaDoc newOids = new ArrayList JavaDoc();
98                 tx.begin();
99                 for (int i = 0; i < nbrCreate; i++) {
100                     Object JavaDoc[] bookInArray = Book.newBook().toArray();
101                     Object JavaDoc oid = session.createObject(bookInArray, Book.class);
102                     newOids.add(oid);
103                 }
104                 tx.commit();
105                 if (withTimer) {
106                     JalistoTimer.timerStop(timerNameCreate, false);
107                 }
108                 synchronized (oids) {
109                     oids.addAll(newOids);
110                 }
111             }
112
113             if (nbrRead > 0) {
114                 if (withTimer) {
115                     JalistoTimer.timerStart(timerNameRead);
116                 }
117                 tx.begin();
118                 Object JavaDoc oid;
119                 for (int i = 0; i < nbrRead; i++) {
120                     synchronized (oids) {
121                         int index = random.nextInt(oids.size());
122                         oid = oids.get(index);
123                         session.readObjectByOid(oid);
124                     }
125                 }
126                 tx.commit();
127                 if (withTimer) {
128                     JalistoTimer.timerStop(timerNameRead, false);
129                 }
130             }
131
132             if (nbrUpdate > 0) {
133                 if (withTimer) {
134                     JalistoTimer.timerStart(timerNameUpdate);
135                 }
136                 tx.begin();
137                 Object JavaDoc oid;
138                 for (int i = 0; i < nbrUpdate; i++) {
139                     synchronized (oids) {
140                         int index = random.nextInt(oids.size());
141                         oid = oids.get(index);
142                         Object JavaDoc[] book = session.readObjectByOid(oid);
143                         book[0] = book[0] + "u_" + numClient;
144                         session.updateObjectByOid(oid, book);
145                     }
146                 }
147                 tx.commit();
148                 if (withTimer) {
149                     JalistoTimer.timerStop(timerNameUpdate, false);
150                 }
151             }
152
153             if (nbrDelete > 0) {
154                 if (withTimer) {
155                     JalistoTimer.timerStart(timerNameDelete);
156                 }
157                 tx.begin();
158                 Object JavaDoc oid;
159                 for (int i = 0; i < nbrDelete; i++) {
160                     synchronized (oids) {
161                         int index = random.nextInt(oids.size());
162                         oid = oids.remove(index);
163                     }
164                     session.deleteObjectByOid(oid);
165                 }
166                 tx.commit();
167                 if (withTimer) {
168                     JalistoTimer.timerStop(timerNameDelete, false);
169                 }
170             }
171         }
172
173         System.out.println("BookClientBasic " + numClient + " ends stability test");
174     }
175
176
177 }
178
Popular Tags