Reputation: 111
For a school assignment I have to show that downloading a 1mb file 100 times is slower than downloading a 100mb file a single time.
The theory is: Downloading a 1mb file 100 times requires more total bytes because of the headers that are sent with it.
Now I wrote a script that times both scenarios and displays it in the console. However, I would like to show the total amount of bytes recieved to show why it is taking longer.
This is what I have so far:
class TestClass
{
private Stopwatch timer;
private WebClient downloader;
private TimeSpan elapsed;
private string elapsedTime;
public void SetUp()
{
timer = new Stopwatch();
downloader = new WebClient();
}
public void StartTest()
{
Console.WriteLine("Starting test");
Console.WriteLine("Downloading 1mb file x100, please wait...");
timer.Start();
for (int i = 0; i < 100; i++)
{
downloader.DownloadFile("http://ftp.telfort.nl/pub/test/1megabyte.bin", @"c:\tempdownload\1mb.txt");
}
timer.Stop();
elapsed = timer.Elapsed;
// Format and display the TimeSpan value.
elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
elapsed.Hours, elapsed.Minutes, elapsed.Seconds,
elapsed.Milliseconds / 10);
Console.WriteLine("RunTime when downloading 1MB 100 times is: " + elapsedTime);
// Now go for 100MB
timer.Reset();
Console.WriteLine("Downloading 100mb file 1x, please wait...");
timer.Start();
downloader.DownloadFile("http://ftp.telfort.nl/pub/test/100megabyte.bin", @"c:\tempdownload\100mb.txt");
timer.Stop();
elapsed = timer.Elapsed;
// Format and display the TimeSpan value.
elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
elapsed.Hours, elapsed.Minutes, elapsed.Seconds,
elapsed.Milliseconds / 10);
Console.WriteLine("RunTime when downloading 100MB 1 time is: " + elapsedTime);
// Finally
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
}
}
Is there a way to accomplish this?
EDIT: I think the title may have been misleading...
Upvotes: 1
Views: 1315
Reputation: 151586
So you want to measure the size of the entire reponse.
The HTTP classes that the BCL offers (HttpWebResponse, WebClient, HttpClient) do not have a way to extract the entire request or response message size (including status-line, response-headers and message-body).
You can emulate this by counting the length of the header's names and values, but due to folding and the condensation of multiple headers into one dictionary entry this won't be entirely accurate.
You can easily make a trivial HTTP GET request using sockets though, and simply make an HTTP/1.0 request to let the server close the connection after sending the entire response (so you won't have to re-implement an HTTP client over sockets).
But the headers are not your issue. Over 100 HTTP requests for a file of 1 MB, the request and response headers will account for a measly ~ 150 bytes per request, or a marginal 15 KB for the total transfer of 100 MB of payload.
The real delay will be caused by the creation of 100 separate TCP connections, and their typical slow start.
Upvotes: 2