`
san_yun
  • 浏览: 2662719 次
  • 来自: 杭州
文章分类
社区版块
存档分类
最新评论

dboss的非阻塞IO

 
阅读更多

一个良好的服务框架是不应该让业务逻辑阻塞IO操作的。



 比如客户端调用一个远程业务方法需要500ms才能返回。那么server的其他io线程不应该受到影响,同样能在500ms返回。

 

现在是一段测试代码:

python:

import threading

DBOSS={
 "dboss":{
    "locations":["localhost:8989"],
    "references":[
        {
            "id":"blogQueryService",
            "version":"1.0"
        }
    ]
  },
}


from dboss import  DuitangRemoteProxy
proxy = DuitangRemoteProxy(DBOSS)
blogQueryService = proxy.getService('blogQueryService')
import time
from time import sleep


class TestThread(threading.Thread):
		
	def run(self):  
		while True:
			starttime = time.time()					
			blogQueryService.testString("ok")
			endtime = time.time()
			exe_time = (endtime - starttime)*1000
			print 'cost %s'%exe_time
			sleep(0.1)

for i in xrange(5):  
	t = TestThread()  
	t.start()    

 

java:

public class BlogQueryServiceImpl implements BlogQueryService {

	public String testString(String str) {
		System.out.println(Thread.currentThread().getName());
		try {
			Thread.sleep(500);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return str;
	}

}

 

dboss:

public class DbossTester {

    public static void main(String[] args) {

        ServiceRemoting serviceRemoting = new NioServiceRemoting();
        DataResolverFactory dataResolverFactory = DataResolvers.getDataResolverFactory();
        ServiceRegister serviceRegister = new ServiceRegister();
        serviceRegister.registe("blogQueryService", new BlogQueryServiceImpl());
        serviceRemoting.init(new ServiceContextImpl(serviceRegister, dataResolverFactory));
        serviceRemoting.start(8989);
    }
}

 

测试结果:

python:
Thread-1 cost 504.622936249
Thread-2 cost 509.635925293
Thread-3 cost 509.853124619
Thread-4 cost 510.01906395
Thread-5 cost 1002.29907036
Thread-1 cost 501.371860504
Thread-2 cost 501.34396553
Thread-3 cost 500.792980194
Thread-4 cost 793.460130692
Thread-1 cost 501.65104866
Thread-2 cost 501.611948013
Thread-3 cost 501.570940018
Thread-5 cost 801.206111908
Thread-4 cost 801.896095276
Thread-2 cost 501.791000366
java:

New I/O server worker #1-1
New I/O server worker #1-2
New I/O server worker #1-2
New I/O server worker #1-1
New I/O server worker #1-4
New I/O server worker #1-3
New I/O server worker #1-2
New I/O server worker #1-4
New I/O server worker #1-3
New I/O server worker #1-1
New I/O server worker #1-2

 

由于netty默认只启动4个io thread,当有超过4个connection请求时,就会出现io被阻塞的情况,所有python端出现1000ms的情况。

通常不应该让io thread直接处理work,下面是测试20个并发,biz service耗时10ms的情况:

Thread-9 cost 21.9979286194
Thread-5 cost 11.8389129639
Thread-13 cost 21.8431949615
Thread-10 cost 12.4440193176
Thread-18 cost 21.8789577484
Thread-19 cost 19.7048187256
Thread-16 cost 11.6169452667
Thread-7 cost 11.8520259857
Thread-1 cost 12.0470523834
Thread-17 cost 11.5780830383
 Thread-2 cost 11.9531154633
Thread-3 cost 11.9800567627
Thread-11 cost 11.9431018829
Thread-14 cost 12.2349262238
Thread-12 cost 11.5549564362
Thread-20 cost 12.4790668488
Thread-8 cost 11.3768577576
Thread-4 cost 11.3980770111
Thread-6 cost 11.8780136108
Thread-15 cost 11.5919113159
Thread-9 cost 13.9129161835
Thread-13 cost 11.4369392395
Thread-5 cost 21.9550132751

 netty提供了ExecutionHandler来处理:

pipeline.addLast("executor", new ExecutionHandler(new OrderedMemoryAwareThreadPoolExecutor(16, 1048576, 1048576)));

python:

Thread-4 cost 11.6369724274
Thread-5 cost 12.0570659637
Thread-6 cost 11.7981433868
Thread-7 cost 11.6400718689
Thread-8 cost 11.7359161377
Thread-9 cost 11.7650032043
Thread-12 cost 11.8789672852
Thread-11 cost 11.7127895355
Thread-14 cost 11.8279457092
Thread-13 cost 11.8789672852
 Thread-10 cost 11.7909908295
Thread-15 cost 11.3301277161
Thread-17 cost 11.6529464722
Thread-18 cost 12.1729373932
Thread-16 cost 12.4118328094
Thread-19 cost 11.7380619049
Thread-20 cost 11.757850647
Thread-3 cost 12.0511054993
Thread-1 cost 11.9280815125
Thread-2 cost 12.1569633484
Thread-4 cost 11.3790035248

java:

pool-41-thread-14
pool-42-thread-1
pool-42-thread-3
pool-41-thread-16
pool-39-thread-14
pool-42-thread-5
pool-42-thread-7
pool-39-thread-16
pool-41-thread-2
pool-38-thread-14
pool-41-thread-4
pool-39-thread-2
pool-42-thread-9
pool-38-thread-16
pool-40-thread-2
pool-42-thread-11
pool-39-thread-4
pool-40-thread-4
pool-39-thread-6
pool-42-thread-13
pool-38-thread-2
pool-40-thread-6
pool-40-thread-8
pool-38-thread-4
pool-42-thread-15
pool-39-thread-8
pool-39-thread-10
pool-42-thread-1
pool-38-thread-6
pool-40-thread-10
pool-38-thread-8
pool-42-thread-3
pool-39-thread-12

 

 

 

  • 大小: 16.9 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics