码迷,mamicode.com
首页 > Web开发 > 详细

针对httptest4net构建elasticsearch集群压力测用例

时间:2014-09-03 15:10:37      阅读:305      评论:0      收藏:0      [点我收藏+]

标签:style   http   os   io   ar   for   数据   2014   代码   

        httptest4net是可以自定义HTTP压力测试的工具,用户可以根据自己的情况编写测试用例加载到httptest4net中并运行测试。由于最近需要对elasticsearch搜索集群进行一个不同情况的测试,所以针对这个测试写了个简单的测试用例。

代码

[Test("ES base")]
    public class ES_SearchUrlTester : IUrlTester
    {
       
        public ES_SearchUrlTester()
        {
          

        }
        public string Url
        {
            get;
            set;
        }


        static string[] urls = new string[] { 
            "http://192.168.20.156:9200/gindex/gindex/_search",
            "http://192.168.20.158:9200/gindex/gindex/_search",
            "http://192.168.20.160:9200/gindex/gindex/_search" };

        private static long mIndex = 0;

        private static List<string> mWords;

        protected static IList<string> Words()
        {

            if (mWords == null)
            {
                lock (typeof(ES_SearchUrlTester))
                {
                    if (mWords == null)
                    {
                        mWords = new List<string>();
                        using (System.IO.StreamReader reader = new StreamReader(@"D:\main.dic"))
                        {
                            string line;

                            while ((line = reader.ReadLine()) != null)
                            {
                                mWords.Add(line);
                            }
                        }
                    }
                }
            }
            return mWords;
        }
        /*
          {"query" : 
	{
  "bool" : {
    "should" : [ {
      "field" : {
        "title" : "#key"
      }
    }, {
      "field" : {
        "kw" : "#key"
      }
    } ]
  }
	},
from:0,
size:10
}
         */
        private static string GetSearchUrlWord()
        {
            IList<string> words= Words();
            System.Threading.Interlocked.Increment(ref mIndex);
            return Resource1.QueryString.Replace("#key", words[(int)(mIndex % words.Count)]); 
        }

        public System.Net.HttpWebRequest CreateRequest()
        {
            var httpWebRequest = (HttpWebRequest)WebRequest.Create(urls[mIndex%urls.Length]);
            httpWebRequest.ContentType = "application/json";
            httpWebRequest.KeepAlive = false;
            httpWebRequest.Method = "POST";
            string json = GetSearchUrlWord();
            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {

                streamWriter.Write(json);
                streamWriter.Flush();
            }
            return httpWebRequest;

        }

        public TestType Type
        {
            get
            {
                return TestType.POST;
            }
        }
    }

用例很简单根据节点和关键字构建不同请求的URL和JSON数据包即可完成。把上面代码编译在DLL后放到httptest4net的运行目录下即可以加载这用例并进行测试。

测试情况

bubuko.com,布布扣

个人站:www.ikende.com
个人开源项目github.com/IKende

elastic communication component for .net

针对httptest4net构建elasticsearch集群压力测用例

标签:style   http   os   io   ar   for   数据   2014   代码   

原文地址:http://my.oschina.net/ikende/blog/309624

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!