原文出处:http://blog.chenlb.com/2009/05/lucene-solr-fsdirectory-niofsdirectory-performance-and-http11nioprotocol.html
lucene2.4开始有一个NIOFSDirectory实现,使用java.nio'sFileChannel读取文件。官方说:在大多数非windows平台下,多个线程共用单个searcher比FSDirectory(在同一时刻只能一个线程使用searcher)可以提高查询的吞吐量。
lucene2.4的CHANGE.TXT说明:
21.LUCENE-753:AddednewDirectoryimplementation
org.apache.lucene.store.NIOFSDirectory,whichusesjava.nio's
FileChanneltodofilereads.Onmostnon-Windowsplatforms,with
manythreadssharingasinglesearcher,thismayyieldsizable
improvementtoquerythroughputwhencomparedtoFSDirectory,
whichonlyallowsasinglethreadtoreadfromanopenfileata
time.(JasonRutherglenviaMikeMcCandless)
下面来测试下NIOFSDirectory能带来多少的提高,在solr1.3里测试。在基本相同情况下,分别对FSDirectory、NIOFSDirectory、NIOFSDirectory+Http11NioProtocol(tomcat)进行测试。用jmeter开100个线程的测试结果:
FSDirectory的
FSDirectory,点击放大
NIOFSDirectory的
NIOFSDirectory,点击放大
NIOFSDirectory+Http11NioProtocol(tomcat)
NIOFSDirectory+Http11NioProtocol(tomcat),点击放大
吞吐量一一提高,关于jmeter测试报告的说明请看:http://blog.chenlb.com/2009/03/jmeter-report-explain.html
单纯从数字上看,NIOFSDirectory比FSDirectory提高了(80.7-67.2)/67.2=20%,并且平均的响应时间也有提高,提高了:1313-1086=227ms。效果还是不错的。使用Http11NioProtocol(tomcat)可以进一步地提高吞吐量。
延伸:
1、在lucene/solr中使用NIOFSDirectory,因程序或solr编写代码时是用FSDirectory,很难对修改代码来指定使用NIOFSDirectory,幸好lucene作者们已经考虑到这个问题,可以用系统属性(Systemproperty)org.apache.lucene.FSDirectory.class指定使用那个FSDirectory,如tomcat的启动脚本bin/startup.sh最上面加:
JAVA_OPTS="$JAVA_OPTS-Dorg.apache.lucene.FSDirectory.class=org.apache.lucene.store.NIOFSDirectory"
exportJAVA_OPTS
2、tomcat使用Http11NioProtocol。在conf/server.xml里修改,如下:
<Connectorport="8080"protocol="org.apache.coyote.http11.Http11NioProtocol"
maxThreads="100"connectionTimeout="10000"redirectPort="8443"
enableLookups="false"minSpareThreads="16"maxSpareThreads="64"acceptCount="0"debug="0"
useURIValidationHack="false"URIEncoding="UTF-8"/>
注意:官方不推荐在windows开台下用NIOFSDirectory,因为windows下java的NIO有个bug:http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6265734