final Random random = new Random();
File dbDir = createDbDir("parallelPutGetTest");
Environment dbEnvironment = BerkeleyDBViewComputationCacheSource.constructDatabaseEnvironment(dbDir, false);
final BerkeleyDBBinaryDataStore dataStore = new BerkeleyDBBinaryDataStore(dbEnvironment, "parallelPutGetTest");
dataStore.start();
final AtomicLong currentMaxIdentifier = new AtomicLong(0L);
final byte[] bytes = new byte[100];
random.nextBytes(bytes);
Thread tPut = new Thread(new Runnable() {
@Override
public void run() {
OperationTimer timer = new OperationTimer(s_logger, "Putting {} entries", numEntries);
for (int i = 0; i < numEntries; i++) {
random.nextBytes(bytes);
dataStore.put(i, bytes);
currentMaxIdentifier.set(i);
}
long numMillis = timer.finished();
double msPerPut = ((double) numMillis) / ((double) numGets);
double putsPerSecond = 1000.0 / msPerPut;
s_logger.info("for {} puts, {} ms/put, {} puts/sec", new Object[] {numEntries, msPerPut, putsPerSecond});
}
}, "Putter");
class GetRunner implements Runnable {
@Override
public void run() {
OperationTimer timer = new OperationTimer(s_logger, "Getting {} entries", numGets);
for (int i = 0; i < numGets; i++) {
int maxIdentifier = (int) currentMaxIdentifier.get();
long actualIdentifier = random.nextInt(maxIdentifier);
dataStore.get(actualIdentifier);
}
long numMillis = timer.finished();
double msPerGet = ((double) numMillis) / ((double) numGets);
double getsPerSecond = 1000.0 / msPerGet;
s_logger.info("for {} gets, {} ms/get, {} gets/sec", new Object[] {numGets, msPerGet, getsPerSecond});
}
};
Thread tGet1 = new Thread(new GetRunner(), "getter-1");
Thread tGet2 = new Thread(new GetRunner(), "getter-2");
//Thread tGet3 = new Thread(new GetRunner(), "getter-3");
//Thread tGet4 = new Thread(new GetRunner(), "getter-4");
//Thread tGet5 = new Thread(new GetRunner(), "getter-5");
tPut.start();
Thread.sleep(5L);
tGet1.start();
tGet2.start();
//tGet3.start();
//tGet4.start();
//tGet5.start();
tPut.join();
tGet1.join();
tGet2.join();
//tGet3.join();
//tGet4.join();
//tGet5.join();
dataStore.delete();
dataStore.stop();
dbEnvironment.close();
}