private static PerformanceRecorder DownloadTest <T>(bool useManager = true, bool useRecorder = true, bool useMD5Validation = false) where T : Stream, new() { var tasks = new List <Task <PerformanceRecorder> >(); foreach (var n in Enumerable.Range(0, numFiles)) { var blobName = $"{n}.dat"; var hash = File.ReadAllBytes(Path.Combine(TempFolderPath, $"{n}-md5.dat")); tasks.Add(DownloadBlob <T>(blobName, hash, useManager: useManager, useRecorder: useRecorder, useMD5Validation: useMD5Validation)); } PerformanceRecorder recorder = null; foreach (var result in Task.WhenAll(tasks).Result) { if (recorder == null) { recorder = result; } else { recorder.Merge(result); } } Console.WriteLine($"Total time was {recorder.Seconds} seconds at {recorder.AvgMbps} Mbps for {recorder.MBits * 1000 * 1000 / 1024 / 1024 / 8} MB"); return(recorder); }
/// <summary> /// Download a file, verify the hash, and return a throughput record of the transfer /// </summary> private static async Task <PerformanceRecorder> DownloadBlob <T>(string blobName, byte[] hash, bool useManager = true, bool useRecorder = true, bool useMD5Validation = false) where T : Stream, new() { Console.WriteLine($"Starting download of {blobName}"); // Create the source CloudBlob instances var blob = await Util.GetCloudBlobAsync(ContainerName, blobName, BlobType.BlockBlob); PerformanceRecorder recorder = null; using (var stream = new T()) { recorder = new PerformanceRecorder(useRecorder ? stream : null); // Start the blob download recorder.Start(); if (useManager) { await TransferManager.DownloadAsync(blob, stream, new DownloadOptions { DisableContentMD5Validation = !useMD5Validation, UseTransactionalMD5 = false }, new SingleTransferContext()); } else { await blob.DownloadToStreamAsync(stream); } recorder.Stop(); recorder.Bytes = stream.Length; if (VerifyStream(stream, hash)) { Console.WriteLine($"Downloaded {blobName}: Average speed {recorder.AvgMbps} Mbps"); } else { Console.WriteLine($"Failed to download {blobName}: Invalid hash"); } } return(recorder); }