影響Java NIO框架性能的因數(shù)
最近打算用kilim做一個(gè)rpc框架, kilim有自己的nio框架 而在業(yè)界有強(qiáng)勁的netty和mina。
所以問(wèn)了一下kilim的作者,他的回答說(shuō) 因?yàn)榈讓佑玫亩际莏ava nio的api,所以留給nio框架最主要的問(wèn)題是這2點(diǎn):
(i) 為了處理很多socket連接和優(yōu)化吞吐量,會(huì)導(dǎo)致了大量的線程切換。
Amount of thread switching done to handle n numbers of sockets and optimizing for throughput.
(ii) 有很多次的selector的中斷和調(diào)用,喚醒seletor是很費(fèi)資源的操作。
所以能做的優(yōu)化有以下幾點(diǎn):
1、對(duì)read writer process操作分別作輕量級(jí)的scheduler,基于actor。
2、有個(gè)trick,就是read或者write操作時(shí)候,但沒(méi)有讀滿或者寫完的情況下,并不是立即返回并再次注冊(cè)channel到selector,而是再嘗試若干次(3次),再返回并注冊(cè)到selector。在mina中也有同樣的處理。不同之處在于kilim會(huì)yield的當(dāng)前task,而mina為了避免線程切換,只是做了簡(jiǎn)單的while循環(huán)。但目的都是減少線程切換和避免多次注冊(cè) selector。
mina 處理代碼
- for (int i = WRITE_SPIN_COUNT; i > 0; i --)
- {
- localWrittenBytes = ch.write(buf.buf());
- if (localWrittenBytes != 0 || !buf.hasRemaining())
- {
- break;
- }
- }
kilim nio的處理
- while (remaining > 0)
- {
- if (n == 0)
- {
- yieldCount++;
- if (yieldCount < YIELD_COUNT)
- {
- Task.yield(); // don't go back to selector yet.
- } else
- {
- pauseUntilWritable();
- yieldCount = 0;
- }
- }
- n = ch.write(buf);
- remaining -= n;
- }
除了上面說(shuō)的2個(gè)因素以外,還有有哪些因素會(huì)影響nio的性能?
原文如下:
I have not tested netty, but here's my experience. All NIO frameworks, Kilim included, are comparable because they use the same underlying NIO API. The difference in performance _may_ stem from the following two sources:
(i) Amount of thread switching done to handle n numbers of sockets and optimizing for throughput.
(ii) Number of times the selector is interrupted and invoked. Waking up the selector is an expensive operation.
The first one is entirely up to the user of the Kilim NIO library. A typical server request consists of one or more reads to accumulate the frame, processing the packet, and one or more writes to write a packet to the socket. One can split up this work between multiple schedulers if you wish. By default, all reading and writing is done outside of the selector's thread. Which brings me to the next point.
I have optimized the access to the selector, by avoiding using it as much as possible. If a socket read or write is unable to transfer any bytes, then the task simply yields. Other runnable tasks get a chance to run. When the task is subsequently resumed, it retries the operation. This goes on for a fixed number of times (3), and if it still fails, it sends a message to the selector thread to wake it up whenever the socket is ready. See the comments on kilim.nio.EndPoint.
Please keep us posted about your netty experiments.
原文地址:http://wangscu.iteye.com/blog/664688
【編輯推薦】