自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

影響Java NIO框架性能的因數(shù)

開發(fā) 后端
最近打算用kilim做一個(gè)rpc框架, kilim有自己的nio框架而在業(yè)界有強(qiáng)勁的netty和mina。

最近打算用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 處理代碼

  1. for (int i = WRITE_SPIN_COUNT; i > 0; i --)  
  2. {  
  3. localWrittenBytes = ch.write(buf.buf());  
  4. if (localWrittenBytes != 0 || !buf.hasRemaining())  
  5. {  
  6. break;  
  7. }  

kilim nio的處理

  1. while (remaining > 0)  
  2. {  
  3. if (n == 0)  
  4. {  
  5. yieldCount++;  
  6. if (yieldCount < YIELD_COUNT)  
  7. {  
  8. Task.yield(); // don't go back to selector yet.  
  9. else  
  10. {  
  11. pauseUntilWritable();  
  12. yieldCount = 0;  
  13. }  
  14. }  
  15. n = ch.write(buf);  
  16. 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

【編輯推薦】

  1. java.nio.Buffer的一些基礎(chǔ)知識(shí)的備忘
  2. Java與Cobol對(duì)決:Cobol軟件質(zhì)量最過(guò)硬
  3. 甲骨文Java專利遭拒 起訴Android侵權(quán)受挫
  4. Java企業(yè)應(yīng)用問(wèn)題代碼最多
  5. 微軟警告稱Java乃入侵目標(biāo)之首
責(zé)任編輯:林師授 來(lái)源: wang_scu的博客
相關(guān)推薦

2011-12-15 09:55:47

javanio

2015-06-19 12:17:49

JAVA性能影響

2014-02-11 14:36:14

UPS

2023-07-12 08:24:19

Java NIO通道

2011-03-11 09:51:47

Java NIO

2015-06-26 09:27:14

Java調(diào)用性能

2011-12-23 11:33:25

JavaGrizzly

2011-12-15 13:28:57

2009-11-30 09:40:23

Java 7 NIO2HTTP Server

2022-11-17 08:00:18

JavaScript錯(cuò)誤性能

2022-12-15 08:00:38

JavaScript錯(cuò)誤性能

2019-05-17 09:05:54

MySQL查詢性能數(shù)據(jù)庫(kù)

2011-12-07 16:12:29

JavaNIO

2013-07-25 14:56:37

JavaEE 性能

2011-08-31 10:54:25

Java性能

2013-06-21 14:36:02

JavaEEx性能

2011-04-15 10:26:38

JavaMVC

2013-06-28 09:45:58

vSphere虛擬機(jī)

2012-05-07 08:18:42

程序日志性能

2021-06-28 17:21:49

MySQL性能Java
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)