KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > test > blob > BLOBTest


1 // You can redistribute this software and/or modify it under the terms of
2
// the Ozone Library License version 1 published by ozone-db.org.
3
//
4
// The original code and portions created by SMB are
5
// Copyright (C) 1997-2001 by SMB GmbH. All rights reserved.
6
//
7
// $Id: BLOBTest.java,v 1.3 2002/12/29 11:15:57 per_nyfelt Exp $
8

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