user746461
user746461

Reputation:

How to measure performance of Response.Redirect(url) and Response.Redirect(url,false)?

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.

I created a asp.net web page, whose only code is <code>Response.Redirect</code>

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.

I set <processModel maxWorkerThreads="4" maxIoThreads="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?

Upvotes: 1

Views: 884

Answers (1)

Aristos
Aristos

Reputation: 66641

There are one more important issue here and this is the security. For the moment let me say why the one is faster than the other.

The slow and unsecured one case Response.Redirect(url,endRespose:false) It is slower because using the false parameter you allow the full cycle of the page to run and render on client ! together with the redirect command ! So the page is normally runs and almost renders on client page - and for that reason is slower.

The other case that is throw an exception is stop the rest processing and stop further rendering of page - and left only the redirect command - and what you have render on page up to that time.

On this question and answer of mine I analyse and prove that if you stop the redirect of the page, you can see the full result of the page - if you have set redirect( ,false) with the false option.

Redirect to a page with endResponse to true VS CompleteRequest and security thread

To close, I always use the redirect with stop of the rest process of the page - but if I use the other one that allow the rest of the processing I have on my mind all the above - that the page is rendered and the extra time that's take.

Upvotes: 0

Related Questions