开源个.NetCore写的 - 并发请求工具PressureTool

本篇和大家分享的是一个 并发请求工具,并发往往代表的就是压力,对于一些订单量比较多的公司这种情况很普遍,也因此出现了很多应对并发的解决方案如:分布式,队列,数据库锁等;

对于没有遇到过或者不可能线上来处理并发问题的我们来说,需要模拟这种环境,不错这就是写并发请求工具的目的:

. 对于api接口做并发请求

. NetCore来写的能跨平台运行

. 允许配置多个目标地址,进行同时并发请求

. 支持Get,Post请求方式(post参数支持:xml,json格式)

工具设计的原理

工具的全部代码都开源至:https://github.com/shenniubuxing3/PressureTool(不妨标个*),下面将举例演示如何使用;工具设计的原理主要采用Task,通过配置目标地址,请求数量来拆分成多个Task,以此完成并行的请求:

由上图可以看出,该工具主要有3层树形结构,最底层是真实发出对目标url地址的请求,使用的Task,Task对于多核CPU来说效果更显著;在讲解例子前咋们先来看看配置文件对应的实体类:

 1 #region 配置信息
 2 
 3         public class MoToolConf
 4         {
 5             /// <summary>
 6             /// 执行结果日志记录路径(全局,默认程序根目录)
 7             /// </summary>
 8             public string ResultLogPath { get; set; }
 9 
10             /// <summary>
11             /// 多个任务
12             /// </summary>
13             public List<MoTaskInfo> MoTaskInfoes { get; set; }
14         }
15 
16         /// <summary>
17         /// 任务信息
18         /// </summary>
19         public class MoTaskInfo
20         {
21 
22             /// <summary>
23             /// 请求方式,目前支持:httpget,httppost
24             /// </summary>
25             public string Method { get; set; }
26 
27             /// <summary>
28             /// 请求地址
29             /// </summary>
30             public string Url { get; set; }
31 
32             /// <summary>
33             /// 连接数
34             /// </summary>
35             public int LinkNum { get; set; }
36 
37             /// <summary>
38             /// 参数(post使用)
39             /// </summary>
40             public string Param { get; set; }
41 
42             /// <summary>
43             /// 执行结果日志记录路径(私有>全局)
44             /// </summary>
45             public string ResultLogPath { get; set; }
46         }
47         #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接口代码如下:

 1 [Route("api/[controller]/[action]")]
 2     public class ValuesController : Controller
 3     {
 4         public static List<MoStudent> _students = new List<MoStudent>();
 5 
 6         // GET api/values
 7         [HttpGet]
 8         public async Task<MoBaseResponse> Get()
 9         {
10 
11             return new MoBaseResponse { Data = _students };
12         }
13 
14         // GET api/values/5
15         [HttpGet("{id}")]
16         public string Get(int id)
17         {
18             return "value";
19         }
20 
21         // POST api/values
22         [HttpPost]
23         public MoBaseResponse PostJson([FromBody]MoStudent student)
24         {
25             var response = new MoBaseResponse() { Msg = "添加失败" };
26             if (student == null) { return response; }
27 
28             _students.Add(student);
29             response.Msg = "添加成功";
30             response.Status = 1;
31 
32             return response;
33         }
34 
35         [HttpPost]
36         public async Task<MoBaseResponse> PostXml()
37         {
38             var response = new MoBaseResponse() { Msg = "添加失败" };
39             var strReq = string.Empty;
40             using (var stream = Request.Body)
41             {
42                 using (var reader = new StreamReader(stream))
43                 {
44                     strReq = await reader.ReadToEndAsync();
45                 }
46             }
47 
48             if (string.IsNullOrWhiteSpace(strReq)) { return response; }
49 
50             var match = Regex.Match(strReq, "<Number>(?<number>[^<]+)</Number>[^<]*<Name>(?<name>[^<]+)</Name>");
51             if (match == null || match.Groups.Count <= 0) { return response; }
52 
53             var student = new MoStudent();
54             student.Number = Convert.ToInt32(match.Groups["number"].Value);
55             student.Name = match.Groups["name"].Value;
56             _students.Add(student);
57 
58             response.Msg = "添加成功";
59             response.Status = 1;
60             return response;
61         }
62     }
63 
64     public class MoBaseResponse
65     {
66         public int Status { get; set; }
67 
68         public string Msg { get; set; }
69 
70         public object Data { get; set; }
71     }
72 
73     public class MoStudent
74     {
75         public int Number { get; set; }
76 
77         public string Name { get; set; }
78     }

我们往测试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版本的工具。

posted @ 2017-07-25 23:24  神牛003  阅读(3939)  评论(6编辑  收藏  举报