三只松鼠网站开发,前端网站开发实例视频,wordpress注册老是显示404,电商详情页设计本篇和大家分享的是一个 并发请求工具#xff0c;并发往往代表的就是压力#xff0c;对于一些订单量比较多的公司这种情况很普遍#xff0c;也因此出现了很多应对并发的解决方案如#xff1a;分布式#xff0c;队列#xff0c;数据库锁等#xff1b;
对于没有遇到过或者…本篇和大家分享的是一个 并发请求工具并发往往代表的就是压力对于一些订单量比较多的公司这种情况很普遍也因此出现了很多应对并发的解决方案如分布式队列数据库锁等
对于没有遇到过或者不可能线上来处理并发问题的我们来说需要模拟这种环境不错这就是写并发请求工具的目的
. 对于api接口做并发请求
. NetCore来写的能跨平台运行
. 允许配置多个目标地址进行同时并发请求
. 支持GetPost请求方式post参数支持xmljson格式
工具设计的原理
工具的全部代码都开源至https://github.com/shenniubuxing3/PressureTool不妨标个*下面将举例演示如何使用工具设计的原理主要采用Task通过配置目标地址请求数量来拆分成多个Task以此完成并行的请求 由上图可以看出该工具主要有3层树形结构最底层是真实发出对目标url地址的请求使用的TaskTask对于多核CPU来说效果更显著在讲解例子前咋们先来看看配置文件对应的实体类
#region 配置信息 public class MoToolConf { /// summary /// 执行结果日志记录路径(全局默认程序根目录) /// /summary public string ResultLogPath { get; set; } /// summary /// 多个任务 /// /summary public ListMoTaskInfo MoTaskInfoes { get; set; } } /// summary /// 任务信息 /// /summary public class MoTaskInfo { /// summary /// 请求方式目前支持httpgethttppost /// /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 ListMoStudent _students new ListMoStudent(); // GET api/values [HttpGet] public async TaskMoBaseResponse 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 TaskMoBaseResponse 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跨平台或扫描二维码关注