Written by open-source. NetCore and open-source. NetCore

Source: Internet
Author: User

Written by open-source. NetCore and open-source. NetCore

This article will share with you a concurrent request tool. Concurrency often represents pressure. This is common for companies with a large number of orders, as a result, many concurrent solutions have emerged, such as distributed, queue, and database locks;

If you have never met or cannot come onlineHandling concurrency ProblemsFor us, we needSimulationThis environment is good. This is the purpose of writing concurrent request tools:

. Make concurrent requests for api Interfaces

. NetCore can be written to run across platforms

. Multiple Target addresses can be configured for concurrent requests.

. Supports Get and Post requests (the post parameter supports xml and json formats)

Principle of tool design

All the code of the tool is open-source:Https://github.com/shenniubuxing3/PressureTool (may wish to mark *)The following example shows how to use the tool. The design principle of the tool mainly uses tasks. By configuring the target address and the number of requests, the tool is split into multiple tasks to complete parallel requests:

It can be seen that the tool mainly has three layers of tree structure, the bottom layer is to actually send a request to the target url address, the use of tasks, tasks for multi-core CPU performance is more remarkable; before explaining the example, let's take a look at the object class corresponding to the configuration file:

1 # region configuration information 2 3 public class MoToolConf 4 {5 // <summary> 6 // execution result log record path (global, default program root directory) 7 /// </summary> 8 public string ResultLogPath {get; set ;} 9 10 /// <summary> 11 /// multiple tasks 12 /// </summary> 13 public List <MoTaskInfo> MoTaskInfoes {get; set ;} 14} 15 16 /// <summary> 17 // task information 18 /// </summary> 19 public class MoTaskInfo20 {21 22 /// <summary> 23 // /request method, currently supported: httpget, httppost24 /// </summary> 25 public string Method {get; set ;} 26 27 /// <summary> 28 /// request address 29 /// </summary> 30 public string Url {get; set ;} 31 32 // <summary> 33 // Number of connections 34 /// </summary> 35 public int LinkNum {get; set ;} 36 37 // <summary> 38 // parameter (used for post) 39 // </summary> 40 public string Param {get; set ;} 41 42 // <summary> 43 // path of the execution result log (Private> global) 44 // </summary> 45 public string ResultLogPath {get; set ;} 46} 47 # endregion
Httpget request configuration

First, we need to find the configuration file PressureTool. json in the root directory, and then configure it to the following get request settings:

{"ResultLogPath": "", // It is not set by default. logs are recorded in the root directory "MoTaskInfoes": [{"Method": "httpget", "Url ": "https://www.baidu.com/", "LinkNum": 10, "Param": "", "ResultLogPath": "" },{ "Method": "httpget", "Url ": "https://cloud.baidu.com/", "LinkNum": 10, "Param": "", "ResultLogPath": ""}]}

Httpget should be the simplest request method. If you need to pass any parameters, you can simply append them to your url. The get request method does not use the Param parameter:

 

Httppost request configuration-the parameter is json

The difference between post configuration and get is to set different Method parameters ("Method": "httppost_json"). If you have parameters, you also need to configure the Param node ("Param ": "{\" Number \ ": 1, \" Name \ ": \" Zhang San \ "}"), refer to the following Configuration:

{"ResultLogPath": "", // It is not set by default. logs are recorded in the root directory "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 \": \ "Zhang San \"} "," ResultLogPath ":" "}]}

Here, I wrote a Simple api interface to test, which receives json and xml parameters respectively. The api test code is as follows:

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/515 [HttpGet ("{id}")] 16 public string Get (int id) 17 {18 return "value "; 19} 20 21 // PO ST api/values22 [HttpPost] 23 public MoBaseResponse PostJson ([FromBody] MoStudent student) 24 {25 var response = new MoBaseResponse () {Msg = "failed to add "}; 26 if (student = null) {return response;} 27 28 _ students. add (student); 29 response. msg = "added successfully"; 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 = "failed to add"}; 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 = "added successfully"; 59 response. status = 1; 60 return response; 61} 62} 63 64 public class MoBaseResponse65 {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 MoStudent74 {75 public int Number {get; set;} 76 77 public string Name {get; set ;} 78}

We send a request to the test api address http: // localhost: 5000/api/Values/PostJson to pass the basic information parameters of the student, and then use the get api to see the effect:

This example only requests an api once. If you want to test the concurrency of your api, you can set the parameter "LinkNum": 10 or more:

 

Httppost request configuration-the parameter is xml

The configuration for passing xml parameters in post mode is similar to that in json mode. Note that you need to modify Method ("Method": "httppost_xml") because the xml and json configuration of tools are separated, the following example shows how to configure the number of five requests in json and xml:

Then, you can use the get api to obtain the following results:

Now the demo is complete. If you think you can use this tool, you can go to the git source code:Https://github.com/shenniubuxing3/PressureToolOr join the NineskyQQ official group: 428310563 to obtain the Framework Version tool.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.