Many people[1][2] say Response.Redirect(url)
is bad, we should use Response.Redirect(url,false)
, because the former throws exception and kills the thread, and thus has scalability issue.
So I want to know the performance differences between the two ways, in numerical values.
I created a asp.net web page, whose only code is Response.Redirect
.
Then I wrote this console application to issue requests to the page.
private const int concurrentRequests = 800;
static void Main(string[] args)
{
Console.WriteLine("Type in the URL:");
var url = Console.ReadLine();
Console.WriteLine($"concurrentRequests={concurrentRequests}");
ServicePointManager.DefaultConnectionLimit = concurrentRequests;
List<Task> tasks = new List<Task>(concurrentRequests);
Stopwatch watch = new Stopwatch();
watch.Start();
for (int i = 0; i < concurrentRequests; i++)
{
Task t = new Task(o => GetResponse((string)o), url);
tasks.Add(t);
t.Start();
}
Task.WaitAll(tasks.ToArray());
watch.Stop();
Console.WriteLine($"Execution time: {watch.ElapsedMilliseconds}");
Console.ReadKey();
}
static void GetResponse(string url)
{
var request =(HttpWebRequest) WebRequest.Create(url);
request.AllowAutoRedirect = false;
var response = request.GetResponse();
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
var content = sr.ReadToEnd();
}
}
I also reduced asp.net threads to 4 in machine.config.
However, it turns out Response.Redirect(url) takes 350ms to execute while Response.Redirect(url,false) takes 415ms.
Why doesn't the result conform to the theory in the articles?