Professional Documents
Culture Documents
Mongo;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.DBCursor;
import com.mongodb.ServerAddress;
import com.mongodb.WriteConcern;
import com.mongodb.CommandResult;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Date;
import java.util.Properties;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.File;
import java.io.Writer;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.locks.ReentrantLock;
public jmongoiibench() {
}
dbName = args[0];
writerThreads = Integer.valueOf(args[1]);
numMaxInserts = Integer.valueOf(args[2]);
documentsPerInsert = Integer.valueOf(args[3]);
insertsPerFeedback = Long.valueOf(args[4]);
secondsPerFeedback = Long.valueOf(args[5]);
logFileName = args[6];
compressionType = args[7];
basementSize = Integer.valueOf(args[8]);
numSeconds = Long.valueOf(args[9]);
queriesPerInterval = Integer.valueOf(args[10]);
queryIntervalSeconds = Integer.valueOf(args[11]);
queryLimit = Integer.valueOf(args[12]);
queryBeginNumDocs = Integer.valueOf(args[13]);
maxInsertsPerSecond = Integer.valueOf(args[14]);
myWriteConcern = args[15];
serverName = args[16];
serverPort = Integer.valueOf(args[17]);
numCharFields = Integer.valueOf(args[18]);
lengthCharFields = Integer.valueOf(args[19]);
numSecondaryIndexes = Integer.valueOf(args[20]);
percentCompressible = Integer.valueOf(args[21]);
createCollection = args[22].toLowerCase();
DB db = m.getDB(dbName);
if ((!indexTechnology.toLowerCase().equals("tokumx")) && (!
indexTechnology.toLowerCase().equals("mongo"))) {
// unknown index technology, abort
logMe(" *** Unknown Indexing Technology %s, shutting
down",indexTechnology);
System.exit(1);
}
if (indexTechnology.toLowerCase().equals("tokumx")) {
logMe(" + compression type = %s",compressionType);
logMe(" + basement node size (bytes) = %d",basementSize);
}
logMe("--------------------------------------------------");
if (writerThreads > 1) {
numMaxInserts = numMaxInserts / writerThreads;
}
try {
writer = new BufferedWriter(new FileWriter(new File(logFileName)));
} catch (IOException e) {
e.printStackTrace();
}
if (createCollection.equals("n"))
{
logMe("Skipping collection creation");
}
else
{
// create the collection
String collectionName = "purchases_index";
if (indexTechnology.toLowerCase().equals("tokumx")) {
DBObject cmd = new BasicDBObject();
cmd.put("create", collectionName);
cmd.put("compression", compressionType);
cmd.put("readPageSize", basementSize);
CommandResult result = db.command(cmd);
//logMe(result.toString());
} else if (indexTechnology.toLowerCase().equals("mongo")) {
// nothing special to do for a regular mongo collection
} else {
// unknown index technology, abort
logMe(" *** Unknown Indexing Technology %s, shutting
down",indexTechnology);
System.exit(1);
}
if (indexTechnology.toLowerCase().equals("tokumx")) {
idxOptions.put("compression",compressionType);
idxOptions.put("readPageSize",basementSize);
}
if (numSecondaryIndexes >= 1) {
logMe(" *** creating secondary index on price + customerid");
coll.ensureIndex(new BasicDBObject("price", 1).append("customerid",
1), idxOptions);
}
if (numSecondaryIndexes >= 2) {
logMe(" *** creating secondary index on cashregisterid + price +
customerid");
coll.ensureIndex(new BasicDBObject("cashregisterid",
1).append("price", 1).append("customerid", 1), idxOptions);
}
if (numSecondaryIndexes >= 3) {
logMe(" *** creating secondary index on price + dateandtime +
customerid");
coll.ensureIndex(new BasicDBObject("price",
1).append("dateandtime", 1).append("customerid", 1), idxOptions);
}
// END: create the collection
}
try {
Thread.sleep(10000);
} catch (Exception e) {
e.printStackTrace();
}
try {
if (writer != null) {
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
// m.dropDatabase("mydb");
m.close();
logMe("Done!");
}
java.util.Random rand;
try {
logMe("Writer thread %d : started to load collection
%s",threadNumber, collectionName);
try {
coll.insert(aDocs);
numInserts += documentsPerInsert;
globalInserts.addAndGet(documentsPerInsert);
} catch (Exception e) {
logMe("Writer thread %d : EXCEPTION",threadNumber);
e.printStackTrace();
globalInsertExceptions.incrementAndGet();
}
if (allDone == 1)
break;
}
} catch (Exception e) {
logMe("Writer thread %d : EXCEPTION",threadNumber);
e.printStackTrace();
}
java.util.Random rand;
long numQueriesExecuted = 0;
long numQueriesTimeMs = 0;
int whichQuery = 0;
try {
logMe("Query thread %d : ready to query collection
%s",threadNumber, collectionName);
while (allDone == 0) {
//try {
// Thread.sleep(10);
//} catch (Exception e) {
// e.printStackTrace();
// }
whichQuery++;
if (whichQuery > 3) {
whichQuery = 1;
}
if (whichQuery == 1) {
// query 1
/*
def generate_pdc_query(row_count, start_time):
customerid = random.randrange(0, FLAGS.customers)
price = ((random.random() * FLAGS.max_price) +
customerid) / 100.0
FLAGS.rows_per_query)
return sql
query.put("$or", list1);
keys.put("price",1);
keys.put("dateandtime",1);
keys.put("customerid",1);
keys.put("_id",0);
} else if (whichQuery == 2) {
// query 2
/*
def generate_market_query(row_count, start_time):
customerid = random.randrange(0, FLAGS.customers)
price = ((random.random() * FLAGS.max_price) +
customerid) / 100.0
query.put("$or", list2);
keys.put("price",1);
keys.put("customerid",1);
keys.put("_id",0);
} else if (whichQuery == 3) {
// query 3
/*
def generate_register_query(row_count, start_time):
customerid = random.randrange(0, FLAGS.customers)
price = ((random.random() * FLAGS.max_price) +
customerid) / 100.0
cashregisterid = random.randrange(0,
FLAGS.cashregisters)
query.put("$or", list3);
keys.put("cashregisterid",1);
keys.put("price",1);
keys.put("customerid",1);
keys.put("_id",0);
globalQueriesExecuted.incrementAndGet();
globalQueriesTimeMs.addAndGet(elapsed);
} else {
if (outputWaiting)
{
logMe("Query thread %d : waiting for %,d document
insert(s) before starting",threadNumber,queryBeginNumDocs);
outputWaiting = false;
}
}
}
}
} catch (Exception e) {
logMe("Query thread %d : EXCEPTION",threadNumber);
e.printStackTrace();
}
while (allDone == 0)
{
try {
Thread.sleep(100);
} catch (Exception e) {
e.printStackTrace();
}
thisInserts = globalInserts.get();
thisQueriesNum = globalQueriesExecuted.get();
thisQueriesMs = globalQueriesTimeMs.get();
thisQueriesStarted = globalQueriesStarted.get();
if (((now > nextFeedbackMillis) && (secondsPerFeedback > 0)) ||
((thisInserts >= nextFeedbackInserts) && (insertsPerFeedback >
0)))
{
intervalNumber++;
nextFeedbackMillis = t0 + (1000 * secondsPerFeedback *
(intervalNumber + 1));
nextFeedbackInserts = (intervalNumber + 1) *
insertsPerFeedback;
if (thisIntervalQueriesNum > 0) {
thisIntervalQueryAvgMs = thisIntervalQueriesMs/
(double)thisIntervalQueriesNum;
}
if (thisQueriesNum > 0) {
thisQueryAvgMs = thisQueriesMs/(double)thisQueriesNum;
}
if (thisQueriesStarted > 0)
{
long adjustedElapsed = now - thisQueriesStarted;
if (adjustedElapsed > 0)
{
thisAvgQPM = (double)thisQueriesNum/
((double)adjustedElapsed/1000.0/60.0);
}
if (thisIntervalMs > 0)
{
thisIntervalAvgQPM = (double)thisIntervalQueriesNum/
((double)thisIntervalMs/1000.0/60.0);
}
}
if (secondsPerFeedback > 0)
{
logMe("%,d inserts : %,d seconds : cum ips=%,.2f : int ips=
%,.2f : cum avg qry=%,.2f : int avg qry=%,.2f : cum avg qpm=%,.2f : int avg qpm=
%,.2f : exceptions=%,d", thisInserts, elapsed / 1000l, thisInsertsPerSecond,
thisIntervalInsertsPerSecond, thisQueryAvgMs, thisIntervalQueryAvgMs, thisAvgQPM,
thisIntervalAvgQPM, thisInsertExceptions);
} else {
logMe("%,d inserts : %,d seconds : cum ips=%,.2f : int ips=
%,.2f : cum avg qry=%,.2f : int avg qry=%,.2f : cum avg qpm=%,.2f : int avg qpm=
%,.2f : exceptions=%,d", intervalNumber * insertsPerFeedback, elapsed / 1000l,
thisInsertsPerSecond, thisIntervalInsertsPerSecond, thisQueryAvgMs,
thisIntervalQueryAvgMs, thisAvgQPM, thisIntervalAvgQPM, thisInsertExceptions);
}
try {
if (outputHeader)
{
writer.write("tot_inserts\telap_secs\tcum_ips\tint_ips\tcum_qry_avg\tint_qry_avg\tc
um_qpm\tint_qpm\texceptions\n");
outputHeader = false;
}
if (secondsPerFeedback > 0)
{
statusUpdate = String.format("%d\t%d\t%.2f\t%.2f\t
%.2f\t%.2f\t%.2f\t%.2f\t%,d\n",thisInserts, elapsed / 1000l, thisInsertsPerSecond,
thisIntervalInsertsPerSecond, thisQueryAvgMs, thisIntervalQueryAvgMs, thisAvgQPM,
thisIntervalAvgQPM, thisInsertExceptions);
} else {
statusUpdate = String.format("%d\t%d\t%.2f\t%.2f\t
%.2f\t%.2f\t%.2f\t%.2f\t%,d\n",intervalNumber * insertsPerFeedback, elapsed /
1000l, thisInsertsPerSecond, thisIntervalInsertsPerSecond, thisQueryAvgMs,
thisIntervalQueryAvgMs, thisAvgQPM, thisIntervalAvgQPM, thisInsertExceptions);
}
writer.write(statusUpdate);
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
lastInserts = thisInserts;
lastQueriesNum = thisQueriesNum;
lastQueriesMs = thisQueriesMs;
lastMs = now;
}
}
writer.write("tot_inserts\telap_secs\tcum_ips\tint_ips\tcum_qry_avg\tint_qry_avg\tc
um_qpm\tint_qpm\n");
outputHeader = false;
}
String statusUpdate = "";
if (secondsPerFeedback > 0)
{
statusUpdate = String.format("%d\t%d\t%.2f\t%.2f\t%.2f\t%.2f\t
%.2f\t%.2f\n",thisInserts, elapsed / 1000l, thisInsertsPerSecond,
thisIntervalInsertsPerSecond, thisQueryAvgMs, thisIntervalQueryAvgMs, thisAvgQPM,
thisIntervalAvgQPM);
} else {
statusUpdate = String.format("%d\t%d\t%.2f\t%.2f\t%.2f\t%.2f\t
%.2f\t%.2f\n",intervalNumber * insertsPerFeedback, elapsed / 1000l,
thisInsertsPerSecond, thisIntervalInsertsPerSecond, thisQueryAvgMs,
thisIntervalQueryAvgMs, thisAvgQPM, thisIntervalAvgQPM);
}
writer.write(statusUpdate);
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}