<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Azure Web App Archives - Pritam&#039;s Blog</title>
	<atom:link href="https://www.pritambaldota.com/tag/azure-web-app/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.pritambaldota.com/tag/azure-web-app/</link>
	<description>cloud technology, enterprise architecture</description>
	<lastBuildDate>Sat, 04 Apr 2020 18:10:52 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8</generator>
	<item>
		<title>How to Load Test Web API in just 5 minutes without any testing tool</title>
		<link>https://www.pritambaldota.com/how-to-load-test-web-api-in-just-5-minutes-without-any-testing-tool/</link>
		
		<dc:creator><![CDATA[Pritam Baldota]]></dc:creator>
		<pubDate>Sat, 04 Apr 2020 18:10:44 +0000</pubDate>
				<category><![CDATA[Load Testing]]></category>
		<category><![CDATA[Api Load Testing]]></category>
		<category><![CDATA[Azure Web App]]></category>
		<category><![CDATA[load test api in 5 min]]></category>
		<guid isPermaLink="false">http://www.pritambaldota.com/?p=273</guid>

					<description><![CDATA[<p>The Problem Testing REST (Representational State Transfer) Api is very common task for any organization. Ideally, dedicated test team or automation runs to do api&#8230;</p>
<p>The post <a href="https://www.pritambaldota.com/how-to-load-test-web-api-in-just-5-minutes-without-any-testing-tool/">How to Load Test Web API in just 5 minutes without any testing tool</a> appeared first on <a href="https://www.pritambaldota.com">Pritam&#039;s Blog</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">The Problem</h2>



<p>Testing <a rel="noreferrer noopener" href="https://en.wikipedia.org/wiki/Representational_state_transfer" target="_blank">REST </a>(Representational State Transfer) Api is very common task for any organization. Ideally, dedicated test team or automation runs to do api test after development. But during development most of the time developer test with small set of data. What if we want to test for massive data, like millions of request? This can be easily achieve in less than 5 min without any special test tool. </p>



<p>Design your Api&#8217;s to handle load during peak hours, so performing load test is essential. For instance, 10000 requests/minute or 200 requests/second can generate millions of request load on server.</p>



<h2 class="wp-block-heading">The Solution</h2>



<p>To perform load test, we have to use some automated load tools. There are several free/paid tools available. These tools comes with lot of great features which involve learning time and cost. </p>



<p>What if we can achieve the purpose without using any specific testing tools? Yes, we can do it very easily in any programming language. I am showing example using C#. </p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>Save every single penny wherever possible.</p><cite>&#8211; General thought</cite></blockquote>



<p>To load test, rationale is very simple &#8211; </p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>Send thousands of  web requests simultaneously.</p><cite>&#8211; Millions of Web Request</cite></blockquote>



<h2 class="wp-block-heading">How to implement the solution</h2>



<p>We can do this in simple C# console App or <a rel="noreferrer noopener" href="https://www.linqpad.net/" target="_blank">Linqpad</a> (great tool for .Net developer) with <a rel="noreferrer noopener" href="https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclient" target="_blank">HttpClient</a> in just 5 minutes. I am using Linqpad to run this script.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="csharp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">void Main()
{
    System.Threading.Tasks.Task.Run(async () =>
    {
        "Start Generating Load".Dump();
        await RunTest();
        "Completed.".Dump();
    });
}

public async System.Threading.Tasks.Task RunTest()
{
	int MAX_ITERATIONS = 500;
	int MAX_PARALLEL_REQUESTS = 500;
	int DELAY = 100;
	
	// Collection of Url's to test. Change to your valid Url's.
    string[] urls = new string[] { "https://hostname.azurewebsites.net/api/datatest", "https://hostname.azurewebsites.net/api/datatest/add" };
	
    using (var httpClient = new System.Net.Http.HttpClient())
    {
		// To add any headers like Bearer Token, Media Type etc.
        //httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.xxxxxx");
		
        for (var step = 1; step &lt; MAX_ITERATIONS; step++)
        {
            $"Started iteration: {step}".Dump();
            var tasks = new List&lt;System.Threading.Tasks.Task&lt;System.Net.Http.HttpResponseMessage>>();
            for (int i = 0; i &lt; MAX_PARALLEL_REQUESTS; i++)
            {
                tasks.Add(httpClient.PostAsync(urls[i % 2], null));
            }

			// Run all 300 tasks in parallel
            var result = await System.Threading.Tasks.Task.WhenAll(tasks);

            $"Completed Iteration: {step}".Dump();
			
			// Some delay before new iteration
            await System.Threading.Tasks.Task.Delay(DELAY);
        }
    }
}</pre>



<p>I ran this script for 500 iterations from 300 to 1000 parallel request per iteration for few hours. The web api  was developed on .Net Core 3.1, hosted on Azure Linux Web App B1 plan. You can look for another post on why to host on linux web app over windows &#8211; <a rel="noreferrer noopener" href="http://www.pritambaldota.com/deploy-asp-net-core-apps-to-azure-app-service-with-lesser-cost/" target="_blank">Deploy ASP.NET Core apps to Azure App Service with lesser cost </a>, which is why I chose Linux Web App over Windows. The test script was able to generate 2 millions of requests successfully from my Surface Book within few hours.</p>



<figure class="wp-block-image size-large"><img fetchpriority="high" decoding="async" width="1024" height="499" src="/wp-content/uploads/2020/04/image-1-1024x499.png" alt="Load Test result on Azure Linux Web App B1 plan. " class="wp-image-278" srcset="https://www.pritambaldota.com/wp-content/uploads/2020/04/image-1-1024x499.png 1024w, https://www.pritambaldota.com/wp-content/uploads/2020/04/image-1-300x146.png 300w, https://www.pritambaldota.com/wp-content/uploads/2020/04/image-1-768x374.png 768w, https://www.pritambaldota.com/wp-content/uploads/2020/04/image-1-1536x748.png 1536w, https://www.pritambaldota.com/wp-content/uploads/2020/04/image-1-2048x998.png 2048w, https://www.pritambaldota.com/wp-content/uploads/2020/04/image-1-769x375.png 769w" sizes="(max-width: 1024px) 100vw, 1024px" /><figcaption>Avg. 13000 API web request/minutes load</figcaption></figure>



<h2 class="wp-block-heading">Conclusion</h2>



<p>To summarize, we should always test our code for performance, scale, which results into better quality code.  </p>



<p>I hope you found this post helpful. </p>
<p>The post <a href="https://www.pritambaldota.com/how-to-load-test-web-api-in-just-5-minutes-without-any-testing-tool/">How to Load Test Web API in just 5 minutes without any testing tool</a> appeared first on <a href="https://www.pritambaldota.com">Pritam&#039;s Blog</a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
