KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > BLOBBench


1 // $Id: BLOBBench.java,v 1.2 2003/11/23 14:16:53 per_nyfelt Exp $
2

3 import java.util.*;
4 import java.util.zip.*;
5
6 import org.ozoneDB.*;
7 import org.ozoneDB.blob.*;
8
9
10 class BLOBBench extends Thread JavaDoc {
11     
12     final static int POOLSIZE = 10;
13     final static int BUFFERSIZE = 32000;
14     final static int PAGESIZE = 32;
15     final static int MINSIZE = 1;
16     final static int MAXSIZE = 1024;
17     
18     int pool;
19     int bsize;
20     int page;
21     int min;
22     int max;
23     
24     String JavaDoc[] names;
25     int[] sizes;
26     byte[] buffer;
27     long[] check;
28     RemoteDatabase db;
29     
30     Random objNr;
31     Random objSize;
32     
33     boolean halted = false;
34     
35     long movement = 0;
36     long avWritespeed = 0;
37     int writecount = 0;
38     long avReadspeed = 0;
39     int readcount = 0;
40     
41     
42     /** Constructor */
43     public BLOBBench( RemoteDatabase _db, int _poolsize, int _buffersize, int _pagesize, int _rangeMin,
44             int _rangeMax ) {
45         
46         pool = _poolsize;
47         bsize = _buffersize;
48         page = _pagesize;
49         min = _rangeMin;
50         max = _rangeMax;
51         
52         names = new String JavaDoc[pool];
53         sizes = new int[pool];
54         check = new long[pool];
55         buffer = new byte[_buffersize];
56         db = _db;
57         
58         objNr = new Random();
59         objSize = new Random();
60     }
61     
62     
63     /** RUN THE THREAD */
64     public void run() {
65         
66         while (true) {
67             
68             int actObj = objNr.nextInt() % pool;
69             actObj = actObj < 0 ? -actObj : actObj;
70             
71             try {
72                 if (names[actObj] == null) {
73                     // at this place there isnt an object yet -> create one
74
if (halted) {
75                         break;
76                     }
77                     createBlob( actObj );
78                 } else {
79                     // there is one -> try to find it
80
BLOBContainer blob = (BLOBContainer)db.objectForName( names[actObj] );
81                     if (blob != null) {
82                         // read and verify it
83
if (halted) {
84                             break;
85                         }
86                         verifyBlob( blob, actObj );
87                         // delete it
88
db.deleteObject( blob );
89                         names[actObj] = null;
90                         sizes[actObj] = 0;
91                         check[actObj] = 0;
92                         System.out.println( " -> deleted." );
93                         // create a new one
94
if (halted) {
95                             break;
96                         }
97                         createBlob( actObj );
98                     } else {
99                         System.out.println( "\nERROR!!! Object for name ´" + names[actObj] + "´ not found!\n" );
100                     }
101                 }
102             
103             } catch (Throwable JavaDoc e) {
104                 e.printStackTrace( System.out );
105             }
106         }
107         
108         try {
109             System.out.println( "\n\nclearing database..." );
110             for (int i = 0; i < pool; i++) {
111                 if (names[i] != null) {
112                     BLOBContainer blob = (BLOBContainer)db.objectForName( names[i] );
113                     db.deleteObject( blob );
114                     System.out.println( names[i] + " deleted." );
115                 }
116             }
117         
118         } catch (Throwable JavaDoc e) {
119             e.printStackTrace( System.out );
120         }
121     
122     }
123     
124     
125     /** */
126     private void createBlob( int _actObj ) throws Exception JavaDoc {
127         
128         int size = objSize.nextInt() % (max - min);
129         size = (size < 0 ? -size : size) + min;
130         size *= 1024;
131         
132         String JavaDoc name = new String JavaDoc( "Blob" + String.valueOf( _actObj ) );
133         System.out.print( "create " + name + "..." );
134         BLOBContainer blob = (BLOBContainer)db.createObject( BLOBContainerImpl.class.getName(), 0, name );
135         blob.init( page );
136         names[_actObj] = name;
137         sizes[_actObj] = size;
138         
139         // fill it up...
140
BLOBOutputStream out = new BLOBOutputStream( blob );
141         CRC32 checksum = new CRC32();
142         CheckedOutputStream checkedOut = new CheckedOutputStream( out, checksum );
143         int pos = 0;
144         long start = System.currentTimeMillis();
145         for (int i = 0; i < size; i++) {
146             buffer[pos++] = (byte)i;
147             
148             
149             if (pos == bsize) {
150                 // flush buffer
151
checkedOut.write( buffer );
152                 pos = 0;
153             }
154         }
155         
156         if (pos != 0) {
157             // flush buffer last time
158
checkedOut.write( buffer, 0, pos );
159         }
160         long stop = System.currentTimeMillis();
161         
162         long bps = (long)size * 1000 / (stop - start);
163         avWritespeed += bps;
164         writecount++;
165         movement += size;
166         
167         check[_actObj] = checksum.getValue();
168         
169         System.out.println( "ok. (Size: " + sizes[_actObj] / 1024 + " kB, " + bps / 1024 + " kB/s)" );
170     }
171     
172     
173     /** */
174     private void verifyBlob( BLOBContainer _blob, int _actObj ) throws Exception JavaDoc {
175         
176         System.out.print( "verify " + names[_actObj] + "..." );
177         
178         BLOBInputStream in = new BLOBInputStream( _blob );
179         CRC32 checksum = new CRC32();
180         CheckedInputStream checkedIn = new CheckedInputStream( in, checksum );
181         int read = 0;
182         int got;
183         
184         long start = System.currentTimeMillis();
185         while ((got = checkedIn.read( buffer )) != -1) {
186             read += got;
187         }
188         long stop = System.currentTimeMillis();
189         
190         long bps = (long)read * 1000 / (stop - start);
191         avReadspeed += bps;
192         readcount++;
193         movement += read;
194         
195         if (read == sizes[_actObj] && checksum.getValue() == check[_actObj]) {
196             System.out.print( "ok. (Size: " + String.valueOf( read / 1024 ) + " kB, " + bps / 1024
197                     + " kB/s), Checksum: " + checksum.getValue() + ")" );
198         } else {
199             System.out.println( "\nERROR!!! " + names[_actObj] + " is invalid. Size: " + String.valueOf( read / 1024 )
200                     + " kB != " + String.valueOf( sizes[_actObj] / 1024 ) + "kB" );
201             System.out.println( " CHECKSUM is " + checksum.getValue() + " (should be " + check[_actObj]
202                     + ")\n" );
203         }
204     }
205     
206     
207     /** */
208     public static void main( String JavaDoc[] _args ) throws Exception JavaDoc {
209         
210         int pool = POOLSIZE;
211         int buffer = BUFFERSIZE;
212         int page = PAGESIZE;
213         int min = MINSIZE;
214         int max = MAXSIZE;
215         String JavaDoc host = "localhost";
216         int port = 3333;
217         
218         try {
219             for (int i = 0; i < _args.length; i++) {
220                 if (_args[i].equals( "-help" ) || _args[i].equals( "-h" )) {
221                     printHelp();
222                 } else if (_args[i].startsWith( "-pool" )) {
223                     pool = Integer.parseInt( _args[i].substring( 5 ) );
224                 } else if (_args[i].startsWith( "-buffer" )) {
225                     buffer = Integer.parseInt( _args[i].substring( 7 ) );
226                 } else if (_args[i].startsWith( "-page" )) {
227                     page = Integer.parseInt( _args[i].substring( 5 ) );
228                 } else if (_args[i].startsWith( "-min" )) {
229                     min = Integer.parseInt( _args[i].substring( 4 ) );
230                 } else if (_args[i].startsWith( "-max" )) {
231                     max = Integer.parseInt( _args[i].substring( 4 ) );
232                 } else if (_args[i].startsWith( "-host" )) {
233                     host = _args[i].substring( 5 );
234                 } else {
235                     if (_args[i].startsWith( "-port" )) {
236                         port = Integer.parseInt( _args[i].substring( 5 ) );
237                     }
238                 }
239             }
240         } catch (Exception JavaDoc e) {
241             printHelp();
242         }
243         if (_args.length == 0) {
244             printHelp();
245         }
246         
247         RemoteDatabase db = new RemoteDatabase();
248         try {
249             db.open( host, port );
250         } catch (Exception JavaDoc e) {
251             System.out.println( "No ozone server found!" );
252             System.exit( 0 );
253         }
254         
255         db.reloadClasses();
256         System.out.println( "connected..." );
257         
258         BLOBBench blob = new BLOBBench( db, pool, buffer, page * 1024, min, max );
259         
260         System.out.println( "----------------------------------------------------------------" );
261         System.out.println( " B L O B Test starting the test thread. Press ENTER to abort.\n" );
262         System.out.println( "environment:" );
263         System.out.println( "connected to db at " + host + ":" + port );
264         System.out.println( "a pool of " + pool + " Blobs will be managed. " );
265         System.out.println( "Streambuffersize = " + buffer + " byte" );
266         System.out.println( "BlobPagesize = " + page + " kB" );
267         System.out.println( "Blobs will be created in the range from " + min + " kB to " + max + " kB" );
268         System.out.println( "----------------------------------------------------------------" );
269         
270         blob.start();
271         long start = System.currentTimeMillis();
272         System.in.read();
273         blob.halted = true;
274         System.out.println( "\nTest halted. Waiting for last databaseoperation to quit..." );
275         blob.join();
276         long stop = System.currentTimeMillis();
277         db.close();
278         System.out.println( "disconnected." );
279         
280         System.out.println( "----------------------------------------------------------------" );
281         System.out.println( "statistics:" );
282         stop = (stop - start) / 1000;
283         start = stop / 60;
284         stop = (stop - start * 60);
285         System.out.println( "test runs " + start + " min and " + stop + " seconds, ca." + blob.movement / 1024 / 1024
286                 + " MB of data were moved (read + write)" );
287         System.out.println( "average writespeed: " + blob.avWritespeed / (blob.writecount == 0
288                 ? 1
289                 : blob.writecount) + " Bytes/s" );
290         System.out.println( "average readspeed: " + blob.avReadspeed / (blob.readcount == 0
291                 ? 1
292                 : blob.readcount) + " Bytes/s" );
293     }
294     
295     
296     private static void printHelp() {
297         System.out.println(
298                 "usage: ojvm BLOB -pool<number> -buffer<number> -page<number> -min<number> -max<number> -host<IP-num> -port<number>" );
299         System.out.println( " -pool number of managed objects" );
300         System.out.println( " -buffer size of StreamBuffer (bytes)" );
301         System.out.println( " -page BlobPagesize (kBytes)" );
302         System.out.println( " -min minSize for Blobs (kBytes)" );
303         System.out.println( " -max maxSize for Blobs (kBytes)" );
304         System.out.println( " -host database server (default: localhost)" );
305         System.out.println( " -port port for dbserver (default: 3333)" );
306         System.out.println( " NOTE: Database has to be running!" );
307         System.exit( 0 );
308     }
309     
310 }
311
Popular Tags