tags:

views:

106

answers:

1

I am writing code to performance test a web site. I have the following code:

        string url = "http://xxxxxx";
        System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();

        System.Diagnostics.Stopwatch totalTime = new System.Diagnostics.Stopwatch();
        totalTime.Start();

        for (int i = 0; i < 10; i++)
        {
            stopwatch.Start();
            WebRequest request = HttpWebRequest.Create(url);
            WebResponse webResponse = request.GetResponse();
            webResponse.Close();
            stopwatch.Stop();
            textBox1.Text += "Time Taken " + i.ToString() + " = " + stopwatch.Elapsed.Milliseconds.ToString() + Environment.NewLine;
            stopwatch.Reset();

        }

        totalTime.Stop();
        textBox1.Text += "Total Time Taken = " + totalTime.Elapsed.Milliseconds.ToString() + Environment.NewLine;

Which is giving the following result:

Time Taken 0 = 88
Time Taken 1 = 161
Time Taken 2 = 218
Time Taken 3 = 417
Time Taken 4 = 236
Time Taken 5 = 217
Time Taken 6 = 217
Time Taken 7 = 218
Time Taken 8 = 409
Time Taken 9 = 48
Total Time Taken = 257

I had expected the total time to be the sum of the individual times. Can anybody see why it is not?

+13  A: 

Use TotalMilliseconds instead of Milliseconds and try your test again. Your results aren't what you think they are.

David Morton
+1 but I'm curious myself what the difference is?
Nate Bross
Thanks that solved it, but like Nate says, what is the difference?
Shiraz Bhaiji
Milliseconds gets the current milliseconds *part* of the time, so 1.234 seconds would give 234. TotalMilliseconds returns the whole time as milliseconds, so the above would be 1234.
Rob Levine
Yes, `Milliseconds` is only the milliseconds fraction, it's not including the seconds, minutes, hours etc. that have passed. You can also use the `ElapsedMilliseconds` or `ElapsedTicks` properties.
0xA3