开源个.NetCore写的
来源:互联网 发布:画图软件怎么移动 编辑:程序博客网 时间:2024/05/02 05:00
本篇和大家分享的是一个 并发请求工具,并发往往代表的就是压力,对于一些订单量比较多的公司这种情况很普遍,也因此出现了很多应对并发的解决方案如:分布式,队列,数据库锁等;
对于没有遇到过或者不可能线上来处理并发问题的我们来说,需要模拟这种环境,不错这就是写并发请求工具的目的:
. 对于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写的
- 开源个.NetCore写的
- netcore
- 用.netcore写一个简单redis驱动,调试windows版本的redis
- .NetCore~TagHelpers标签的使用
- .NetCore~TagHelpers标签的使用
- docker对.netcore的部署
- .NetCore~C#6的一些新特性
- .NetCore~C#6的一些新特性
- NETCore开发跨平台的应用部属到-Linux-Ubuntu
- centos7环境基于jenkins、nuget、nexus的netcore持续集成
- .netcore开发
- .NetCore WebSocket
- .NetCore Docker
- 跨平台的 NodeJS 组件解决 .NetCore 不支持 System.Drawing图形功能的若干问题
- NetCore连接小区宽带Modem,显示IP地址冲突的解决方法
- 解决VMware虚拟机在netcore nw703路由器下无法上网的问题
- ubuntu16.04-x64系统中Jexus web server部署.NetCore和端口分析引发的猜想!
- Entity Framework Core 批处理语句
- 全文索引
- SOA和微服务之间的区别
- Entity Framework Core 命名约定
- jquery 整数值动态拼接字符组成id 获取元素实现方式
- 开源个.NetCore写的
- [信息安全] 4.一次性密码 &amp;&amp; 身份认证三要素
- 使用 InSpec 实现符合性即代码
- ASP.NET Core API 版本控制
- 扩展entity framework core实现默认字符串长度,decimal精度,entity自动注册和配置
- 课堂练习1
- Postgresql快速写入\/读取大量数据(.net)
- Entity Framework Core 软删除与查询过滤器
- 在Azure Container Service创建Kubernetes(k8s)群集运行ASP.NET Core跨平台应用程序