We can use Application Initialization to warmup an worker instance in Azure App Service. So that when an App Service Plan is scaled out, the new worker instance is warmed up, before it receives real end user requests.
I deployed a .Net framework MVC application with a WarmupController class below:
public class WarmupController : Controller
{
// GET: Warmup
public string Index()
{
Thread.Sleep(60000);
return "Warmed up";
}
}
And the web.config file:
<applicationInitialization>
<add initializationPage="/warmup/" />
</applicationInitialization>
<tracing>
<traceFailedRequests>
<remove path="*" />
<add path="/warmup/">
<traceAreas>
<add provider="ASP" verbosity="Verbose" />
<add provider="ASPNET" areas="Infrastructure,Module,Page,AppServices" verbosity="Verbose" />
<add provider="ISAPI Extension" verbosity="Verbose" />
<add provider="WWW Server" areas="Authentication,Security,Filter,StaticFile,CGI,Compression,Cache,RequestNotifications,Module,Rewrite,iisnode" verbosity="Verbose" />
</traceAreas>
<failureDefinitions statusCodes="200-500" />
</add>
</traceFailedRequests>
</tracing>
I turn on the Failed Request Trace
and Web Server Log
for the site.
The log files can be found in the D:\home\LogFiles\
folder of the site. I can access the log files via the Kudu site.
I use ab
to trigger site requests load:
ab -n 1000000 -c 2 http://juzhuwarmup.azurewebsites.net/home/about
When the load test is on, I scale out the plan to add a new worker instance.
The ab
requests are not sent the new instance, until the warmup request is returned.
public string NoSleep()
{
TimeSpan differnce = DateTime.Now - startupTimestamp;
if (differnce < TimeSpan.FromSeconds(80))
{
Response.StatusCode = 502;
}
return "Warming up";
}