针对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的运行目录下即可以加载这用例并进行测试。
测试情况
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架