2006-06-22 10:37:48 +02:00
// test application
// shall be used to compare the kelondroDB with other databases
// with relevance to yacy-specific use cases
import java.io.File ;
import java.io.IOException ;
2007-04-17 17:15:47 +02:00
import java.util.ArrayList ;
2006-06-22 10:37:48 +02:00
import java.util.Date ;
2007-04-19 15:37:02 +02:00
import java.util.HashSet ;
2008-01-20 22:42:35 +01:00
import java.util.Iterator ;
2006-09-30 00:27:20 +02:00
import java.util.Random ;
2006-06-22 10:37:48 +02:00
2007-01-16 00:31:50 +01:00
import javax.imageio.ImageIO ;
2009-01-30 16:33:00 +01:00
import de.anomic.kelondro.index.Row ;
import de.anomic.kelondro.index.RowSet ;
import de.anomic.kelondro.index.ObjectIndex ;
2009-03-02 11:00:32 +01:00
import de.anomic.kelondro.index.ObjectArrayCache ;
2009-01-30 23:08:08 +01:00
import de.anomic.kelondro.order.Base64Order ;
import de.anomic.kelondro.order.CloneableIterator ;
import de.anomic.kelondro.order.NaturalOrder ;
import de.anomic.kelondro.table.EcoTable ;
import de.anomic.kelondro.table.Tree ;
2009-01-30 23:44:20 +01:00
import de.anomic.kelondro.table.SQLTable ;
import de.anomic.kelondro.table.SplitTable ;
import de.anomic.kelondro.util.MemoryControl ;
2008-03-27 13:03:16 +01:00
import de.anomic.server.serverInstantBusyThread ;
2006-06-22 10:37:48 +02:00
import de.anomic.ymage.ymageChart ;
public class dbtest {
public final static int keylength = 12 ;
public final static int valuelength = 223 ; // sum of all data length as defined in plasmaURL
2008-08-06 21:43:12 +02:00
private static final byte [ ] dummyvalue2 = new byte [ valuelength ] ;
2006-06-22 10:37:48 +02:00
static {
// fill the dummy value
for ( int i = 0 ; i < valuelength ; i + + ) dummyvalue2 [ i ] = '-' ;
dummyvalue2 [ 0 ] = '{' ;
dummyvalue2 [ valuelength - 1 ] = '}' ;
}
public static byte [ ] randomHash ( final long r0 , final long r1 ) {
// a long can have 64 bit, but a 12-byte hash can have 6 * 12 = 72 bits
// so we construct a generic Hash using two long values
2009-01-30 16:33:00 +01:00
final String s = ( Base64Order . enhancedCoder . encodeLong ( Math . abs ( r0 ) , 6 ) +
Base64Order . enhancedCoder . encodeLong ( Math . abs ( r1 ) , 6 ) ) ;
2007-04-19 15:37:02 +02:00
return s . getBytes ( ) ;
2006-06-22 10:37:48 +02:00
}
2008-08-02 14:12:04 +02:00
public static byte [ ] randomHash ( final Random r ) {
2006-06-22 10:37:48 +02:00
return randomHash ( r . nextLong ( ) , r . nextLong ( ) ) ;
}
public final static class STEntry {
private final byte [ ] key ;
private final byte [ ] value ;
public STEntry ( final long aSource ) {
this . key = randomHash ( aSource , aSource ) ;
this . value = new byte [ valuelength ] ;
for ( int i = 0 ; i < valuelength ; i + + ) this . value [ i ] = 0 ;
final byte [ ] tempKey = String . valueOf ( aSource ) . getBytes ( ) ;
System . arraycopy ( tempKey , 0 , this . value , 0 , tempKey . length ) ;
}
public STEntry ( final byte [ ] aKey , final byte [ ] aValue ) {
this . key = aKey ;
this . value = aValue ;
}
public boolean isValid ( ) {
2008-08-02 14:12:04 +02:00
final String s = new String ( this . value ) . trim ( ) ;
2006-06-22 10:37:48 +02:00
if ( s . length ( ) = = 0 ) return false ;
2009-02-17 10:12:47 +01:00
final long source = Long . valueOf ( s ) . longValue ( ) ;
2006-06-22 10:37:48 +02:00
return new String ( this . key ) . equals ( new String ( randomHash ( source , source ) ) ) ;
}
public byte [ ] getKey ( ) {
return this . key ;
}
public byte [ ] getValue ( ) {
return this . value ;
}
public String toString ( ) {
return new String ( this . key ) + " # " + new String ( this . value ) ;
}
}
public static abstract class STJob implements Runnable {
2009-01-30 16:33:00 +01:00
private final ObjectIndex table_test , table_reference ;
2006-06-22 10:37:48 +02:00
private final long source ;
2009-01-30 16:33:00 +01:00
public STJob ( final ObjectIndex table_test , final ObjectIndex table_reference , final long aSource ) {
2008-01-20 22:42:35 +01:00
this . table_test = table_test ;
this . table_reference = table_reference ;
2006-06-22 10:37:48 +02:00
this . source = aSource ;
}
2009-01-30 16:33:00 +01:00
public ObjectIndex getTable_test ( ) {
2008-01-20 22:42:35 +01:00
return this . table_test ;
}
2009-01-30 16:33:00 +01:00
public ObjectIndex getTable_reference ( ) {
2008-01-20 22:42:35 +01:00
return this . table_reference ;
2006-06-22 10:37:48 +02:00
}
public abstract void run ( ) ;
public long getSource ( ) {
return this . source ;
}
}
public static final class WriteJob extends STJob {
2009-01-30 16:33:00 +01:00
public WriteJob ( final ObjectIndex table_test , final ObjectIndex table_reference , final long aSource ) {
2008-01-20 22:42:35 +01:00
super ( table_test , table_reference , aSource ) ;
2006-06-22 10:37:48 +02:00
}
public void run ( ) {
final STEntry entry = new STEntry ( this . getSource ( ) ) ;
2009-01-30 16:33:00 +01:00
System . out . println ( " write: " + NaturalOrder . arrayList ( entry . getKey ( ) , 0 , entry . getKey ( ) . length ) ) ;
2006-06-22 10:37:48 +02:00
try {
2008-01-20 22:42:35 +01:00
getTable_test ( ) . put ( getTable_test ( ) . row ( ) . newEntry ( new byte [ ] [ ] { entry . getKey ( ) , entry . getValue ( ) , entry . getValue ( ) } ) ) ;
if ( getTable_reference ( ) ! = null ) getTable_reference ( ) . put ( getTable_test ( ) . row ( ) . newEntry ( new byte [ ] [ ] { entry . getKey ( ) , entry . getValue ( ) , entry . getValue ( ) } ) ) ;
2008-08-02 14:12:04 +02:00
} catch ( final IOException e ) {
2006-06-22 10:37:48 +02:00
System . err . println ( e ) ;
e . printStackTrace ( ) ;
System . exit ( 0 ) ;
}
}
}
2007-04-17 17:15:47 +02:00
public static final class RemoveJob extends STJob {
2009-01-30 16:33:00 +01:00
public RemoveJob ( final ObjectIndex table_test , final ObjectIndex table_reference , final long aSource ) {
2008-01-20 22:42:35 +01:00
super ( table_test , table_reference , aSource ) ;
2007-04-17 17:15:47 +02:00
}
public void run ( ) {
final STEntry entry = new STEntry ( this . getSource ( ) ) ;
2009-01-30 16:33:00 +01:00
System . out . println ( " remove: " + NaturalOrder . arrayList ( entry . getKey ( ) , 0 , entry . getKey ( ) . length ) ) ;
2007-04-17 17:15:47 +02:00
try {
2008-07-05 02:35:20 +02:00
getTable_test ( ) . remove ( entry . getKey ( ) ) ;
if ( getTable_reference ( ) ! = null ) getTable_reference ( ) . remove ( entry . getKey ( ) ) ;
2008-08-02 14:12:04 +02:00
} catch ( final IOException e ) {
2007-04-17 17:15:47 +02:00
System . err . println ( e ) ;
e . printStackTrace ( ) ;
System . exit ( 0 ) ;
}
}
}
2006-06-22 10:37:48 +02:00
public static final class ReadJob extends STJob {
2009-01-30 16:33:00 +01:00
public ReadJob ( final ObjectIndex table_test , final ObjectIndex table_reference , final long aSource ) {
2008-01-20 22:42:35 +01:00
super ( table_test , table_reference , aSource ) ;
2006-06-22 10:37:48 +02:00
}
public void run ( ) {
final STEntry entry = new STEntry ( this . getSource ( ) ) ;
try {
2009-01-30 16:33:00 +01:00
Row . Entry entryBytes = getTable_test ( ) . get ( entry . getKey ( ) ) ;
2008-01-20 22:42:35 +01:00
if ( entryBytes ! = null ) {
final STEntry dbEntry = new STEntry ( entryBytes . getColBytes ( 0 ) , entryBytes . getColBytes ( 1 ) ) ;
if ( ! dbEntry . isValid ( ) ) {
System . out . println ( " INVALID table_test: " + dbEntry ) ;
} / * else {
System . out . println ( " _VALID_ table_test: " + dbEntry ) ;
getTable ( ) . remove ( entry . getKey ( ) , true ) ;
} * /
}
if ( getTable_reference ( ) ! = null ) {
entryBytes = getTable_reference ( ) . get ( entry . getKey ( ) ) ;
2006-06-22 10:37:48 +02:00
if ( entryBytes ! = null ) {
final STEntry dbEntry = new STEntry ( entryBytes . getColBytes ( 0 ) , entryBytes . getColBytes ( 1 ) ) ;
if ( ! dbEntry . isValid ( ) ) {
2008-01-20 22:42:35 +01:00
System . out . println ( " INVALID table_reference: " + dbEntry ) ;
2008-01-17 13:12:52 +01:00
} / * else {
2008-01-20 22:42:35 +01:00
System . out . println ( " _VALID_ table_reference: " + dbEntry ) ;
2008-01-17 13:12:52 +01:00
getTable ( ) . remove ( entry . getKey ( ) , true ) ;
} * /
2006-06-22 10:37:48 +02:00
}
2008-01-20 22:42:35 +01:00
}
2008-08-02 14:12:04 +02:00
} catch ( final IOException e ) {
2006-06-22 10:37:48 +02:00
System . err . println ( e ) ;
e . printStackTrace ( ) ;
System . exit ( 0 ) ;
}
}
}
2009-01-30 16:33:00 +01:00
public static ObjectIndex selectTableType ( final String dbe , final String tablename , final Row testRow ) throws Exception {
2008-01-20 22:42:35 +01:00
if ( dbe . equals ( " kelondroRowSet " ) ) {
2009-01-30 16:33:00 +01:00
return new RowSet ( testRow , 0 ) ;
2008-01-20 22:42:35 +01:00
}
if ( dbe . equals ( " kelondroSplitTable " ) ) {
2008-08-02 14:12:04 +02:00
final File tablepath = new File ( tablename ) . getParentFile ( ) ;
2009-01-30 23:44:20 +01:00
return new SplitTable ( tablepath , new File ( tablename ) . getName ( ) , testRow , true ) ;
2008-01-20 22:42:35 +01:00
}
if ( dbe . equals ( " kelondroEcoTable " ) ) {
2009-01-30 23:08:08 +01:00
return new EcoTable ( new File ( tablename ) , testRow , EcoTable . tailCacheForceUsage , 1000 , 0 ) ;
2008-01-20 22:42:35 +01:00
}
if ( dbe . equals ( " mysql " ) ) {
2009-01-30 23:44:20 +01:00
return new SQLTable ( " mysql " , testRow ) ;
2008-01-20 22:42:35 +01:00
}
if ( dbe . equals ( " pgsql " ) ) {
2009-01-30 23:44:20 +01:00
return new SQLTable ( " pgsql " , testRow ) ;
2008-01-20 22:42:35 +01:00
}
return null ;
}
2009-01-30 16:33:00 +01:00
public static boolean checkEquivalence ( final ObjectIndex test , final ObjectIndex reference ) throws IOException {
2008-01-20 22:42:35 +01:00
if ( reference = = null ) return true ;
if ( test . size ( ) = = reference . size ( ) ) {
System . out . println ( " * Testing equivalence of test table to reference table, " + test . size ( ) + " entries " ) ;
} else {
System . out . println ( " * Testing equivalence of test table to reference table: FAILED! the tables have different sizes: test.size() = " + test . size ( ) + " , reference.size() = " + reference . size ( ) ) ;
return false ;
}
boolean eq = true ;
2009-01-30 16:33:00 +01:00
Row . Entry test_entry , reference_entry ;
2008-01-20 22:42:35 +01:00
2009-02-24 11:40:20 +01:00
Iterator < Row . Entry > i = test . rows ( ) ;
2008-01-20 22:42:35 +01:00
System . out . println ( " * Testing now by enumeration over test table " ) ;
2008-08-02 14:12:04 +02:00
final long ts = System . currentTimeMillis ( ) ;
2008-01-20 22:42:35 +01:00
while ( i . hasNext ( ) ) {
test_entry = i . next ( ) ;
reference_entry = reference . get ( test_entry . getPrimaryKeyBytes ( ) ) ;
if ( ! test_entry . equals ( reference_entry ) ) {
System . out . println ( " * FAILED: test entry with key ' " + new String ( test_entry . getPrimaryKeyBytes ( ) ) + " ' has no equivalent entry in reference " ) ;
eq = false ;
}
}
2009-02-24 11:40:20 +01:00
i = reference . rows ( ) ;
2008-01-20 22:42:35 +01:00
System . out . println ( " * Testing now by enumeration over reference table " ) ;
2008-08-02 14:12:04 +02:00
final long rs = System . currentTimeMillis ( ) ;
2008-01-20 22:42:35 +01:00
while ( i . hasNext ( ) ) {
reference_entry = i . next ( ) ;
test_entry = test . get ( reference_entry . getPrimaryKeyBytes ( ) ) ;
if ( ! test_entry . equals ( reference_entry ) ) {
System . out . println ( " * FAILED: reference entry with key ' " + new String ( test_entry . getPrimaryKeyBytes ( ) ) + " ' has no equivalent entry in test " ) ;
eq = false ;
}
}
System . out . println ( " * finished checkEquivalence; test-enumeration = " + ( ( rs - ts ) / 1000 ) + " seconds, reference-enumeration = " + ( ( System . currentTimeMillis ( ) - rs ) / 1000 ) + " seconds " ) ;
if ( eq ) System . out . println ( " * SUCCESS! the tables are equal. " ) ; else System . out . println ( " * FAILED! the tables are not equal. " ) ;
return eq ;
}
2008-08-02 14:12:04 +02:00
public static void main ( final String [ ] args ) {
2006-06-22 10:37:48 +02:00
System . setProperty ( " java.awt.headless " , " true " ) ;
2008-01-20 22:42:35 +01:00
System . out . println ( " *** YaCy Database Test " ) ;
// print out command line
boolean assertionenabled = false ;
assert assertionenabled = true ;
if ( assertionenabled ) System . out . println ( " *** Asserts are enabled " ) ; else System . out . println ( " *** HINT: YOU SHOULD ENABLE ASSERTS! (include -ea in start arguments " ) ;
2009-01-30 16:33:00 +01:00
final long mb = MemoryControl . available ( ) / 1024 / 1024 ;
2008-01-20 22:42:35 +01:00
System . out . println ( " *** RAM = " + mb + " MB " ) ;
System . out . print ( " >java " +
( ( assertionenabled ) ? " -ea " : " " ) +
( ( mb > 100 ) ? ( " -Xmx " + ( mb + 11 ) + " m " ) : " " ) +
" -classpath classes dbtest " ) ;
for ( int i = 0 ; i < args . length ; i + + ) System . out . print ( args [ i ] + " " ) ;
System . out . println ( ) ;
2006-06-22 10:37:48 +02:00
2008-01-20 22:42:35 +01:00
// read command options
2008-08-02 14:12:04 +02:00
final String command = args [ 0 ] ; // test command
final String dbe_test = args [ 1 ] ; // the database engine that shall be tested
2008-01-20 22:42:35 +01:00
String dbe_reference = args [ 2 ] ; // the reference database engine (may be null)
2008-08-02 14:12:04 +02:00
final String tablename_test = args [ 3 ] ; // name of test-table
2008-01-20 22:42:35 +01:00
if ( dbe_reference . toLowerCase ( ) . equals ( " null " ) ) dbe_reference = null ;
2008-08-02 14:12:04 +02:00
final String tablename_reference = ( dbe_reference = = null ) ? null : ( tablename_test + " .reference " ) ;
2008-01-20 22:42:35 +01:00
// start test
2008-08-02 14:12:04 +02:00
final long startup = System . currentTimeMillis ( ) ;
2006-06-22 10:37:48 +02:00
try {
// create a memory profiler
2008-08-02 14:12:04 +02:00
final memprofiler profiler = new memprofiler ( 1024 , 320 , 120 , new File ( tablename_test + " .profile.png " ) ) ;
2006-06-22 10:37:48 +02:00
profiler . start ( ) ;
// create the database access
2009-03-13 17:52:31 +01:00
final Row testRow = new Row ( " byte[] key- " + keylength + " , byte[] dummy- " + keylength + " , value- " + valuelength , Base64Order . enhancedCoder ) ;
2009-01-30 16:33:00 +01:00
final ObjectIndex table_test = selectTableType ( dbe_test , tablename_test , testRow ) ;
final ObjectIndex table_reference = ( dbe_reference = = null ) ? null : selectTableType ( dbe_reference , tablename_reference , testRow ) ;
2006-06-22 10:37:48 +02:00
2008-08-02 14:12:04 +02:00
final long afterinit = System . currentTimeMillis ( ) ;
2008-01-20 22:42:35 +01:00
System . out . println ( " Test for db-engine " + dbe_test + " started to create file " + tablename_test + " with test " + command ) ;
2006-06-22 10:37:48 +02:00
// execute command
if ( command . equals ( " create " ) ) {
// do nothing, since opening of the database access must handle this
System . out . println ( " Database created " ) ;
}
if ( command . equals ( " fill " ) ) {
// fill database with random entries;
// args: <number-of-entries> <random-startpoint>
2008-01-20 22:42:35 +01:00
// example: java -ea -Xmx200m fill kelondroEcoTable kelondroFlexTable filldbtest 50000 0
2008-08-02 14:12:04 +02:00
final long count = Long . parseLong ( args [ 4 ] ) ;
final long randomstart = Long . parseLong ( args [ 5 ] ) ;
final Random random = new Random ( randomstart ) ;
2006-06-22 10:37:48 +02:00
byte [ ] key ;
for ( int i = 0 ; i < count ; i + + ) {
key = randomHash ( random ) ;
2008-01-20 22:42:35 +01:00
table_test . put ( testRow . newEntry ( new byte [ ] [ ] { key , key , dummyvalue2 } ) ) ;
if ( table_reference ! = null ) table_reference . put ( testRow . newEntry ( new byte [ ] [ ] { key , key , dummyvalue2 } ) ) ;
2006-06-23 14:49:42 +02:00
if ( i % 1000 = = 0 ) {
2007-03-06 23:43:32 +01:00
System . out . println ( i + " entries. " ) ;
2006-06-22 10:37:48 +02:00
}
}
}
if ( command . equals ( " read " ) ) {
// read the database and compare with random entries;
// args: <number-of-entries> <random-startpoint>
2008-08-02 14:12:04 +02:00
final long count = Long . parseLong ( args [ 4 ] ) ;
final long randomstart = Long . parseLong ( args [ 5 ] ) ;
final Random random = new Random ( randomstart ) ;
2009-01-30 16:33:00 +01:00
Row . Entry entry ;
2006-06-22 10:37:48 +02:00
byte [ ] key ;
for ( int i = 0 ; i < count ; i + + ) {
key = randomHash ( random ) ;
2008-01-20 22:42:35 +01:00
entry = table_test . get ( key ) ;
if ( entry = = null )
System . out . println ( " missing value for entry " + new String ( key ) + " in test table " ) ;
else
if ( ! ( new String ( entry . getColBytes ( 1 ) ) . equals ( new String ( key ) ) ) ) System . out . println ( " wrong value for entry " + new String ( key ) + " : " + new String ( entry . getColBytes ( 1 ) ) + " in test table " ) ;
if ( table_reference ! = null ) {
entry = table_reference . get ( key ) ;
if ( entry = = null )
System . out . println ( " missing value for entry " + new String ( key ) + " in reference table " ) ;
else
if ( ! ( new String ( entry . getColBytes ( 1 ) ) . equals ( new String ( key ) ) ) ) System . out . println ( " wrong value for entry " + new String ( key ) + " : " + new String ( entry . getColBytes ( 1 ) ) + " in reference table " ) ;
}
2006-06-23 14:49:42 +02:00
if ( i % 1000 = = 0 ) {
2006-06-22 10:37:48 +02:00
System . out . println ( i + " entries processed so far. " ) ;
}
}
}
2006-08-16 21:49:31 +02:00
if ( command . equals ( " ramtest " ) ) {
// fill database with random entries and delete them again;
// this is repeated without termination; after each loop
// the current ram is printed out
// args: <number-of-entries> <random-startpoint>
2008-08-02 14:12:04 +02:00
final long count = Long . parseLong ( args [ 4 ] ) ;
final long randomstart = Long . parseLong ( args [ 5 ] ) ;
2006-08-16 21:49:31 +02:00
byte [ ] key ;
Random random ;
long start , write , remove ;
int loop = 0 ;
while ( true ) {
// write
random = new Random ( randomstart ) ;
start = System . currentTimeMillis ( ) ;
for ( int i = 0 ; i < count ; i + + ) {
key = randomHash ( random ) ;
2008-01-20 22:42:35 +01:00
table_test . put ( table_test . row ( ) . newEntry ( new byte [ ] [ ] { key , key , dummyvalue2 } ) ) ;
2006-08-16 21:49:31 +02:00
}
write = System . currentTimeMillis ( ) - start ;
// delete
random = new Random ( randomstart ) ;
start = System . currentTimeMillis ( ) ;
for ( int i = 0 ; i < count ; i + + ) {
key = randomHash ( random ) ;
2008-07-05 02:35:20 +02:00
table_test . remove ( key ) ;
2006-08-16 21:49:31 +02:00
}
remove = System . currentTimeMillis ( ) - start ;
System . out . println ( " Loop " + loop + " : Write = " + write + " , Remove = " + remove ) ;
System . out . println ( " bevore GC: " +
2009-01-30 16:33:00 +01:00
" free = " + MemoryControl . free ( ) +
2009-04-17 00:45:39 +02:00
" , max = " + MemoryControl . maxMemory +
2009-01-30 16:33:00 +01:00
" , total = " + MemoryControl . total ( ) ) ;
2006-08-16 21:49:31 +02:00
System . gc ( ) ;
System . out . println ( " after GC: " +
2009-01-30 16:33:00 +01:00
" free = " + MemoryControl . free ( ) +
2009-04-17 00:45:39 +02:00
" , max = " + MemoryControl . maxMemory +
2009-01-30 16:33:00 +01:00
" , total = " + MemoryControl . total ( ) ) ;
2006-08-16 21:49:31 +02:00
loop + + ;
}
}
2006-06-22 10:37:48 +02:00
if ( command . equals ( " list " ) ) {
2009-01-30 23:08:08 +01:00
CloneableIterator < Row . Entry > i = null ;
2009-02-24 11:40:20 +01:00
if ( table_test instanceof Tree ) i = ( ( Tree ) table_test ) . rows ( ) ;
if ( table_test instanceof SQLTable ) i = ( ( SQLTable ) table_test ) . rows ( ) ;
2008-06-06 18:01:27 +02:00
if ( i ! = null ) {
2009-01-30 16:33:00 +01:00
Row . Entry row ;
2008-06-06 18:01:27 +02:00
while ( i . hasNext ( ) ) {
row = i . next ( ) ;
for ( int j = 0 ; j < row . columns ( ) ; j + + ) System . out . print ( row . getColString ( j , null ) + " , " ) ;
System . out . println ( ) ;
}
2006-06-22 10:37:48 +02:00
}
}
if ( command . equals ( " stressThreaded " ) ) {
//
// args: <number-of-writes> <number-of-reads-per-write> <random-startpoint>
2008-01-20 22:42:35 +01:00
// example: java -ea -classpath classes dbtest stressThreaded kelondroEcoTable kelondroFlexTable stressthreadedtest 500 50 0
2008-01-19 13:23:56 +01:00
/ * result with svn 4346
kelondroFlex :
removed : 70 , size of jcontrol set : 354 , size of kcontrol set : 354
Database size = 354 unique entries .
Execution time : open = 1329 , command = 36234 , close = 17 , total = 37580
kelondroEco :
removed : 70 , size of jcontrol set : 354 , size of kcontrol set : 354
Database size = 354 unique entries .
Execution time : open = 1324 , command = 34032 , close = 1 , total = 35357
* /
2008-08-02 14:12:04 +02:00
final long writeCount = Long . parseLong ( args [ 4 ] ) ;
final long readCount = Long . parseLong ( args [ 5 ] ) ;
final long randomstart = Long . parseLong ( args [ 6 ] ) ;
2006-06-22 10:37:48 +02:00
final Random random = new Random ( randomstart ) ;
2007-04-17 17:15:47 +02:00
long r ;
2007-04-19 15:37:02 +02:00
Long R ;
int p , rc = 0 ;
2008-08-02 14:12:04 +02:00
final ArrayList < Long > ra = new ArrayList < Long > ( ) ;
final HashSet < Long > jcontrol = new HashSet < Long > ( ) ;
2009-03-02 11:00:32 +01:00
final ObjectArrayCache kcontrol = new ObjectArrayCache ( 1 , 0 ) ;
2006-06-22 10:37:48 +02:00
for ( int i = 0 ; i < writeCount ; i + + ) {
2008-01-17 13:12:52 +01:00
r = Math . abs ( random . nextLong ( ) % 1000 ) ;
2008-08-06 21:43:12 +02:00
jcontrol . add ( Long . valueOf ( r ) ) ;
2008-01-17 13:12:52 +01:00
kcontrol . putb ( ( int ) r , " x " . getBytes ( ) ) ;
2008-03-27 13:03:16 +01:00
serverInstantBusyThread . oneTimeJob ( new WriteJob ( table_test , table_reference , r ) , 0 , 50 ) ;
2008-08-06 21:43:12 +02:00
if ( random . nextLong ( ) % 5 = = 0 ) ra . add ( Long . valueOf ( r ) ) ;
2006-06-22 10:37:48 +02:00
for ( int j = 0 ; j < readCount ; j + + ) {
2008-03-27 13:03:16 +01:00
serverInstantBusyThread . oneTimeJob ( new ReadJob ( table_test , table_reference , random . nextLong ( ) % writeCount ) , random . nextLong ( ) % 1000 , 20 ) ;
2006-06-22 10:37:48 +02:00
}
2007-04-17 17:15:47 +02:00
if ( ( ra . size ( ) > 0 ) & & ( random . nextLong ( ) % 7 = = 0 ) ) {
2008-01-17 13:12:52 +01:00
rc + + ;
2008-08-02 15:57:00 +02:00
p = Math . abs ( random . nextInt ( ) % ra . size ( ) ) ;
2008-06-06 18:01:27 +02:00
R = ra . get ( p ) ;
2008-01-17 13:12:52 +01:00
jcontrol . remove ( R ) ;
kcontrol . removeb ( ( int ) R . longValue ( ) ) ;
System . out . println ( " remove: " + R . longValue ( ) ) ;
2008-06-06 18:01:27 +02:00
serverInstantBusyThread . oneTimeJob ( new RemoveJob ( table_test , table_reference , ( ra . remove ( p ) ) . longValue ( ) ) , 0 , 50 ) ;
2007-04-17 17:15:47 +02:00
}
}
2007-04-19 15:37:02 +02:00
System . out . println ( " removed: " + rc + " , size of jcontrol set: " + jcontrol . size ( ) + " , size of kcontrol set: " + kcontrol . size ( ) ) ;
2008-03-27 13:03:16 +01:00
while ( serverInstantBusyThread . instantThreadCounter > 0 ) {
2008-08-02 14:12:04 +02:00
try { Thread . sleep ( 1000 ) ; } catch ( final InterruptedException e ) { } // wait for all tasks to finish
2008-03-27 13:03:16 +01:00
System . out . println ( " count: " + serverInstantBusyThread . instantThreadCounter + " , jobs: " + serverInstantBusyThread . jobs . toString ( ) ) ;
2006-06-22 10:37:48 +02:00
}
2008-08-02 14:12:04 +02:00
try { Thread . sleep ( 6000 ) ; } catch ( final InterruptedException e ) { }
2006-06-22 10:37:48 +02:00
}
if ( command . equals ( " stressSequential " ) ) {
//
// args: <number-of-writes> <number-of-reads> <random-startpoint>
2008-08-02 14:12:04 +02:00
final long writeCount = Long . parseLong ( args [ 4 ] ) ;
final long readCount = Long . parseLong ( args [ 5 ] ) ;
final long randomstart = Long . parseLong ( args [ 6 ] ) ;
final Random random = new Random ( randomstart ) ;
2007-04-19 15:37:02 +02:00
long r ;
Long R ;
int p , rc = 0 ;
2008-08-02 14:12:04 +02:00
final ArrayList < Long > ra = new ArrayList < Long > ( ) ;
final HashSet < Long > jcontrol = new HashSet < Long > ( ) ;
2009-03-02 11:00:32 +01:00
final ObjectArrayCache kcontrol = new ObjectArrayCache ( 1 , 0 ) ;
2006-06-22 10:37:48 +02:00
for ( int i = 0 ; i < writeCount ; i + + ) {
2008-01-17 13:12:52 +01:00
//if (i == 30) random = new Random(randomstart);
r = Math . abs ( random . nextLong ( ) % 1000 ) ;
2008-08-06 21:43:12 +02:00
jcontrol . add ( Long . valueOf ( r ) ) ;
2008-01-17 13:12:52 +01:00
kcontrol . putb ( ( int ) r , " x " . getBytes ( ) ) ;
2008-01-20 22:42:35 +01:00
new WriteJob ( table_test , table_reference , r ) . run ( ) ;
2008-08-06 21:43:12 +02:00
if ( random . nextLong ( ) % 5 = = 0 ) ra . add ( Long . valueOf ( r ) ) ;
2006-06-22 10:37:48 +02:00
for ( int j = 0 ; j < readCount ; j + + ) {
2008-01-20 22:42:35 +01:00
new ReadJob ( table_test , table_reference , random . nextLong ( ) % writeCount ) . run ( ) ;
2006-06-22 10:37:48 +02:00
}
2007-04-19 15:37:02 +02:00
if ( ( ra . size ( ) > 0 ) & & ( random . nextLong ( ) % 7 = = 0 ) ) {
2008-01-17 13:12:52 +01:00
rc + + ;
2008-08-02 15:57:00 +02:00
p = Math . abs ( random . nextInt ( ) % ra . size ( ) ) ;
2008-06-06 18:01:27 +02:00
R = ra . get ( p ) ;
2008-01-17 13:12:52 +01:00
jcontrol . remove ( R ) ;
kcontrol . removeb ( ( int ) R . longValue ( ) ) ;
2008-06-06 18:01:27 +02:00
new RemoveJob ( table_test , table_reference , ( ra . remove ( p ) ) . longValue ( ) ) . run ( ) ;
2007-04-19 15:37:02 +02:00
}
2006-06-22 10:37:48 +02:00
}
2008-08-02 14:12:04 +02:00
try { Thread . sleep ( 1000 ) ; } catch ( final InterruptedException e ) { }
2007-04-19 15:37:02 +02:00
System . out . println ( " removed: " + rc + " , size of jcontrol set: " + jcontrol . size ( ) + " , size of kcontrol set: " + kcontrol . size ( ) ) ;
2006-06-22 10:37:48 +02:00
}
2008-08-02 14:12:04 +02:00
final long aftercommand = System . currentTimeMillis ( ) ;
2008-01-20 22:42:35 +01:00
checkEquivalence ( table_test , table_reference ) ;
2008-08-02 14:12:04 +02:00
final long afterequiv = System . currentTimeMillis ( ) ;
2006-06-28 18:14:47 +02:00
// final report
2008-01-20 22:42:35 +01:00
System . out . println ( " Database size = " + table_test . size ( ) + " unique entries. " ) ;
2006-06-22 10:37:48 +02:00
// finally close the database/table
2008-01-20 22:42:35 +01:00
table_test . close ( ) ;
if ( table_reference ! = null ) table_reference . close ( ) ;
2006-06-22 10:37:48 +02:00
2008-08-02 14:12:04 +02:00
final long afterclose = System . currentTimeMillis ( ) ;
2006-06-22 10:37:48 +02:00
2008-01-20 22:42:35 +01:00
System . out . println ( " Execution time: open= " + ( afterinit - startup ) +
" , command= " + ( aftercommand - afterinit ) +
" , equiv= " + ( afterequiv - aftercommand ) +
" , close= " + ( afterclose - afterequiv ) +
" , total= " + ( afterclose - startup ) ) ;
2006-06-22 10:37:48 +02:00
profiler . terminate ( ) ;
2008-08-02 14:12:04 +02:00
} catch ( final Exception e ) {
2006-06-22 10:37:48 +02:00
e . printStackTrace ( ) ;
}
}
}
final class memprofiler extends Thread {
ymageChart memChart ;
boolean run ;
File outputFile ;
long start ;
2008-08-02 14:12:04 +02:00
public memprofiler ( final int width , final int height , final int expectedTimeSeconds , final File outputFile ) {
2006-06-22 10:37:48 +02:00
this . outputFile = outputFile ;
2008-08-02 14:12:04 +02:00
final int expectedKilobytes = 20 * 1024 ; //(Runtime.getRuntime().totalMemory() / 1024);
2008-04-08 16:44:39 +02:00
memChart = new ymageChart ( width , height , " FFFFFF " , " 000000 " , " 000000 " , 50 , 20 , 20 , 20 , " MEMORY CHART FROM EXECUTION AT " + new Date ( ) , null ) ;
2008-08-02 14:12:04 +02:00
final int timescale = 10 ; // steps with each 10 seconds
final int memscale = 1024 ;
2007-11-30 04:22:42 +01:00
memChart . declareDimension ( ymageChart . DIMENSION_BOTTOM , timescale , ( width - 40 ) * timescale / expectedTimeSeconds , 0 , " FFFFFF " , " 555555 " , " SECONDS " ) ;
memChart . declareDimension ( ymageChart . DIMENSION_LEFT , memscale , ( height - 40 ) * memscale / expectedKilobytes , 0 , " FFFFFF " , " 555555 " , " KILOBYTES " ) ;
2006-06-22 10:37:48 +02:00
run = true ;
start = System . currentTimeMillis ( ) ;
}
public void run ( ) {
2009-05-05 08:31:35 +02:00
try {
int seconds0 = 0 , kilobytes0 = 0 ;
int seconds1 = 0 , kilobytes1 = 0 ;
while ( run ) {
memChart . setColor ( " FF0000 " ) ;
seconds1 = ( int ) ( ( System . currentTimeMillis ( ) - start ) / 1000 ) ;
kilobytes1 = ( int ) ( MemoryControl . used ( ) / 1024 ) ;
memChart . chartLine ( ymageChart . DIMENSION_BOTTOM , ymageChart . DIMENSION_LEFT , seconds0 , kilobytes0 , seconds1 , kilobytes1 ) ;
seconds0 = seconds1 ;
kilobytes0 = kilobytes1 ;
try { Thread . sleep ( 100 ) ; } catch ( final InterruptedException e ) { }
}
} catch ( final Exception e ) {
e . printStackTrace ( ) ;
2006-06-22 10:37:48 +02:00
}
try {
2007-01-16 00:31:50 +01:00
ImageIO . write ( memChart . getImage ( ) , " png " , outputFile ) ;
2009-05-05 08:31:35 +02:00
} catch ( final IOException e ) {
// do noting
} catch ( final Exception e ) {
e . printStackTrace ( ) ;
}
2006-06-22 10:37:48 +02:00
}
public void terminate ( ) {
run = false ;
while ( this . isAlive ( ) ) {
2008-08-02 14:12:04 +02:00
try { Thread . sleep ( 1000 ) ; } catch ( final InterruptedException e ) { }
2006-06-22 10:37:48 +02:00
}
}
}
2006-06-22 17:45:33 +02:00