qileilove

          blog已經轉移至github,大家請訪問 http://qaseven.github.io/

          針對Httptest4net構建Elasticsearch集群壓力測試用例

           httptest4net是可以自定義HTTP壓力測試的工具,用戶可以根據自己的情況編寫測試用例加載到httptest4net中并運行測試。由于最近需要對elasticsearch搜索集群進行一個不同情況的測試,所以針對這個測試寫了個簡單的測試用例。
            代碼
          1  [Test("ES base")]
          2     public class ES_SearchUrlTester : IUrlTester
          3     {
          4
          5         public ES_SearchUrlTester()
          6         {
          7
          8
          9         }
          10         public string Url
          11         {
          12             get;
          13             set;
          14         }
          15
          16
          17         static string[] urls = new string[] {
          18             "http://192.168.20.156:9200/gindex/gindex/_search",
          19             "http://192.168.20.158:9200/gindex/gindex/_search",
          20             "http://192.168.20.160:9200/gindex/gindex/_search" };
          21
          22         private static long mIndex = 0;
          23
          24         private static List<string> mWords;
          25
          26         protected static IList<string> Words()
          27         {
          28
          29             if (mWords == null)
          30             {
          31                 lock (typeof(ES_SearchUrlTester))
          32                 {
          33                     if (mWords == null)
          34                     {
          35                         mWords = new List<string>();
          36                         using (System.IO.StreamReader reader = new StreamReader(@"D:\main.dic"))
          37                         {
          38                             string line;
          39
          40                             while ((line = reader.ReadLine()) != null)
          41                             {
          42                                 mWords.Add(line);
          43                             }
          44                         }
          45                     }
          46                 }
          47             }
          48             return mWords;
          49         }
          50         /*
          51           {"query" :
          52     {
          53   "bool" : {
          54     "should" : [ {
          55       "field" : {
          56         "title" : "#key"
          57       }
          58     }, {
          59       "field" : {
          60         "kw" : "#key"
          61       }
          62     } ]
          63   }
          64     },
          65 from:0,
          66 size:10
          67 }
          68          */
          69         private static string GetSearchUrlWord()
          70         {
          71             IList<string> words= Words();
          72             System.Threading.Interlocked.Increment(ref mIndex);
          73             return Resource1.QueryString.Replace("#key", words[(int)(mIndex % words.Count)]);
          74         }
          75
          76         public System.Net.HttpWebRequest CreateRequest()
          77         {
          78             var httpWebRequest = (HttpWebRequest)WebRequest.Create(urls[mIndex%urls.Length]);
          79             httpWebRequest.ContentType = "application/json";
          80             httpWebRequest.KeepAlive = false;
          81             httpWebRequest.Method = "POST";
          82             string json = GetSearchUrlWord();
          83             using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
          84             {
          85
          86                 streamWriter.Write(json);
          87                 streamWriter.Flush();
          88             }
          89             return httpWebRequest;
          90
          91         }
          92
          93         public TestType Type
          94         {
          95             get
          96             {
          97                 return TestType.POST;
          98             }
          99         }
          100     }
           用例很簡單根據節點和關鍵字構建不同請求的URL和JSON數據包即可完成。把上面代碼編譯在DLL后放到httptest4net的運行目錄下即可以加載這用例并進行測試。
            測試情況

          posted on 2014-11-26 14:16 順其自然EVO 閱讀(601) 評論(0)  編輯  收藏 所屬分類: 測試學習專欄

          <2014年11月>
          2627282930311
          2345678
          9101112131415
          16171819202122
          23242526272829
          30123456

          導航

          統計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 康定县| 鹤山市| 湖北省| 西盟| 班玛县| 洛隆县| 五峰| 宜兴市| 攀枝花市| 兴安盟| 手游| 股票| 廊坊市| 和顺县| 永康市| 朝阳市| 芜湖市| 和林格尔县| 开江县| 绥阳县| 成武县| 嫩江县| 施秉县| 望江县| 廊坊市| 南华县| 上蔡县| 峨边| 沾益县| 扬州市| 清镇市| 会理县| 连云港市| 论坛| 汾阳市| 洛宁县| 湖南省| 凌云县| 象州县| 城口县| 白朗县|