2009-10-18 03:38:07 +02:00
package net.yacy ;
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 ;
import java.util.Date ;
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 ;
2011-03-07 21:36:40 +01:00
import net.yacy.cora.document.UTF8 ;
2012-09-21 16:46:57 +02:00
import net.yacy.cora.order.Base64Order ;
2011-12-16 23:59:29 +01:00
import net.yacy.cora.order.CloneableIterator ;
2012-09-21 16:46:57 +02:00
import net.yacy.cora.order.NaturalOrder ;
2012-07-27 12:13:53 +02:00
import net.yacy.cora.util.SpaceExceededException ;
2010-08-04 15:33:12 +02:00
import net.yacy.kelondro.index.Index ;
2009-10-10 01:32:08 +02:00
import net.yacy.kelondro.index.Row ;
import net.yacy.kelondro.index.RowSet ;
2009-11-05 21:28:37 +01:00
import net.yacy.kelondro.logging.Log ;
2009-10-10 03:14:19 +02:00
import net.yacy.kelondro.table.SQLTable ;
import net.yacy.kelondro.table.SplitTable ;
import net.yacy.kelondro.table.Table ;
2011-05-27 10:24:54 +02:00
import net.yacy.kelondro.util.ByteBuffer ;
2009-10-10 03:14:19 +02:00
import net.yacy.kelondro.util.MemoryControl ;
2009-10-20 00:34:44 +02:00
import net.yacy.visualization.ChartPlotter ;
2009-10-10 01:22:22 +02:00
2006-06-22 10:37:48 +02:00
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 ] = '}' ;
}
2011-07-15 10:38:10 +02:00
2006-06-22 10:37:48 +02:00
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
2011-02-25 01:56:31 +01:00
final String s = ( Base64Order . enhancedCoder . encodeLongSB ( Math . abs ( r0 ) , 6 ) . toString ( ) +
Base64Order . enhancedCoder . encodeLongSB ( Math . abs ( r1 ) , 6 ) . toString ( ) ) ;
2007-04-19 15:37:02 +02:00
return s . getBytes ( ) ;
2006-06-22 10:37:48 +02:00
}
2011-07-15 10:38:10 +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 ( ) ) ;
}
2011-07-15 10:38:10 +02:00
2006-06-22 10:37:48 +02:00
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 ( ) {
2011-03-07 21:36:40 +01:00
final String s = UTF8 . String ( this . value ) . trim ( ) ;
2012-07-10 22:59:03 +02:00
if ( s . isEmpty ( ) ) return false ;
2010-01-11 00:09:48 +01:00
final long source = Long . parseLong ( s ) ;
2011-05-27 10:24:54 +02:00
return ByteBuffer . equals ( this . key , randomHash ( source , source ) ) ;
2006-06-22 10:37:48 +02:00
}
public byte [ ] getKey ( ) {
return this . key ;
}
public byte [ ] getValue ( ) {
return this . value ;
}
2011-03-08 02:51:51 +01:00
@Override
2006-06-22 10:37:48 +02:00
public String toString ( ) {
2011-03-07 21:36:40 +01:00
return UTF8 . String ( this . key ) + " # " + UTF8 . String ( this . value ) ;
2006-06-22 10:37:48 +02:00
}
}
public static abstract class STJob implements Runnable {
2010-08-04 15:33:12 +02:00
private final Index table_test , table_reference ;
2006-06-22 10:37:48 +02:00
private final long source ;
2010-08-04 15:33:12 +02:00
public STJob ( final Index table_test , final Index 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 ;
}
2010-08-04 15:33:12 +02:00
public Index getTable_test ( ) {
2008-01-20 22:42:35 +01:00
return this . table_test ;
}
2010-08-04 15:33:12 +02:00
public Index getTable_reference ( ) {
2008-01-20 22:42:35 +01:00
return this . table_reference ;
2006-06-22 10:37:48 +02:00
}
2012-05-28 23:53:48 +02:00
@Override
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 {
2010-08-04 15:33:12 +02:00
public WriteJob ( final Index table_test , final Index 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
}
2012-05-28 23:53:48 +02:00
@Override
2006-06-22 10:37:48 +02:00
public void run ( ) {
2011-07-15 10:38:10 +02:00
final STEntry entry = new STEntry ( 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 ) ;
2009-11-05 21:28:37 +01:00
Log . logException ( e ) ;
2006-06-22 10:37:48 +02:00
System . exit ( 0 ) ;
2012-07-27 12:13:53 +02:00
} catch ( final SpaceExceededException e ) {
2009-12-10 00:27:26 +01:00
System . err . println ( e ) ;
Log . logException ( e ) ;
System . exit ( 0 ) ;
2006-06-22 10:37:48 +02:00
}
}
}
2007-04-17 17:15:47 +02:00
public static final class RemoveJob extends STJob {
2010-08-04 15:33:12 +02:00
public RemoveJob ( final Index table_test , final Index 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
}
2012-05-28 23:53:48 +02:00
@Override
2007-04-17 17:15:47 +02:00
public void run ( ) {
2011-07-15 10:38:10 +02:00
final STEntry entry = new STEntry ( 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 ( ) ) ;
2010-04-20 16:47:41 +02:00
if ( getTable_reference ( ) ! = null ) getTable_reference ( ) . delete ( 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 ) ;
2009-11-05 21:28:37 +01:00
Log . logException ( e ) ;
2007-04-17 17:15:47 +02:00
System . exit ( 0 ) ;
}
}
}
2006-06-22 10:37:48 +02:00
public static final class ReadJob extends STJob {
2010-08-04 15:33:12 +02:00
public ReadJob ( final Index table_test , final Index 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
}
2012-05-28 23:53:48 +02:00
@Override
2006-06-22 10:37:48 +02:00
public void run ( ) {
2011-07-15 10:38:10 +02:00
final STEntry entry = new STEntry ( getSource ( ) ) ;
2006-06-22 10:37:48 +02:00
try {
2011-07-15 10:38:10 +02:00
Row . Entry entryBytes = getTable_test ( ) . get ( entry . getKey ( ) , false ) ;
2008-01-20 22:42:35 +01:00
if ( entryBytes ! = null ) {
2011-02-25 13:41:27 +01:00
final STEntry dbEntry = new STEntry ( entryBytes . getPrimaryKeyBytes ( ) , entryBytes . getColBytes ( 1 , true ) ) ;
2008-01-20 22:42:35 +01:00
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 ) {
2011-07-15 10:38:10 +02:00
entryBytes = getTable_reference ( ) . get ( entry . getKey ( ) , false ) ;
2006-06-22 10:37:48 +02:00
if ( entryBytes ! = null ) {
2011-02-25 13:41:27 +01:00
final STEntry dbEntry = new STEntry ( entryBytes . getPrimaryKeyBytes ( ) , entryBytes . getColBytes ( 1 , true ) ) ;
2006-06-22 10:37:48 +02:00
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 ) ;
2009-11-05 21:28:37 +01:00
Log . logException ( e ) ;
2006-06-22 10:37:48 +02:00
System . exit ( 0 ) ;
}
}
}
2011-07-15 10:38:10 +02:00
2010-08-04 15:33:12 +02:00
public static Index 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-09-07 22:30:57 +02:00
return new SplitTable ( tablepath , new File ( tablename ) . getName ( ) , testRow , true , true ) ;
2008-01-20 22:42:35 +01:00
}
if ( dbe . equals ( " kelondroEcoTable " ) ) {
2011-09-15 12:01:21 +02:00
return new Table ( new File ( tablename ) , testRow , 1000 , 0 , true , true , true ) ;
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 ;
}
2011-07-15 10:38:10 +02:00
2010-08-04 15:33:12 +02:00
public static boolean checkEquivalence ( final Index test , final Index 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 ;
2011-07-15 10:38:10 +02: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 ( ) ;
2011-07-15 10:38:10 +02:00
reference_entry = reference . get ( test_entry . getPrimaryKeyBytes ( ) , false ) ;
2008-01-20 22:42:35 +01:00
if ( ! test_entry . equals ( reference_entry ) ) {
2011-03-07 21:36:40 +01:00
System . out . println ( " * FAILED: test entry with key ' " + UTF8 . String ( test_entry . getPrimaryKeyBytes ( ) ) + " ' has no equivalent entry in reference " ) ;
2008-01-20 22:42:35 +01:00
eq = false ;
}
}
2011-07-15 10:38:10 +02:00
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 ( ) ;
2011-07-15 10:38:10 +02:00
test_entry = test . get ( reference_entry . getPrimaryKeyBytes ( ) , false ) ;
2008-01-20 22:42:35 +01:00
if ( ! test_entry . equals ( reference_entry ) ) {
2011-03-07 21:36:40 +01:00
System . out . println ( " * FAILED: reference entry with key ' " + UTF8 . String ( test_entry . getPrimaryKeyBytes ( ) ) + " ' has no equivalent entry in test " ) ;
2008-01-20 22:42:35 +01:00
eq = false ;
}
}
2011-07-15 10:38:10 +02:00
2008-01-20 22:42:35 +01:00
System . out . println ( " * finished checkEquivalence; test-enumeration = " + ( ( rs - ts ) / 1000 ) + " seconds, reference-enumeration = " + ( ( System . currentTimeMillis ( ) - rs ) / 1000 ) + " seconds " ) ;
2011-07-15 10:38:10 +02:00
if ( eq ) System . out . println ( " * SUCCESS! the tables are equal. " ) ; else System . out . println ( " * FAILED! the tables are not equal. " ) ;
2008-01-20 22:42:35 +01:00
return eq ;
}
2011-07-15 10:38:10 +02:00
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 " ) ;
2011-07-15 10:38:10 +02:00
2008-01-20 22:42:35 +01:00
System . out . println ( " *** YaCy Database Test " ) ;
// print out command line
boolean assertionenabled = false ;
2012-09-10 07:15:52 +02:00
assert ( assertionenabled = true ) = = true ; // compare to true to remove warning: "Possible accidental assignement"
2008-01-20 22:42:35 +01:00
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 " ) ;
2011-07-15 10:38:10 +02:00
for ( final String arg : args )
System . out . print ( arg + " " ) ;
2008-01-20 22:42:35 +01:00
System . out . println ( ) ;
2011-07-15 10:38:10 +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 " ) ;
2011-07-15 10:38:10 +02:00
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 ( ) ;
2011-07-15 10:38:10 +02:00
2006-06-22 10:37:48 +02:00
// 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 ) ;
2010-08-04 15:33:12 +02:00
final Index table_test = selectTableType ( dbe_test , tablename_test , testRow ) ;
final Index table_reference = ( dbe_reference = = null ) ? null : selectTableType ( dbe_reference , tablename_reference , testRow ) ;
2011-07-15 10:38:10 +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 ) ;
2011-07-15 10:38:10 +02:00
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 " ) ;
}
2011-07-15 10:38:10 +02:00
2006-06-22 10:37:48 +02:00
if ( command . equals ( " fill " ) ) {
// fill database with random entries;
// args: <number-of-entries> <random-startpoint>
2011-07-15 10:38:10 +02: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
}
}
}
2011-07-15 10:38:10 +02:00
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 ) ;
2011-07-15 10:38:10 +02:00
entry = table_test . get ( key , false ) ;
2008-01-20 22:42:35 +01:00
if ( entry = = null )
2011-03-07 21:36:40 +01:00
System . out . println ( " missing value for entry " + UTF8 . String ( key ) + " in test table " ) ;
2008-01-20 22:42:35 +01:00
else
2011-05-27 10:24:54 +02:00
if ( ! ByteBuffer . equals ( entry . getColBytes ( 1 , false ) , key ) ) System . out . println ( " wrong value for entry " + UTF8 . String ( key ) + " : " + UTF8 . String ( entry . getColBytes ( 1 , false ) ) + " in test table " ) ;
2008-01-20 22:42:35 +01:00
if ( table_reference ! = null ) {
2011-07-15 10:38:10 +02:00
entry = table_reference . get ( key , false ) ;
2008-01-20 22:42:35 +01:00
if ( entry = = null )
2011-03-07 21:36:40 +01:00
System . out . println ( " missing value for entry " + UTF8 . String ( key ) + " in reference table " ) ;
2008-01-20 22:42:35 +01:00
else
2011-05-27 10:24:54 +02:00
if ( ! ByteBuffer . equals ( entry . getColBytes ( 1 , false ) , key ) ) System . out . println ( " wrong value for entry " + UTF8 . String ( key ) + " : " + UTF8 . String ( entry . getColBytes ( 1 , false ) ) + " in reference table " ) ;
2008-01-20 22:42:35 +01:00
}
2011-07-15 10:38:10 +02:00
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. " ) ;
}
}
}
2011-07-15 10:38:10 +02:00
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 ) ;
2010-04-20 16:47:41 +02:00
table_test . delete ( key ) ;
2006-08-16 21:49:31 +02:00
}
remove = System . currentTimeMillis ( ) - start ;
2011-07-15 10:38:10 +02:00
2006-08-16 21:49:31 +02:00
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 ( ) +
2011-07-19 14:48:50 +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 ( ) +
2011-07-19 14:48:50 +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 + + ;
}
}
2011-07-15 10:38:10 +02:00
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 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 ( ) ;
2011-09-30 09:57:50 +02:00
for ( int j = 0 ; j < row . columns ( ) ; j + + ) System . out . print ( row . getColUTF8 ( j ) + " , " ) ;
2008-06-06 18:01:27 +02:00
System . out . println ( ) ;
}
2006-06-22 10:37:48 +02:00
}
}
2011-07-15 10:38:10 +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. " ) ;
2011-07-15 10:38:10 +02:00
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 ( ) ;
2011-07-15 10:38:10 +02:00
2008-08-02 14:12:04 +02:00
final long afterclose = System . currentTimeMillis ( ) ;
2011-07-15 10:38:10 +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 ) {
2009-11-05 21:28:37 +01:00
Log . logException ( e ) ;
2006-06-22 10:37:48 +02:00
}
}
}
final class memprofiler extends Thread {
2011-07-15 10:38:10 +02:00
2009-10-20 00:34:44 +02:00
ChartPlotter memChart ;
2006-06-22 10:37:48 +02:00
boolean run ;
File outputFile ;
long start ;
2011-07-15 10:38:10 +02:00
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);
2011-07-15 10:38:10 +02:00
this . memChart = new ChartPlotter ( 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 ;
2011-07-15 10:38:10 +02:00
this . memChart . declareDimension ( ChartPlotter . DIMENSION_BOTTOM , timescale , ( width - 40 ) * timescale / expectedTimeSeconds , 0 , " FFFFFF " , " 555555 " , " SECONDS " ) ;
this . memChart . declareDimension ( ChartPlotter . DIMENSION_LEFT , memscale , ( height - 40 ) * memscale / expectedKilobytes , 0 , " FFFFFF " , " 555555 " , " KILOBYTES " ) ;
this . run = true ;
this . start = System . currentTimeMillis ( ) ;
2006-06-22 10:37:48 +02:00
}
2011-07-15 10:38:10 +02:00
2011-03-08 02:51:51 +01:00
@Override
2006-06-22 10:37:48 +02:00
public void run ( ) {
2009-05-05 08:31:35 +02:00
try {
int seconds0 = 0 , kilobytes0 = 0 ;
int seconds1 = 0 , kilobytes1 = 0 ;
2011-07-15 10:38:10 +02:00
while ( this . run ) {
this . memChart . setColor ( " FF0000 " ) ;
seconds1 = ( int ) ( ( System . currentTimeMillis ( ) - this . start ) / 1000 ) ;
2009-05-05 08:31:35 +02:00
kilobytes1 = ( int ) ( MemoryControl . used ( ) / 1024 ) ;
2011-07-15 10:38:10 +02:00
this . memChart . chartLine ( ChartPlotter . DIMENSION_BOTTOM , ChartPlotter . DIMENSION_LEFT , seconds0 , kilobytes0 , seconds1 , kilobytes1 ) ;
2009-05-05 08:31:35 +02:00
seconds0 = seconds1 ;
kilobytes0 = kilobytes1 ;
try { Thread . sleep ( 100 ) ; } catch ( final InterruptedException e ) { }
}
} catch ( final Exception e ) {
2009-11-05 21:28:37 +01:00
Log . logException ( e ) ;
2006-06-22 10:37:48 +02:00
}
2010-08-21 02:08:36 +02:00
ImageIO . setUseCache ( false ) ;
2006-06-22 10:37:48 +02:00
try {
2011-07-15 10:38:10 +02:00
ImageIO . write ( this . memChart . getImage ( ) , " png " , this . outputFile ) ;
2009-05-05 08:31:35 +02:00
} catch ( final IOException e ) {
// do noting
} catch ( final Exception e ) {
2009-11-05 21:28:37 +01:00
Log . logException ( e ) ;
2009-05-05 08:31:35 +02:00
}
2006-06-22 10:37:48 +02:00
}
2011-07-15 10:38:10 +02:00
2006-06-22 10:37:48 +02:00
public void terminate ( ) {
2011-07-15 10:38:10 +02:00
this . run = false ;
while ( 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