Skip to content

Instantly share code, notes, and snippets.

@btm

btm/jcouch.java

Created Feb 28, 2009
Embed
What would you like to do?
/*
* Copyright 2009 Bryan McLellan (btm@loftninjas.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
/*
* jcouch.sh /path/to/binary/file
*
* sudo apt-get install couchdb
* sudo apt-get install sun-java5-jdk
* sudo apt-get install liblog4j1.2-java libcommons-httpclient-java libcommons-logging-java libcommons-beanutils-java libcommons-codec-java
*
* java -cp .:jcouchdb-0.7.2.jar:svenson-1.2.8.jar:/usr/share/java/log4j-1.2.jar:/usr/share/java/commons-httpclient.jar:/usr/share/java/commons-logging.jar:/usr/share/java/commons-beanutils.jar:/usr/share/java/commons-codec.jar jcouch $*
*/
import org.jcouchdb.db.Database;
import org.jcouchdb.db.ServerImpl;
import java.io.*;
import java.util.*;
import java.lang.*;
import java.lang.management.*;
import java.util.concurrent.TimeUnit;
import org.apache.commons.codec.binary.Base64;
class jcouch {
static String dbHost = "localhost";
static String dbName = "test-bytearray";
public static void main(String args[]) {
boolean writeBase64 = false;
int numThreads = -1;
int maxThreads = 0;
if (args.length < 1) {
System.out.println("usage: java [lib stuff] jcouch file threads");
System.out.println(" file: binary file to write to couch");
System.out.println(" threads: number of simultaneous threads");
System.exit(1);
}
String fileName = args[0];
if (args.length == 2) {
numThreads = Integer.valueOf(args[1]).intValue();
}
System.out.println("Threads,MilliSeconds,Iterations,Base64?,thread bytes,thread kbytes/sec");
// Scale up threads if not provided
if (numThreads == -1) {
numThreads = 1;
maxThreads = 10;
} else {
maxThreads = numThreads;
}
// Get some data from passed file
byte[] data = loadFile(fileName);
for (int x = numThreads; x <= maxThreads; x = x * 2) {
numThreads = x;
resetDatabase(fileName, data);
System.out.println("\nbase64 read test");
runTest(numThreads, data, true, "read");
resetDatabase(fileName, data);
System.out.println("\nbytearray read test");
runTest(numThreads, data, false, "read");
resetDatabase(fileName, data);
System.out.println("\nattach read test");
runTest(numThreads, data, false, "getattach");
resetDatabase(fileName, data);
System.out.println("\nbase64 write test");
runTest(numThreads, data, true, "write");
resetDatabase(fileName, data);
System.out.println("\nbytearray write test");
runTest(numThreads, data, false, "write");
resetDatabase(fileName, data);
System.out.println("\nattach write test");
runTest(numThreads, data, false, "attach");
System.out.print('\n');
}
}
static void resetDatabase(String fileName, byte[] data) {
// Start with a fresh database
ServerImpl couch = new ServerImpl(dbHost);
try {
couch.deleteDatabase(dbName);
} catch (org.jcouchdb.exception.CouchDBException e) {
}
couch.createDatabase(dbName);
// Put in documents to read
Database db = new Database(dbHost, dbName);
putDocument(db, data, true, "readtest_base64");
putDocument(db, data, false, "readtest_binarray");
putAttachment(db, data, "readtest_attach", "data");
}
static void runTest(int numThreads, byte[] data, boolean writeBase64, String action) {
jcouch j = new jcouch();
Thread[] children = new Thread[ numThreads ];
for (int i = 0; i < children.length ; i++) {
children[i] = new Thread(j.getThread(data, writeBase64, action));
children[i].start();
}
for (int i = 0; i < children.length; i++) {
try {
children[i].join();
} catch (InterruptedException e) {
System.out.println("error: interrupted: " + e);
}
}
}
static byte[] loadFile(String fileName) {
byte bytearray[] = null;
try {
File f = new File(fileName);
FileInputStream fis = new FileInputStream(f);
int numberBytes = fis.available();
bytearray = new byte[numberBytes];
fis.read(bytearray);
fis.close();
System.out.println("-- File: " + fileName + " Size: " + numberBytes + " bytes");
} catch (IOException e) {
System.out.println("file exception: " + e);
}
return bytearray;
}
static void putDocument(Database db, byte[] data, boolean writeBase64) {
putDocument(db, data, writeBase64, null);
}
static void putDocument(Database db, byte[] data, boolean writeBase64, String id) {
// create a hash map document with two fields
Map<String,Object> doc = new HashMap<String,Object>();
if (id != null) {
doc.put("_id", id);
}
if (writeBase64) {
//byte dataBase64[] = Base64.encodeBase64(data);
String dataString = new String(Base64.encodeBase64(data));
doc.put("data", dataString);
} else {
doc.put("data", data);
}
// create the document in couchdb
db.createDocument(doc);
}
static void putAttachment(Database db, byte[] data) {
String docId = UUID.randomUUID().toString();
String attId = UUID.randomUUID().toString();
putAttachment(db, data, docId, attId);
}
static void putAttachment(Database db, byte[] data, String docId, String attId) {
db.createAttachment(docId, null, attId, "image/jpeg", data);
}
public myThread getThread(byte[] data, boolean writeBase64, String action) {
return new myThread(data, writeBase64, action);
}
class myThread implements Runnable {
Database db = new Database(dbHost, dbName);
int numLoops = 100;
byte[] m_data;
boolean m_writeBase64;
String m_action;
public myThread(byte[] data, boolean writeBase64, String action) {
m_data = data;
m_writeBase64 = writeBase64;
m_action = action;
}
public void run() {
ThreadMXBean mx = ManagementFactory.getThreadMXBean();
long start = System.nanoTime();
if (m_action.equals("write")) {
for ( int i = 0 ; i < numLoops ; i++ ) {
putDocument(db, m_data, m_writeBase64);
}
}
if (m_action.equals("read")) {
String docName = "";
if (m_writeBase64 == true) {
docName = "readtest_base64";
} else {
docName = "readtest_binarray";
}
Map<String,Object> doc = new HashMap<String, Object>();
for ( int i = 0 ; i < numLoops ; i++ ) {
doc = db.getDocument(Map.class, docName);
}
Object o = doc.get("data");
if (o == null) {
System.out.println("o is null");
}
//if (m_writeBase64 == false) {
}
if (m_action.equals("attach")) {
for ( int i = 0 ; i < numLoops ; i++ ) {
putAttachment(db, m_data);
}
}
if (m_action.equals("getattach")) {
byte[] t_data = db.getAttachment("readtest_attach", "data");
if (Arrays.equals(m_data,t_data) == false) {
System.out.print('!');
}
}
long duration = System.nanoTime() - start;
System.out.println(Thread.activeCount() + "," + TimeUnit.NANOSECONDS.toMillis(duration) + "," +
numLoops + "," + m_writeBase64 + "," + (m_data.length * numLoops) + "," +
(m_data.length * numLoops / TimeUnit.NANOSECONDS.toMillis(duration) ) );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment