开源个.NetCore写的 - 并发请求工具PressureTool
本篇和大家分享的是一個?并發請求工具,并發往往代表的就是壓力,對于一些訂單量比較多的公司這種情況很普遍,也因此出現了很多應對并發的解決方案如:分布式,隊列,數據庫鎖等;
對于沒有遇到過或者不可能線上來處理并發問題的我們來說,需要模擬這種環境,不錯這就是寫并發請求工具的目的:
.?對于api接口做并發請求
. NetCore來寫的能跨平臺運行
.?允許配置多個目標地址,進行同時并發請求
.?支持Get,Post請求方式(post參數支持:xml,json格式)
工具設計的原理
工具的全部代碼都開源至:https://github.com/shenniubuxing3/PressureTool(不妨標個*),下面將舉例演示如何使用;工具設計的原理主要采用Task,通過配置目標地址,請求數量來拆分成多個Task,以此完成并行的請求:
由上圖可以看出,該工具主要有3層樹形結構,最底層是真實發出對目標url地址的請求,使用的Task,Task對于多核CPU來說效果更顯著;在講解例子前咋們先來看看配置文件對應的實體類:
#region 配置信息
? ? ? ? public class MoToolConf
? ? ? ? {
? ? ? ? ? ? /// <summary>
? ? ? ? ? ? /// 執行結果日志記錄路徑(全局,默認程序根目錄)
? ? ? ? ? ? /// </summary>
? ? ? ? ? ? public string ResultLogPath { get; set; }
? ? ? ? ? ? /// <summary>
? ? ? ? ? ? /// 多個任務
? ? ? ? ? ? /// </summary>
? ? ? ? ? ? public List<MoTaskInfo> MoTaskInfoes { get; set; }
? ? ? ? }
? ? ? ? /// <summary>
? ? ? ? /// 任務信息
? ? ? ? /// </summary>
? ? ? ? public class MoTaskInfo
? ? ? ? {
? ? ? ? ? ? /// <summary>
? ? ? ? ? ? /// 請求方式,目前支持:httpget,httppost
? ? ? ? ? ? /// </summary>
? ? ? ? ? ? public string Method { get; set; }
? ? ? ? ? ? /// <summary>
? ? ? ? ? ? /// 請求地址
? ? ? ? ? ? /// </summary>
? ? ? ? ? ? public string Url { get; set; }
? ? ? ? ? ? /// <summary>
? ? ? ? ? ? /// 連接數
? ? ? ? ? ? /// </summary>
? ? ? ? ? ? public int LinkNum { get; set; }
? ? ? ? ? ? /// <summary>
? ? ? ? ? ? /// 參數(post使用)
? ? ? ? ? ? /// </summary>
? ? ? ? ? ? public string Param { get; set; }
? ? ? ? ? ? /// <summary>
? ? ? ? ? ? /// 執行結果日志記錄路徑(私有>全局)
? ? ? ? ? ? /// </summary>
? ? ? ? ? ? public string ResultLogPath { get; set; }
? ? ? ? }
? ? ? ? #endregion
httpget請求的配置
首先我們需要在根目錄下找到配置文件:PressureTool.json,然后配置成如下get請求設置:
{
? "ResultLogPath": "",//默認不設置,日志記錄在根目錄
? "MoTaskInfoes": [
? ? {
? ? ? "Method": "httpget",
? ? ? "Url": "https://www.baidu.com/",
? ? ? "LinkNum": 10,
? ? ? "Param": "",
? ? ? "ResultLogPath": ""
? ? },
? ? {
? ? ? "Method": "httpget",
? ? ? "Url": "https://cloud.baidu.com/",
? ? ? "LinkNum": 10,
? ? ? "Param": "",
? ? ? "ResultLogPath": ""
? ? }
? ]
}
httpget應該是最簡單的請求方式了,如果你需要傳遞什么參數,就直接往您url上追加就行了,get請求方式是用不到Param參數的:
?
httppost請求的配置 - 參數為json
post的配置與get不同的是設置不同的Method參數(?"Method":?"httppost_json"?),并且如果你有參數那么還需要配置Param節點(?"Param":?"{\"Number\": 1,\"Name\": \"張三\"}"?),參考如下配置:
{ ?"ResultLogPath": "", //默認不設置,日志記錄在根目錄"MoTaskInfoes": [{ ? ? ?"Method": "httpget", ? ? ?"Url": "https://www.baidu.com/", ? ? ?"LinkNum": 10, ? ? ?"Param": "", ? ? ?"ResultLogPath": ""},{ ? ? ?"Method": "httppost_json", ? ? ?"Url": "http://localhost:5000/api/Values/PostJson", ? ? ?"LinkNum": 1, ? ? ?"Param": "{\"Number\": 1,\"Name\": \"張三\"}", ? ? ?"ResultLogPath": ""}] }這里為了測試我寫了一個簡單的api接口,分別接收json和xml的參數,測試api接口代碼如下:
[Route("api/[controller]/[action]")]
? ? public class ValuesController : Controller
? ? {
? ? ? ? public static List<MoStudent> _students = new List<MoStudent>();
? ? ? ? // GET api/values
? ? ? ? [HttpGet]
? ? ? ? public async Task<MoBaseResponse> Get()
? ? ? ? {
? ? ? ? ? ? return new MoBaseResponse { Data = _students };
? ? ? ? }
? ? ? ? // GET api/values/5
? ? ? ? [HttpGet("{id}")]
? ? ? ? public string Get(int id)
? ? ? ? {
? ? ? ? ? ? return "value";
? ? ? ? }
? ? ? ? // POST api/values
? ? ? ? [HttpPost]
? ? ? ? public MoBaseResponse PostJson([FromBody]MoStudent student)
? ? ? ? {
? ? ? ? ? ? var response = new MoBaseResponse() { Msg = "添加失敗" };
? ? ? ? ? ? if (student == null) { return response; }
? ? ? ? ? ? _students.Add(student);
? ? ? ? ? ? response.Msg = "添加成功";
? ? ? ? ? ? response.Status = 1;
? ? ? ? ? ? return response;
? ? ? ? }
? ? ? ? [HttpPost]
? ? ? ? public async Task<MoBaseResponse> PostXml()
? ? ? ? {
? ? ? ? ? ? var response = new MoBaseResponse() { Msg = "添加失敗" };
? ? ? ? ? ? var strReq = string.Empty;
? ? ? ? ? ? using (var stream = Request.Body)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? using (var reader = new StreamReader(stream))
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? strReq = await reader.ReadToEndAsync();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? if (string.IsNullOrWhiteSpace(strReq)) { return response; }
? ? ? ? ? ? var match = Regex.Match(strReq, "<Number>(?<number>[^<]+)</Number>[^<]*<Name>(?<name>[^<]+)</Name>");
? ? ? ? ? ? if (match == null || match.Groups.Count <= 0) { return response; }
? ? ? ? ? ? var student = new MoStudent();
? ? ? ? ? ? student.Number = Convert.ToInt32(match.Groups["number"].Value);
? ? ? ? ? ? student.Name = match.Groups["name"].Value;
? ? ? ? ? ? _students.Add(student);
? ? ? ? ? ? response.Msg = "添加成功";
? ? ? ? ? ? response.Status = 1;
? ? ? ? ? ? return response;
? ? ? ? }
? ? }
? ? public class MoBaseResponse
? ? {
? ? ? ? public int Status { get; set; }
? ? ? ? public string Msg { get; set; }
? ? ? ? public object Data { get; set; }
? ? }
? ? public class MoStudent
? ? {
? ? ? ? public int Number { get; set; }
? ? ? ? public string Name { get; set; }
? ? }
我們往測試api地址?http://localhost:5000/api/Values/PostJson?發出請求,傳遞學生基本信息參數,然后通過api的get接口看看效果:
這里演示的只請求一次api,如果你想測試你自己api接口并發情況,你可以設置參數:?"LinkNum":?10?或者跟多:
?
httppost請求的配置 - 參數為xml
post方式傳遞xml參數的配置和json差不多,需要注意的是需要修改Method(?"Method":?"httppost_xml"?),因為工具吧xml和json的配置區分開了,下面來演示下json和xml分別配置5次請求數的效果:
然后通過api的get接口獲取下效果:
好了到這里演示就完了,如果您覺得該工具可以你可以去git源碼:https://github.com/shenniubuxing3/PressureTool?,或者加入 NineskyQQ官方群:428310563 獲取Framework版本的工具。
原文地址:http://www.cnblogs.com/wangrudong003/p/7235323.html
.NET社區新聞,深度好文,微信中搜索dotNET跨平臺或掃描二維碼關注
總結
以上是生活随笔為你收集整理的开源个.NetCore写的 - 并发请求工具PressureTool的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [信息安全] 4.一次性密码 amp;a
- 下一篇: asp.net ajax控件工具集 Au