Skip to content

Instantly share code, notes, and snippets.

@ingenthr
Last active December 28, 2015 06:59
Show Gist options
  • Save ingenthr/7460611 to your computer and use it in GitHub Desktop.
Save ingenthr/7460611 to your computer and use it in GitHub Desktop.
A quick test to determine the number of threads created by a CouchbaseClient.
package com.couchbase.client.demo;
import com.couchbase.client.CouchbaseClient;
import java.io.IOException;
import java.net.URI;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
public class Main {
private static int numBuckets = 1;
private static CouchbaseClient clients[];
private static List<URI> baseList;
public static void main(String args[]) throws InterruptedException {
baseList = Arrays.asList(
URI.create("http://192.168.1.200:8091/pools"),
URI.create("http://192.168.1.201:8091/pools"));
clients = new CouchbaseClient[numBuckets];
Thread.sleep(100);
System.out.println("Before starting test...");
printThreads();
createClients();
Thread.sleep(1500);
System.out.println("Post creating clients...");
printThreads();
System.out.println("Shutting down...");
shutdownClients();
System.exit(0);
}
private static void printThreads() {
int currThreads = Thread.getAllStackTraces().keySet().size();
int runningThreads = 0;
for (Thread t : Thread.getAllStackTraces().keySet()) {
if (t.getState()==Thread.State.RUNNABLE) runningThreads++;
}
System.out.println("Threads: " + runningThreads + "/" + currThreads + " (run/total)");
}
public static void createClients() {
for (int i=0; i<numBuckets; i++) {
String bucketName = "bucket" + i;
System.out.println("creating " + bucketName);
try {
clients[i] = new CouchbaseClient(baseList, bucketName, bucketName);
} catch (IOException e) {
System.err.println("Failed to create client: " + e.getCause());
}
}
}
public static void shutdownClients() {
for (int i=0; i<numBuckets; i++) {
String bucketName = "bucket" + i;
clients[i].shutdown(1, TimeUnit.SECONDS);
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.couchbase.client.demo</groupId>
<artifactId>ThreadConnectionTest</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.couchbase.client</groupId>
<artifactId>couchbase-client</artifactId>
<version>1.2.0</version>
</dependency>
</dependencies>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment