Reputation: 157
How can I measure the download rate when copying from a stream? Here's my download code:
var client = new HttpClient();
using (var respStream = await client.GetStreamAsync(Url))
{
using (var file = new FileStream(FileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite))
{
await CopyToAsync(respStream, file);
file.Flush();
}
}
PS: I'm using a custom CopyToAsync
implementation, but that's not really relevant.
Upvotes: 2
Views: 220
Reputation: 157
I resolved it. The answer was to create a cache where I add the bytesRead
value I got out of the sourceStream.ReadAsync(...)
in my custom Stream::CopyToAsync(...)
implementation. Then do the standard speed calculation:
(I used a stopwatch to measure the time it took to download)
bytesPerSecond = bytesRead / watch.Elapsed.TotalSeconds
If you'd like to see the implementation of Stream::CopyToAsync(...)
, you can find that here: https://referencesource.microsoft.com/#mscorlib/system/io/stream.cs,08ee62b6d544c8fe
Upvotes: 2