客户端的响应时间比tomcat中的QTime长得多

本文关键字:QTime 中的 tomcat 响应时间 客户端 | 更新日期: 2023-09-27 17:54:13

我有一个solr网站,大约有500个文档(模式中定义的30个字段),在同一台机器上有一个c#客户端,它将向该solr网站发送http get请求。这些日志是由我的c#客户端记录的:

01-16 23:54:49,301 [107] INFO LogHelper - requst time too long: 1054, solr time: 1003
01-16 23:54:49,847 [63] INFO LogHelper - requst time too long: 1068, solr time: 1021
01-16 23:57:17,813 [108] INFO LogHelper - requst time too long: 1051, solr time: 1027
01-16 23:57:18,313 [111] INFO LogHelper - requst time too long: 1031, solr time: 1007
and so on…

可以看到,从solr查询的时间非常长,而且非常相似(在1000ms到1050ms之间)。同时,tomcat中相应的日志:

2013-1-16 23:54:49 org.apache.solr.core.SolrCore execute
Info: [suit1] webapp=/vanclsearchV2 path=/select/ params={fl=id,typeid,createtime,vprice,sprice,price,totalassesscount,totalsalescount,productcode,productname,stylecode,tag,vpricesku,spricesku,pricesku,userrate,assesscount,lstphotos,mainphotos,salesflag,isduanma,detailsalescount,productplusstyleinfo&sort=createtime+desc&start=0&q=*:*&wt=json&fq=ancestorsid:(28976+OR+28978)&fq=typeid:(1)&rows=30} hits=43 status=0 QTime=0 
2013-1-16 23:54:49 org.apache.solr.core.SolrCore execute
Info: [suit1] webapp=/vanclsearchV2 path=/select/ params={fl=id,typeid,createtime,vprice,sprice,price,totalassesscount,totalsalescount,productcode,productname,stylecode,tag,vpricesku,spricesku,pricesku,userrate,assesscount,lstphotos,mainphotos,salesflag,isduanma,detailsalescount,productplusstyleinfo&sort=createtime+desc&start=0&q=*:*&wt=json&fq=ancestorsid:(28976+OR+28978)&fq=typeid:(1)&rows=30} hits=43 status=0 QTime=0
2013-1-16 23:57:17 org.apache.solr.core.SolrCore execute
Info: [suit1] webapp=/vanclsearchV2 path=/select/ params={fl=id,typeid,createtime,vprice,sprice,price,totalassesscount,totalsalescount,productcode,productname,stylecode,tag,vpricesku,spricesku,pricesku,userrate,assesscount,lstphotos,mainphotos,salesflag,isduanma,detailsalescount,productplusstyleinfo&sort=createtime+desc&start=0&q=*:*&wt=json&fq=ancestorsid:(27547+OR+27614)&rows=30} hits=9 status=0 QTime=0 
2013-1-16 23:57:18 org.apache.solr.core.SolrCore execute
Info: [suit1] webapp=/vanclsearchV2 path=/select/ params={fl=id,typeid,createtime,vprice,sprice,price,totalassesscount,totalsalescount,productcode,productname,stylecode,tag,vpricesku,spricesku,pricesku,userrate,assesscount,lstphotos,mainphotos,salesflag,isduanma,detailsalescount,productplusstyleinfo&sort=createtime+desc&start=0&q=*:*&wt=json&fq=ancestorsid:(27547+OR+27614)&rows=30} hits=9 status=0 QTime=0

每一个奇怪的,所有的QTime都是零!谁能解释一下这种情况,以及如何解决这个问题?

string QUERY_TEMPLATE = ConfigurationManager.AppSettings["solr-select-url"] + "/select/?fl={0}&q={1}{2}&start={3}&rows={4}&sort={5}&wt=json";
WebRequest request = HttpWebRequest.Create(string.Format(QUERY_TEMPLATE,
                                                requestInfo.BrowserType==BrowserTypeEnum.Style?STYLE_FIELDS:COLOR_FIELDS,
                                                string.IsNullOrWhiteSpace(requestInfo.KeyWord) ? "*:*" : requestInfo.KeyWord,
                                                filterQuery,
                                                (requestInfo.Page - 1) * requestInfo.RowsCount,
                                                requestInfo.RowsCount,
                                                sortFiled)
                                            );
request.Method="GET";
string resultString=null;
Stopwatch solrWatch = new Stopwatch();
solrWatch.Start();
using (WebResponse response = request.GetResponse())
{
    using (Stream stream = response.GetResponseStream())
    {
        using (StreamReader reader = new StreamReader(stream))
        {
            resultString = reader.ReadToEnd();
        }
    }
}
solrWatch.Stop();
solrTime = solrWatch.ElapsedMilliseconds;

客户端的响应时间比tomcat中的QTime长得多

QTime是Solr进行搜索的时间。其余时间用于生成响应,包括从磁盘获取字段的存储内容并将其生成到JSON表单中。

我将研究以下几件事:

  • 使用SolrNet库访问Solr,而不是自己进行查询和解析响应
  • 检查您是否正在发送调试信息并将其关闭;查看返回的JSON并在solrconfig.xml
  • 中进行配置
  • 检查您是否真的需要响应中的所有字段
  • 检查是否任何特定字段真的很大,以及您是否受到磁盘而不是CPU的瓶颈;如果是这样,您可以(在Solr 4中)将该字段声明为压缩存储-这可以加快从磁盘
  • 获取字段的速度。

感谢您添加用于访问Solr服务器的代码。从tomcat日志中可以看出,对Solr的请求发生得非常快。(Solr正在缓存查询,因此QTime为0)。这1000多毫秒的时间不是用来让Solr服务器响应您的。这些时间用于将Solr响应传输回您的客户端(在这种情况下微不足道,因为它位于同一台服务器上)并读取响应流。

此外,您可能需要考虑使用SolrNet客户端从c#访问Solr。它为查询Solr提供了更丰富的接口,并将查询结果映射到POCO对象。