title: Why upload to Azure blob so slow?
body: <p>I have a custom stream that is used to perform write operations directly into the page cloud blob.</p>
  
  <pre><code>public sealed class WindowsAzureCloudPageBlobStream : Stream
  {
      // 4 MB is the top most limit for page blob write operations
      public const int MaxPageWriteCapacity = 4 * 1024 * 1024;
  
      // Every operation on a page blob has to manipulate a value which is rounded up to 512 bytes
      private const int PageBlobPageAdjustmentSize = 512;
  
      private CloudPageBlob _pageBlob;
  
      public override void Write(byte[] buffer, int offset, int count)
      {
          var additionalOffset = 0;
          var bytesToWriteTotal = count;
  
          List&lt;Task&gt; list = new List&lt;Task&gt;();
          while (bytesToWriteTotal &gt; 0)
          {
              var bytesToWriteTotalAdjusted = RoundUpToPageBlobSize(bytesToWriteTotal);
  
              // Azure does not allow us to write as many bytes as we want
              // Max allowed size per write is 4MB
              var bytesToWriteNow = Math.Min((int)bytesToWriteTotalAdjusted, MaxPageWriteCapacity);
              var adjustmentBuffer = new byte[bytesToWriteNow];
              ...
              var memoryStream = new MemoryStream(adjustmentBuffer, 0, bytesToWriteNow, false, false);
              var task = _pageBlob.WritePagesAsync(memoryStream, Position, null);
              list.Add(task);
          }
  
          Task.WaitAll(list.ToArray());
      }
  
      private static long RoundUpToPageBlobSize(long size) 
      { 
          return (size + PageBlobPageAdjustmentSize - 1) &amp; ~(PageBlobPageAdjustmentSize - 1); 
      }
  </code></pre>
  
  <p>I have a low performance of <code>Write()</code>. For example: </p>
  
  <pre><code>Stopwatch s = new Stopwatch();
  s.Start();
  using (var memoryStream = new MemoryStream(adjustmentBuffer, 0, bytesToWriteNow, false, false))
  {
        _pageBlob.WritePages(memoryStream, Position);
  }
  
  s.Stop();
  Console.WriteLine(s.Elapsed); =&gt; 00:00:01.52 == Average speed 2.4 MB/s
  </code></pre>
  
  <p>How can I improve my algorithm?
  How to use <code>Parallel.ForEach</code> to speedup the process?</p>
  
  <p>Why just only 2.5 MB/sec, but not a 60MB/sec as in <a href="https://azure.microsoft.com/en-us/documentation/articles/storage-performance-checklist/" rel="nofollow">official site</a>  or <a href="http://blogs.microsoft.co.il/applisec/2012/01/05/windows-azure-benchmarks-part-2-blob-write-throughput/" rel="nofollow">http://blogs.microsoft.co.il/applisec/2012/01/05/windows-azure-benchmarks-part-2-blob-write-throughput/</a></p>