Created
April 23, 2014 12:16
-
-
Save abhi-bit/11212988 to your computer and use it in GitHub Desktop.
Couchbase Java SDK durability perf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.couchbase.client.CouchbaseClient; | |
import com.couchbase.client.CouchbaseConnectionFactoryBuilder; | |
import java.net.URI; | |
import java.util.ArrayList; | |
import java.util.concurrent.CountDownLatch; | |
import java.util.concurrent.TimeUnit; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
import net.spy.memcached.PersistTo; | |
import net.spy.memcached.ReplicateTo; | |
import net.spy.memcached.internal.OperationFuture; | |
import net.spy.memcached.internal.GetCompletionListener; | |
import net.spy.memcached.internal.GetFuture; | |
import net.spy.memcached.internal.OperationCompletionListener; | |
public class App { | |
private static String BUCKET = "test"; | |
public static void main(String[] args) throws Exception { | |
//couchbase server URIs | |
ArrayList<URI> uris = new ArrayList<URI>(); | |
uris.add(URI.create("http://127.0.0.1:8091/pools")); | |
CouchbaseConnectionFactoryBuilder cfb = new CouchbaseConnectionFactoryBuilder(); | |
cfb.setObsPollInterval(1000); | |
CouchbaseClient client = null; | |
try { | |
client = new CouchbaseClient(cfb.buildCouchbaseConnection(uris, BUCKET, "")); | |
} catch (Exception e) { | |
System.err.println("Error connecting to Couchbase: " + e.getMessage()); | |
System.exit(1); | |
} | |
long startTime, stopTime; | |
int noOfRuns = 1000; | |
// PersistTo 0, ReplicateTo 1 | |
startTime = System.nanoTime(); | |
run(client, 0, 1, noOfRuns); | |
stopTime = System.nanoTime(); | |
System.out.println("PersistoTo.ZERO, ReplicateTo.ONE :: Average over " + noOfRuns + " runs: " + TimeUnit.MICROSECONDS.convert(((stopTime - startTime) / noOfRuns), TimeUnit.NANOSECONDS) + " microseconds."); | |
// PersistTo 1 ReplicateTo 0 | |
startTime = System.nanoTime(); | |
run(client, 1, 0, noOfRuns); | |
stopTime = System.nanoTime(); | |
System.out.println("PersistoTo.ONE, ReplicateTo.ZERO :: Average over " + noOfRuns + " runs: " + TimeUnit.MICROSECONDS.convert(((stopTime - startTime) / noOfRuns), TimeUnit.NANOSECONDS) + " microseconds."); | |
// PersistTo 0 ReplicateTo 0 | |
startTime = System.nanoTime(); | |
run(client, 0, 0, noOfRuns); | |
stopTime = System.nanoTime(); | |
System.out.println("PersistoTo.ZERO, ReplicateTo.ZERO :: Average over " + noOfRuns + " runs: " + TimeUnit.MICROSECONDS.convert(((stopTime - startTime) / noOfRuns), TimeUnit.NANOSECONDS) + " microseconds."); | |
// Shutdown the client | |
client.shutdown(); | |
} | |
public static void run(CouchbaseClient client, int persistTo, int replicateTo, int noOfRuns) throws InterruptedException { | |
final CountDownLatch latch = new CountDownLatch(noOfRuns); | |
for (int i = 0; i < noOfRuns; i++){ | |
OperationFuture<Boolean> future ; | |
if (persistTo == 1 && replicateTo == 0) { | |
future = client.add(i + "_P1_R0", 0, "add_test_P1_R0" + i, PersistTo.ONE, ReplicateTo.ZERO); | |
} else if (persistTo == 0 && replicateTo == 1) { | |
future = client.add(i + "_P0_R1", 0, "add_test_P0_R1" + i, PersistTo.ZERO, ReplicateTo.ONE); | |
} else { | |
future = client.add(i + "_P0_R0", 0, "add_test_P0_R0" + i, PersistTo.ZERO, ReplicateTo.ZERO); | |
} | |
future.addListener(new OperationCompletionListener() { | |
@Override | |
public void onComplete(OperationFuture<?> future) throws Exception { | |
latch.countDown(); | |
} | |
}); | |
} | |
latch.await(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Results from local testing: