HttpTest4Net是一款基于C#實現的和HTTP壓力測試工具,通過工具可以簡單地對HTTP服務進行一個壓力測試.雖然VS.NET也集成了壓力測試項目,但由于VS自身占用的資源導致了在配置不高的PC上難以做到高并發壓力測試,再加上需要裝VS這個樣一個龐大的工具也是件很麻煩的事情.使用HttpTest4Net這個小工具只需要簡單地配置一下參數就可以進行現有的HTTP服務進行壓力測試并得到一個詳細的測試結果匯總.
工具只提供基礎的GET和POST測試用例,但在一些場景中這兩種測試可能滿足不了需要;所以工作提供自定義測試用例的功能,開發人員可能通過實現HttpTest4Net.Interfaces.IUrlTester接口實現功能相對比較復雜的測試用例;編寫好的測試用例只需要編譯成DLL后放到測試工具運行的目錄下即可完成.以下是組件提供的POST測試用例實現代碼:
01 | [Test("post base")] |
02 | publicclassPostUrlTester:IUrlTester |
03 | { |
04 | publicPostUrlTester() |
05 | { |
06 | Encoding ="UTF-8"; |
07 | |
08 | } |
09 | publicstringUrl |
10 | { |
11 | get; |
12 | set; |
13 | } |
14 |
15 | publicstringPostData |
16 | { |
17 | get; |
18 | set; |
19 | } |
20 |
21 | publicstringEncoding |
22 | { |
23 | get; |
24 | set; |
25 | } |
26 | |
27 | publicSystem.Net.HttpWebRequest CreateRequest() |
28 | { |
29 | HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url); |
30 | request.Method ="POST"; |
31 | request.ContentType ="application/x-www-form-urlencoded; charset="+Encoding; |
32 | byte[] data = System.Text.Encoding.GetEncoding(Encoding).GetBytes(PostData); |
33 | request.ContentLength = data.Length; |
34 | Stream myStream = request.GetRequestStream(); |
35 | myStream.Write(data, 0, data.Length); |
36 | myStream.Close(); |
37 | returnrequest; |
38 | } |
39 |
40 | publicTestType Type |
41 |