WCF stream.

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;

    // 1. write to local file stream
    // 2. raise file download progress events

} while (true);

long elapsedTime = stopwatch.ElapsedMilliseconds;
int kbsPerSecond = Convert.ToInt32(((double)totalBytesRead / 1024) / ((double)elapsedTime / 1000));

Will this approach be accurate?

+4
source share
3 answers

Have you measured the actual impact of a stopwatch on performance? I just ran a quick test on my machine (like YMMV) with an external stopwatch to measure the total time, and a stopwatch inside the loop to simulate the one you are using. The cycle was repeated a million times, and the overall performance impact of the internal stopwatch was only 49 milliseconds (scattered across all 1 million iterations), so you are talking about low single digits per iteration.

, , .

+3

, , , , , .

, : , chunkSize

, , bytesRead == 0 incrementing totalBytesRead , , stopwatch.start/stop -

+1

- , - ( ), , ( , , ):

byte[] buffer = new byte[int.MaxValue]; // Alternatively, enter the size of the content
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
int totalBytesRead = stream.Read(buffer, 0, buffer.Length);
stopwatch.Stop();
long elapsedTime = stopwatch.ElapsedMilliseconds;
double kbsPerSecond = ((double)totalBytesRead / 1024) / ((double)elapsedTime / 1000);

However, if this is production code, and you are trying to measure real life, then your approach is correct, and the extra time spent on making Stop and Start methods is insignificant compared to the time spent reading from the stream.

0
source

Source: https://habr.com/ru/post/1547072/


All Articles