httptest4net是能夠自定義HTTP壓力測試的工具,用戶能夠根據本身的狀況編寫測試用例加載到httptest4net中並運行測試。因爲最近須要對elasticsearch搜索集羣進行一個不一樣狀況的測試,因此針對這個測試寫了個簡單的測試用例。 git
[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的運行目錄下便可以加載這用例並進行測試。 github
我的站:www.ikende.com
我的開源項目github.com/IKende json