Skip to content

Instantly share code, notes, and snippets.

@afeinberg
Created October 20, 2009 02:16
Show Gist options
  • Save afeinberg/213934 to your computer and use it in GitHub Desktop.
Save afeinberg/213934 to your computer and use it in GitHub Desktop.
package voldemort.performance;
import voldemort.VoldemortException;
import voldemort.utils.pool.KeyedResourcePool;
import voldemort.utils.pool.ResourceFactory;
import voldemort.utils.pool.ResourcePoolConfig;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
public class ResourcePoolCorrectnessTest {
final static int NUM_KEYS = 5;
final static int NUM_THREADS = 65;
final static int NUM_REQUESTS = 100000;
public static void main (String[] args) throws Exception {
final KeyedResourcePool<Integer, ExceptionThrowingResource> pool = KeyedResourcePool.create(
new ExceptionThrowingResouceFactory(),
new ResourcePoolConfig().setMaxPoolSize(NUM_KEYS)
.setTimeout(100, TimeUnit.MILLISECONDS)
.setIsFair(true)
);
ExecutorService executor = Executors.newFixedThreadPool(NUM_THREADS);
// Create a long running thread
executor.execute(new Runnable() {
public void run() {
try {
ExceptionThrowingResource r = pool.checkout(0);
//Thread.sleep(10 * 1000);
try {
r.doSomething();
} catch (VoldemortException e) {
System.out.println(e);
} finally {
pool.checkin(0, r);
}
} catch (Exception e) {
System.out.println(e);
}
}
});
for (int i=0; i < NUM_REQUESTS; i++) {
final int key = i % NUM_KEYS;
executor.execute(new Runnable() {
private final Random generator = new Random();
public void run() {
try {
ExceptionThrowingResource r = pool.checkout(key);
try {
r.doSomething();
Thread.sleep(generator.nextInt(10) * 10);
}
catch (VoldemortException ve) {
System.out.println(ve);
}
finally {
pool.checkin(key, r);
}
}
catch (Exception e) {
System.out.println(e);
}
}
});
}
executor.shutdown();
}
private static class ExceptionThrowingResource {
private final Random generator = new Random();
public void doSomething() {
int r = generator.nextInt(100);
try {
Thread.sleep(r * 100);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
if (r == 5) {
throw new VoldemortException("doSomething()");
}
}
public void destructor() {
}
}
private static class ExceptionThrowingResouceFactory implements
ResourceFactory<Integer,ExceptionThrowingResource> {
private final Random generator = new Random();
AtomicBoolean failMe = new AtomicBoolean(false);
public ExceptionThrowingResource create(Integer key) throws Exception {
if (failMe.get()) {
Thread.sleep(10000);
failMe.set(false);
}
int r = generator.nextInt(10);
Thread.sleep(r * 100);
return new ExceptionThrowingResource();
}
public void destroy(Integer key, ExceptionThrowingResource obj) throws Exception {
obj.destructor();
}
public boolean validate(Integer key, ExceptionThrowingResource value) {
int r = generator.nextInt(10);
if (r == 7) {
System.out.println("Not going to validate.");
failMe.set(true);
return false;
}
return true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment