Write stress test by yourself-three muskeys httpclient + async + parallel

Source: Internet
Author: User

Once go through the sea

Information such as water Great pressure

In the function implementation stage, we analyze the information to be processed very carefully and thoroughly. However, at the product stage, the information to be processed is like a flood of traffic. The original assumption was that it was broken into fragments. The errors and problems generated at this time are hard to be reproduced on the development machine.

If the user acceptance test is to check the function implementation, the need to drip; the pressure test is the capacity test, to meet the wave of baptism.

(Copyright: 2012-2013 Yu qin'an)

Round fat Yan thin

Many stress testing tools, especially HP's LoadRunner, have even become industry standards. However, in the course of research and investigation, I was gradually questioning whether I had to use these tools? First, they are commercial software, which is expensive. Second, it is because commercial software has too many functions and is too large. I don't need many things. Why not do it as simple and practical?

When Xiao Zhao studied selenium, I felt that his syntax was very close to the business language. So I asked a question, can I use it for stress testing? He said no, because selenium wants to completely start the browser. At ordinary times, it seems that small browsers consume a lot of resources, especially compared with the capacity of stress testing. You can try to see what the situation will be when you open 100 browser windows at the same time on your machine.

Slim beauty Httpclient

After denying selenium, I quickly found my target httpclient (in fact, there is a previous WebClient, which is described later ). From the name, we can know that it has been positioned to a very low HTTP layer, which is the best balance between efficiency and ease of use. However, it is a part under. Net 4.5 and must be downloaded using nuget in. Net 4.0.

After reading a lot of information, I can be sure that httpclient is exactly what I want. She also has a big feature, providing only asynchronous interfaces. This is actually another kind of big slimming, resource-consuming slimming, httpclient officially I want the lady fair.

Foresight: WebClient

var values = new NameValueCollection();foreach (var key_value in ui.FormData){values.Add(key_value.Key, key_value.Value);}var client = new WebClient();client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");Console.WriteLine(string.Concat(base_site + ui.Path, ui.Method.ToString().ToLower(), values));byte[] result = client.UploadValues(base_site + ui.Path, ui.Method.ToString().ToLower(), values);string ResultAuthTicket = Encoding.UTF8.GetString(result);Console.WriteLine(client.BaseAddress);Console.WriteLine(client.ResponseHeaders.ToString());Console.WriteLine(ResultAuthTicket);

The async method of httpclient. Note that the last wait () method converts the asynchronous mode to synchronous mode.

var form_data=new Dictionary<string, string>();form_data.Add("system_account","test1@skight.com");form_data.Add("system_password","123456");var values = new NameValueCollection();foreach (var key_value in form_data){values.Add(key_value.Key, key_value.Value);}var client = new HttpClient();client.GetStringAsync("http://esr20syst.skight.com/District/03/UserLogin.do")  .ContinueWith(  t =>  {  Console.WriteLine("Time {0}", DateTime.Now);  Console.WriteLine(t.Result);  })  .Wait();
SYN C or async. This is a problem.

After. Net 4 and 5 came out, they never cared too much about its new functions and features. It's just that the company upgraded to use vs2012, except for the gray and smooth interface, and the so-called performance improvement (in fact, vs2010 is too many times), there is no special feeling.

However, when viewing httpclient information this time, we accidentally discovered a. Net 4.5 syntax-level highlight: Asyn and await. This makes asynchronous programming easier and more beautiful. It seems that asynchronous programming will be a big trend in the future, and Microsoft will not hesitate.

The effect of applying the new syntax does not seem to be much different from the synchronous encoding in time, except for the await and async that come out from time to time.

var form_data=new Dictionary<string, string>();form_data.Add("system_account","test1@skight.com");form_data.Add("system_password","123456");foreach (var key_value in form_data){values.Add(key_value.Key, key_value.Value);}var httpClient = new HttpClient();var content= await httpClient.GetStringAsync("http://esr20syst.skight.com/District/03/UserLogin.do");Console.WriteLine(t.Result);

The benefits of asynchronous performance cannot be underestimated. Previously, Web Services built with node. js were much faster than Apache (http://zgadzaj.com/benchmarking-nodejs-basic-performance-tests-against-apache-php) thanks to the asynchronous running supported by JavaScript's native function callback method. Currently, there are. NET Friendly support for async and a large number of components are rewritten asynchronously. It is said that Microsoft recommends that all functions that run for more than 20 milliseconds should be written asynchronously. Httpclient is an example. Its predecessor, WebClient, does not have asynchronous calls, while httpclient simply does not provide synchronous interfaces.

Parallel universeParallel

In fact, async and parallel are both syntactic sugar. But as a hard-working developer, this is what we do.

Async makes it easy for us to continuously send parallel requests without waiting for the network. This is a complete stress testing model. I have set a 100*10 request. There is no specific calculation, and I only know how many requests are sent. Many of them are enough to let my system reproduce the problems on the product machine.

 Parallel.For(1, 1000, i =>Parallel.For(1, 5, case_number =>   LoginScenario(case_number)   .run_by(runner)));
Appendix : My business syntax sugar DSL

Here is my code for defining system page operations. It is implemented by the syntax sugar DSL. To a certain extent, it meets the requirements of code, that is, the document. This part of Code cannot be run directly, because it uses my own Web framework, so that it can automatically generate a URL with a strong type. It is provided for reference only as part of the example.

private static Scenario LoginScenario(int case_number){returnUI.context(Keys.Context.District.with_value(DistrictIdentifier.of("03")))  .to<UserLoginGet>()  .then(  UI.input(SystemPayloadKeys.Account.with_value(string.Format("test{0}@skight.com", case_number))).and_input(SystemPayloadKeys.Password.with_value("123456")).to<UserLoginPost>());}

(The Copyright in this article is 2012-2013 Yu Qinan | repost the article to indicate the author and source)

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.