Scenario: There is a WCF streaming service that provides file streams to clients. On the client side, the WCF file stream is read by calling stream.Read (buffer, 0, chunkSize), where chunkSize is 10240.
I wonder if the data is transmitted over the network (Internet) only when the Read call is made, or something that reads the data in the background, and potentially the data is already read when the stream.Read call is made. I ask about this because I need to measure the download speed. But after I read a piece of data, I perform other operations - sending events, writing data just read to the local file stream. So I need to challenge these other operations so that I have a clear picture of how much time it took to download only the file. Therefore, I continue to measure how long it takes to download 10 KB of data, and at the end, kbsPerSecond is calculated using the total file size and total time.
Unfortunately, the presence of a stopwatch in the cycle leads to a decrease in performance, but I think I will have to live with it if I want to correctly measure the lane.
long totalBytesRead = 0;
Stopwatch stopwatch = new Stopwatch();
do
{
int bytesRead;
stopwatch.Start();
bytesRead = stream.Read(buffer, 0, chunkSize);
stopwatch.Stop();
if (bytesRead == 0)
break;
totalBytesRead += bytesRead;
} while (true);
long elapsedTime = stopwatch.ElapsedMilliseconds;
int kbsPerSecond = Convert.ToInt32(((double)totalBytesRead / 1024) / ((double)elapsedTime / 1000));
Will this approach be accurate?
source
share