Hello,
We have a problem with the speed of our downloads. And depending on method, it can go 20x faster/slower.
The slow method is HttpContent.ReadAsStreamAsync(). We use this method so we can make a progression on the download.
I tried to use HttpContent.ReadAsByteArrayAsync(), this is so much faster !!
Is it normal ? How can I improve download speed of ReadAsStreamAsync ?
It tried to enlarge the size of the buffer, that didnt change anything...
Weird thing : ReadAsStreamAsync speed seems relatively OK when running on an emulator.
Simplified code below for the 2 tests
Slow method :
int bufferSizeHttp = 1024 * 4;
using (Stream stream = await response.Content.ReadAsStreamAsync())
{
do
{
var percentBefore = percent;
try
{
var bytesRead = await stream.ReadAsync(buffer, 0, bufferSizeHttp);
if (bytesRead > 0)
{
var data = new byte[bytesRead];
buffer.ToList().CopyTo(0, data, 0, bytesRead);
await saveFileStream.WriteAsync(data, 0, data.Length);
await saveFileStream.FlushAsync();
totalBytesRead += bytesRead;
percent = Math.Floor((double)(totalBytesRead * 100 / totalBytes));
}
//other stuff
Fast method :
var data = await response.Content.ReadAsByteArrayAsync();
do
{
var percentBefore = percent;
try
{
var bytesRead = data.Length;
if (bytesRead > 0)
{
await saveFileStream.WriteAsync(data, 0, bytesRead);
await saveFileStream.FlushAsync();
totalBytesRead += bytesRead;
percent = Math.Floor((double)(totalBytesRead * 100 / totalBytes));
}
//other stuff