title: What is a valid way to track ReadAsStreamAsync download speed rate
body: <p>I'm learning C# 5 async / await on some real-world example - save a file from web with displaying download speed rate.</p>
  
  <p>There is a method that I'm worried is wrong, especially with a download speed calculation.</p>
  
  <pre><code>public async Task DownloadAsyncDemo(Uri requestUri, IProgress&lt;string&gt; progress, CancellationToken cancellationToken)
          {
              var downloadedFileName = "file";
              var downloadDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "TmpDownload");
  
              using (var httpClient = new HttpClient())
              {
                  using (var request = new HttpRequestMessage(HttpMethod.Get, requestUri))
                  {
                      using (var responseMessage = await httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false))
                      {
                          if (responseMessage.Content.Headers.ContentDisposition != null)
                              downloadedFileName = responseMessage.Content.Headers.ContentDisposition.FileName;
                          else
                              downloadedFileName = Path.GetFileName(requestUri.LocalPath);
  
                          downloadedFileName = Path.Combine(downloadDirectory, downloadedFileName);
  
                          if (!Directory.Exists(downloadDirectory))
                              Directory.CreateDirectory(downloadDirectory);
  
                          int bufferSize = 1024;  // 1 KB buffer
                          using (var httpStream = await responseMessage.Content.ReadAsStreamAsync().ConfigureAwait(false))
                          {
                              using (var filestream = new FileStream(downloadedFileName, FileMode.Create, FileAccess.Write, FileShare.None, 4084, true))
                              {
                                  //await httpStream.CopyToAsync(filestream); // need more low level logic below
                                  Stopwatch sw = new Stopwatch();
                                  byte[] buffer = new byte[bufferSize];
                                  while (true)
                                  {
                                      sw.Reset();
                                      sw.Start();
                                      int num = await httpStream.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false);
                                      sw.Stop();
                                      if (sw.ElapsedMilliseconds &gt; 0)
                                          progress.Report(string.Format("Time taken: {0} ms Data size: {1} KB Speed: {2} kbps", sw.ElapsedMilliseconds, num, num * 8 / sw.ElapsedMilliseconds));
                                      int bytesRead;
                                      if ((bytesRead = num) != 0)
                                          await filestream.WriteAsync(buffer, 0, bytesRead, cancellationToken).ConfigureAwait(false);
                                      else
                                          break;
                                  }
                              }
                          }
                      }
                  }
              }
          }
  </code></pre>
  
  <p>I run it like:</p>
  
  <pre><code>var dManger = DownloadManager.Instance;
  var cancelationToken = new CancellationToken();
  Task.Run(() =&gt; dManger.DownloadAsyncDemo(new Uri("http://speedtest.tokyo.linode.com/100MB-tokyo.bin")
                  , new Progress&lt;string&gt;(data =&gt; Console.WriteLine(data)), cancelationToken));
  </code></pre>
  
  <p>I get result like:</p>
  
  <p><img src="https://i.stack.imgur.com/hSJiZ.png" alt="enter image description here"></p>
  
  <p>I'm worried that something is wrong with my code because for download speed rate calculation I get speed from 81 to 8192 kpbs.
  In my async task example I used download rate counting approach taken from <a href="https://stackoverflow.com/questions/4641733/measure-data-transfer-rate-over-tcp-using-c-sharp">Measure data transfer rate over tcp using c#</a></p>
  
  <p>What am I doing wrong in the code?</p>