// })
.build();
final List<HostConnectionPool<TestClient>> hostPools = Lists.newArrayList(pool.getActivePools());
final TestDriver driver = new TestDriver.Builder()
.withIterationCount(0)
.withThreadCount(200)
// .withFutures(100, TimeUnit.MILLISECONDS)
.withCallsPerSecondSupplier(Suppliers.ofInstance(200))
// .withFutures(100, TimeUnit.MILLISECONDS)
.withCallback(new Function<TestDriver, Void>() {
public Void apply(final TestDriver driver) {
long startTime = System.nanoTime();
try {
pool.executeWithFailover(new TestOperation() {
public String execute(TestClient client) throws ConnectionException, OperationException {
try {
function.apply(driver);
return null;
}
catch (RuntimeException e) {
if (e.getCause() instanceof ConnectionException)
throw (ConnectionException)e.getCause();
throw e;
}
}
}, new RunOnce());
} catch (PoolTimeoutException e) {
LOG.info(e.getMessage());
} catch (ConnectionException e) {
} finally {
sampler.addSample((System.nanoTime() - startTime)/1000000);
}
return null;
}
})
//
// Event to turn timeouts on/off
//
.withRecurringEvent(10, TimeUnit.SECONDS, new Function<TestDriver, Void>() {
@Override
public Void apply(TestDriver driver) {
timeoutsEnabled.getAndSet(!timeoutsEnabled.get());
// LOG.info("Toggle timeouts " + timeoutsEnabled.get());
return null;
}
})
//
// Print status information
//
.withRecurringEvent(1, TimeUnit.SECONDS, new Function<TestDriver, Void>() {
@Override
public Void apply(TestDriver driver) {
long opCount = lastOperationCount.get();
lastOperationCount.set(driver.getOperationCount());
System.out.println("" + driver.getRuntime() + "," + sampler.getScore() + "," + (lastOperationCount.get() - opCount));
System.out.println(monitor.toString());
System.out.println(monitor.toString());
for (HostConnectionPool<TestClient> host : pool.getPools()) {
System.out.println(" " + host.toString());
}
return null;
}
})
//
// Remove a random host
//
.withRecurringEvent(10, TimeUnit.SECONDS, new Function<TestDriver, Void>() {
@Override
public Void apply(TestDriver driver) {
// System.out.println("Latency: " + sampler.getScore());
//
// List<Host> newHosts = Lists.newArrayList(hosts);
// newHosts.remove(new Random().nextInt(hosts.size()));
// pool.setHosts(newHosts);
//
// System.out.println(monitor.toString());
// for (HostConnectionPool<TestClient> host : pool.getPools()) {
// System.out.println(" " + host.toString());
// }
return null;
}
})
.build();
driver.start();
try {
driver.await();
} catch (InterruptedException e) {
}
}