Please see below.
There is a main class, which calls a Thread class.
I made two tests: first in the sub class with the actual couch query, then with a simple Thread.sleep().
I would expect that the test with the query would act the same as the Thread.sleep() test: when executed with 3 parallel threads - each thread executed 3 times would have a 3 times faster execution time than 1 thread executed 9 times. (and the average time per thread/query would remain the same, not almost 3 times slower than in the 3 parallel threads case).
Thank you.
package couch;
import com.couchbase.client.java.Bucket;
import com.couchbase.client.java.CouchbaseCluster;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class CouchbasePerfTestController {
public static void main(String args[]) throws Exception {
CouchbaseCluster cluster = CouchbaseCluster.create(Collections.singletonList("192.168.96.110"));
Bucket bucket = cluster.openBucket("Hotel", "xxxxxx");
try {
int maxThreads = 1; // OR the second test with 3 parallel threads
int batch = 9;
int threadIndex = 1;
int maxCycles = batch/maxThreads;
List<Thread> allThreads = new ArrayList<>();
long startTime = System.currentTimeMillis();
while (threadIndex <= maxThreads) {
CouchThread couchThread = new CouchThread();
couchThread.setCycles(maxCycles);
couchThread.setBucket(bucket);
Thread t = new Thread(couchThread);
t.start();
allThreads.add(t);
threadIndex++;
}
for (Thread t : allThreads) {
t.join();
}
long endTime = System.currentTimeMillis() - startTime;
double avg = endTime / (batch/maxThreads);
System.out.println("*** Total time " + endTime + " millis, average is " + avg + " millis ");
bucket.close();
cluster.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
}
package couch;
import com.couchbase.client.java.Bucket;
import com.couchbase.client.java.query.N1qlQuery;
public class CouchThread implements Runnable {
private int cycles;
private Bucket bucket;
public int getCycles() {
return cycles;
}
public void setCycles(int cycles) {
this.cycles = cycles;
}
public Bucket getBucket() {
return bucket;
}
public void setBucket(Bucket bucket) {
this.bucket = bucket;
}
public void run() {
int innerIndex = 1;
while (innerIndex <= cycles) {
long start = System.currentTimeMillis();
// System.out.println(“Current thread start time:” + Thread.currentThread().getId() + “, index:” + innerIndex + " — " + start);
//1. Call query
N1qlQuery query = N1qlQuery.simple("select meta().id, lon, lat, instances from Hotel where documentType ='bas' limit(10000)");
bucket.query(query);
//2. Thread sleep
// try {
// Thread.sleep(500);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
long end = System.currentTimeMillis();
System.out.println("Current thread time:" + Thread.currentThread().getId()+ ", index:" + innerIndex + " --- time: " + (end-start));
innerIndex++;
}
}
}