ThomasArdal
ThomasArdal

Reputation: 5229

Detailed timing info from HttpWebRequest

I'm trying to extract detailed timing info, much like the Timing tab in Chrome, from a HTTP request made through HttpWebRequest in .NET:

enter image description here

Anyone know if this is possible? I cannot find any other documentation, than wrapping the entire request in a Stopwatch. But I really want the details, like how long it takes to resolve from DNS, how long it takes to request, content download etc.

Upvotes: 4

Views: 632

Answers (4)

DAmbrozic
DAmbrozic

Reputation: 47

Use a packet sniffer such as wire shark it'll monitor all HTTP traffic and put time stamps on each send/received packet

Upvotes: 0

ThomasArdal
ThomasArdal

Reputation: 5229

For future reference, I'm answering my own question. While the proposed answer with the logger might work, I've found a better (IMO) and much simpler approach:

var waiting = new Stopwatch();
var contentDownload = new Stopwatch();
waiting.Start();
using (var webResponse = (HttpWebResponse)webRequest.GetResponse())
{
    waiting.Stop();
    contentDownload.Start();
    using (var reader = new StreamReader(webResponse.GetResponseStream()))
    {
        var body = reader.ReadToEnd();
        contentDownload.Stop();
    }
}

It's as simple as that really. Calling GetResponse corresponds Chrome's Waiting and GetResponseStream+ReadToEnd corresponds Content Download.

In this example I'm making a GET (body-less) request. Being able to time Request Sent would make sense, but not really achievable using this approach.

Upvotes: 3

csm8118
csm8118

Reputation: 1213

Well, I'm not sure if it will help, but poking around in the reference source I see references to GlobalLog and Logging. Perhaps you could create your own trace listener to capture the information you need. This post and this answer might be useful for you as it shows how to configure a trace listener.

Upvotes: 1

Chris Warnes
Chris Warnes

Reputation: 728

I would recommend looking into Miniprofiler https://miniprofiler.com/ and Glimpse http://getglimpse.com/

Glimpse is good to set up first, it will show you a lot of information about the http request you made just out of the box, but you can configure mini profiler to discover slow SQL queries etc. You have to write a bit of code to get it to give you the details but its very good at showing you where you have bottlenecks, and its possible to integrate it with Glimpse so that you can see all the information in the same place.

Upvotes: 0

Related Questions