針對httptest4net構建elasticsearch叢集壓力測用例

來源:互聯網
上載者:User

標籤: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的運行目錄下即可以載入這用例並進行測試。

測試情況

個人站:www.ikende.com
個人開源項目github.com/IKende

elastic communication component for .net

針對httptest4net構建elasticsearch叢集壓力測用例

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.