Created
May 13, 2021 10:18
-
-
Save shawnweisfeld/5e791e76827319e77264a43047acc101 to your computer and use it in GitHub Desktop.
hier vs flat list
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Azure.Storage.Blobs; | |
using System; | |
using System.Collections.Concurrent; | |
using System.Diagnostics; | |
using System.Linq; | |
using System.Threading; | |
using System.Threading.Tasks; | |
namespace ListSpeedTest | |
{ | |
class Program | |
{ | |
static long _count = 0; | |
static long _bytes = 0; | |
static SemaphoreSlim _slim; | |
static ConcurrentBag<Task> _tasks; | |
static async Task Main(string[] args) | |
{ | |
string connection = ""; | |
string[] containerNames = new string[] { "" }; | |
foreach (var container in containerNames) | |
{ | |
await StraightList(connection, container); | |
await HierList(connection, container); | |
} | |
Console.WriteLine("done!"); | |
Console.ReadKey(); | |
} | |
static async Task StraightList(string connection, string container) | |
{ | |
Console.WriteLine($"StraightList for {container} starting."); | |
Stopwatch sw = new Stopwatch(); | |
sw.Start(); | |
var blobServiceClient = new BlobServiceClient(connection); | |
var blobContainerClient = blobServiceClient.GetBlobContainerClient(container); | |
_count = 0; | |
_bytes = 0; | |
await foreach (var item in blobContainerClient.GetBlobsAsync()) | |
{ | |
_count++; | |
_bytes += item.Properties.ContentLength.GetValueOrDefault(); | |
} | |
sw.Stop(); | |
Console.WriteLine($"StraightList for {container} finished {_count:N0} files {_bytes:N0} bytes in {sw.Elapsed.TotalSeconds:N0} seconds."); | |
} | |
static async Task HierList(string connection, string container) | |
{ | |
Console.WriteLine($"HierList for {container} starting."); | |
Stopwatch sw = new Stopwatch(); | |
sw.Start(); | |
_count = 0; | |
_bytes = 0; | |
_slim = new SemaphoreSlim(Environment.ProcessorCount * 8); | |
_tasks = new ConcurrentBag<Task>(); | |
ProcessFolder(connection, container, ""); | |
//wait for enough to get the todo list so we don't exit before we started | |
await Task.Delay(1000); | |
// wait while there are any tasks that have not finished | |
while (_tasks.Any(x => !x.IsCompleted)) | |
{ | |
Console.WriteLine("Waiting to finish."); | |
await Task.Delay(10000); | |
} | |
sw.Stop(); | |
Console.WriteLine($"HierList for {container} finished {_count:N0} files {_bytes:N0} bytes in {sw.Elapsed.TotalSeconds:N0} seconds."); | |
} | |
static void ProcessFolder(string connection, string container, string folder) | |
{ | |
_tasks.Add(Task.Run(async () => { | |
await _slim.WaitAsync(); | |
Console.WriteLine($"ProcessingFolder '{folder}'"); | |
var blobServiceClient = new BlobServiceClient(connection); | |
var blobContainerClient = blobServiceClient.GetBlobContainerClient(container); | |
await foreach (var item in blobContainerClient.GetBlobsByHierarchyAsync(prefix: folder, delimiter: "/")) | |
{ | |
if (item.IsPrefix) | |
{ | |
ProcessFolder(connection, container, item.Prefix); | |
} | |
else | |
{ | |
Interlocked.Add(ref _count, 1); | |
Interlocked.Add(ref _bytes, item.Blob.Properties.ContentLength.GetValueOrDefault()); | |
} | |
} | |
_slim.Release(); | |
})); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment