当前位置: 首页 > 工具软件 > jdbm > 使用案例 >

Java代码测试数据库性能_JDBM纯Java数据库性能测试评估

董意蕴
2023-12-01

import java.util.Properties;

import jdbm.RecordManager;

import jdbm.RecordManagerFactory;

import jdbm.helper.StringComparator;

import jdbm.btree.BTree;

public class FamousPeople {

static String DATABASE = "people";

static String BTREE_NAME = "FamousPeople";

/**

* Example main entrypoint.

*/

public static void main( String[] args ) {

RecordManager recman;

long          recid;

BTree         tree;

Properties    props;

props = new Properties();

try {

// open database and setup an object cache

recman = RecordManagerFactory.createRecordManager( DATABASE, props );

// try to reload an existing B+Tree

recid = recman.getNamedObject( BTREE_NAME );

if ( recid != 0 ) {

tree = BTree.load( recman, recid );

System.out.println( "Reloaded existing BTree with " + tree.size()

+ " famous people." );

} else {

// create a new B+Tree data structure and use a StringComparator

// to order the records based on people's name.

tree = BTree.createInstance( recman, new StringComparator() );

recman.setNamedObject( BTREE_NAME, tree.getRecid() );

System.out.println( "Created a new empty BTree" );

}

// insert people with their respective occupation

System.out.println();

long key = 1999;

int nRecords=100000;

long start=System.currentTimeMillis();

for (int i = 0; i < nRecords; i++) {

key = (3141592621L*key + 2718281829L) % 1000000007L;

tree.insert( key+"", Long.toString(key), false );

}

System.out.println("Elapsed time for inserting " + nRecords + " records: "

+ (System.currentTimeMillis() - start) + " milliseconds");

key = 1999;

start=System.currentTimeMillis();

for (int i = 0; i < nRecords; i++) {

key = (3141592621L*key + 2718281829L) % 1000000007L;

tree.find( key+"" );

}

System.out.println("Elapsed time for performing " + nRecords + " index searches: "

+ (System.currentTimeMillis() - start) + " milliseconds");

key = 1999;

start=System.currentTimeMillis();

for (int i = 0; i < nRecords; i++) {

key = (3141592621L*key + 2718281829L) % 1000000007L;

tree.remove( key+"" );

}

System.out.println("Elapsed time for deleting " + nRecords + " records: "

+ (System.currentTimeMillis() - start) + " milliseconds");

// make the data persistent in the database

recman.commit();

recman.close();

} catch ( Exception except ) {

except.printStackTrace();

}

}

}

 类似资料: