HashMap<String, SerializationStrategy> map = new HashMap<String, SerializationStrategy>();
DispatchQueue queue = Dispatch.createQueue();
ServerInvokerImpl server = new ServerInvokerImpl("tcp://localhost:0", queue, map);
server.start();
ClientInvokerImpl client = new ClientInvokerImpl(queue, map);
client.start();
try {
final HelloImpl helloImpl = new HelloImpl();
server.registerService("service-id", new ServerInvoker.ServiceFactory() {
public Object get() {
return helloImpl;
}
public void unget() {
}
}, HelloImpl.class.getClassLoader());
InvocationHandler handler = client.getProxy(server.getConnectAddress(), "service-id", HelloImpl.class.getClassLoader());
final Hello hello = (Hello) Proxy.newProxyInstance(HelloImpl.class.getClassLoader(), new Class[] { Hello.class }, handler);
assertEquals("Hello World!", hello.helloworld());
final AtomicInteger requests = new AtomicInteger(0);
final AtomicInteger failures = new AtomicInteger(0);
final long latencies[] = new long[BENCHMARK_CLIENTS * BENCHMARK_INVOCATIONS_PER_CLIENT];
final long start = System.nanoTime();
Thread[] threads = new Thread[BENCHMARK_CLIENTS];
for (int t = 0; t < BENCHMARK_CLIENTS; t++) {
final int thread_idx = t;
threads[t] = new Thread() {
public void run() {
for (int i = 0; i < BENCHMARK_INVOCATIONS_PER_CLIENT; i++) {
try {
requests.incrementAndGet();
String response;
final long start = System.nanoTime();
response = hello.hello("Fabric");
final long end = System.nanoTime();
latencies[(thread_idx* BENCHMARK_INVOCATIONS_PER_CLIENT)+i] = end-start;
assertEquals("Hello Fabric!", response);
} catch (Throwable t) {
latencies[(thread_idx* BENCHMARK_INVOCATIONS_PER_CLIENT)+i] = -1;
failures.incrementAndGet();
if (t instanceof UndeclaredThrowableException) {
t = ((UndeclaredThrowableException) t).getUndeclaredThrowable();
}
System.err.println("Error: " + t.getClass().getName() + (t.getMessage() != null ? " (" + t.getMessage() + ")" : ""));
}
}
}
};
threads[t].start();
}
for (int t = 0; t < BENCHMARK_CLIENTS; t++) {
threads[t].join();
}
final long end = System.nanoTime();
long latency_sum = 0;
for (int t = 0; t < latencies.length; t++) {
if( latencies[t] != -1 ) {
latency_sum += latencies[t];
}
}
double latency_avg = ((latency_sum * 1.0d)/requests.get()) / MILLIS_IN_A_NANO;
double request_rate = ((requests.get() * 1.0d)/(end-start)) * SECONDS_IN_A_NANO;
System.err.println(String.format("Requests/Second: %,.2f", request_rate));
System.err.println(String.format("Average request latency: %,.2f ms", latency_avg));
System.err.println("Error Ratio: " + failures.get() + " / " + requests.get());
}
finally {
server.stop();
client.stop();
}
}