users@grizzly.java.net

Re: performance

From: Jeanfrancois Arcand <Jeanfrancois.Arcand_at_Sun.COM>
Date: Wed, 29 Apr 2009 19:01:59 -0400

Salut,

Apology...completely forgot to look at the your code. Are you still
interested. Really sorry...

Thanks

-- Jeanfrancois

bill_joe wrote:
> here is my server code
>
> TCPSelectorHandler tcp_handler = new TCPSelectorHandler();
> Controller controller = new Controller();
> tcp_handler.setSelectionKeyHandler(new
> DefaultSelectionKeyHandler());
> tcp_handler.setTcpNoDelay(true);
> tcp_handler.setReuseAddress(true);
> controller.setSelectorHandler(tcp_handler);
> ProtocolFilter ping = new PingFilter();
> ProtocolFilter read = new ReadFilter();
> controller.setProtocolChainInstanceHandler(new
> DefaultProtocolChainInstanceHandler() {
> public ProtocolChain poll() {
> ProtocolChain protocol_chain =
> protocolChains.poll();
> if (protocol_chain == null) {
> protocol_chain = new DefaultProtocolChain();
> protocol_chain.addFilter(read);
> protocol_chain.addFilter(ping);
> }
> return protocol_chain;
> }
> });
> final DefaultThreadPool pool = new DefaultThreadPool();
> pool.setCorePoolSize(30);
> pool.setMaximumPoolSize(100);
> pool.setByteBufferType(ByteBufferType.DIRECT);
> pool.setThreadFactory(new ThreadFactory() {
> public Thread newThread(Runnable r) {
> WorkerThreadImpl thread = new WorkerThreadImpl(pool,
> pool.getName() + "-WorkerThread()", r, pool.getInitialByteBufferSize());
> thread.setUncaughtExceptionHandler(pool);
> thread.setByteBufferType(ByteBufferType.DIRECT);
>
> thread.setByteBuffer(ByteBufferFactory.allocate(ByteBufferType.DIRECT,
> 8096));
> return thread;
> }
>
> });
> controller.setThreadPool(pool);
> controller.setReadThreadsCount(30);
>
> pool.execute(controller);
>
> Here is my ping filter on the server
>
> public boolean execute(Context ctx) throws IOException {
> int id = 0;
> ByteBuffer buffer = workerThread.getByteBuffer();
> buffer.flip();
> if (buffer.hasRemaining()) {
> .... parse the buffer to get the id ...
> }
> final WorkerThread workerThread =
> ((WorkerThread)Thread.currentThread());
> SelectableChannel channel = ctx.getSelectionKey().channel();
> final ByteBuffer buffer = workerThread.getByteBuffer();
>
> buffer.clear();
> buffer.putInt(id);
>
> bufer.flip();
> OutputWriter.flushChannel(channel, bufer);
> bufer.clear();
>
>
> here is my code for the client
> init() {
> ProtocolFilter pingResponse = new PingResponseFilter();
> connectorHandler.setController(controller);
> tcp_handler.setTcpNoDelay(true);
> tcp_handler.setReuseAddress(true);
>
> tcp_handler.setSelectionKeyHandler(new
> DefaultSelectionKeyHandler());
> controller.setSelectorHandler(tcp_handler);
> controller.setProtocolChainInstanceHandler(new
> DefaultProtocolChainInstanceHandler() {
> public ProtocolChain poll() {
> ProtocolChain protocol_chain =
> protocolChains.poll();
> if (protocol_chain == null) {
> protocol_chain = new DefaultProtocolChain();
> protocol_chain.addFilter(read);
> protocol_chain.addFilter(pingResponse);
>
> }
> return protocol_chain;
> }
> }); // end overridden method
> final DefaultThreadPool pool = new DefaultThreadPool();
> pool.setCorePoolSize(30);
> pool.setMaximumPoolSize(100);
> pool.setByteBufferType(ByteBufferType.DIRECT);
> pool.setThreadFactory(new ThreadFactory() {
> public Thread newThread(Runnable r) {
> WorkerThreadImpl thread = new WorkerThreadImpl(pool,
> pool.getName() + "-WorkerThread()", r, pool.getInitialByteBufferSize());
> thread.setUncaughtExceptionHandler(pool);
> thread.setByteBufferType(ByteBufferType.DIRECT);
>
> thread.setByteBuffer(ByteBufferFactory.allocate(ByteBufferType.DIRECT,
> 8096));
> return thread;
> }
>
> });
> controller.setThreadPool(pool);
> controller.setReadThreadsCount(30);
> controller.addStateListener(new ControllerStateListenerAdapter() {
>
> public void onException(Throwable e) {
> }
>
> public void onReady() {
> started.countDown();
> }
> });
>
>
> pool.execute(controller);
> }
>
> public void ping() throws Exception {
> BlockingQueue blocking = new ArrayBlockingQueue(1);
> int id = getRequestId();
> requests.put(Integer.toString(id), blocking);
> byte[]array = ... insert the id into the byte array
> ByteBuffer byteBuffer = ByteBuffer.wrap(array, 0, array.length);
> connectorHandler.writeToAsyncQueue(byteBuffer);
> blocking.take();
> }
>
> I chopped up the code and kept the relevant parts
>
> The client connects, then keeps calling ping over and over.
> The client filter PingResponse reads the buffer and wakes up the thread that
> initiated the ping.