I have a HttpClient
and try to download roughly 25k 6byte size files into ram in order to generate a SHA256 key from it.
I have tried to make it parallel to the best of my ability but couldn't see a change in speed at all.
The wrapping function which is starting the download tasks in parallel:
SemaphoreSlim downloadConcurrencySemaphore = new SemaphoreSlim(40);
ConcurrentQueue<Task> Sha256Tasks = new ConcurrentQueue<Task>();
foreach (string url in urls)
{
var t = Task.Run(async () =>
{
await downloadConcurrencySemaphore.WaitAsync();
try
{
await uploadedFile.CalculateChecksum();
}
catch (Exception ex)
{
{ } // breakpoint for debugging
}
finally
{
downloadConcurrencySemaphore.Release();
}
});
Sha256Tasks.Enqueue(t);
}
Task.WaitAll(Sha256Tasks.ToArray());
The CalculateChecksum
called by wrapping function, downloads file and generates sha256sum from byte array resulting from download:
public async Task CalculateChecksum()
{ // is beeing called up to 40 times in parallel (connection limit)
byte[] file = await API.DownloadClient.Download(URL);
Sha256Sum = Sha256.GetSha256Sum(file);
}
The DownloadClient
class being called in parallel to download the files:
internal static class DownloadClient
{
static DownloadClient()
{
_Client = new HttpClient();
ServicePointManager.DefaultConnectionLimit = 40;
var handler = new HttpClientHandler();
handler.ClientCertificateOptions = ClientCertificateOption.Manual;
handler.ServerCertificateCustomValidationCallback =
(httpRequestMessage, cert, cetChain, policyErrors) =>
{
return true;
};
handler.MaxConnectionsPerServer = 40;
_Client = new HttpClient(handler);
}
private static HttpClient _Client;
internal static async Task<byte[]> Download(string url)
{
HttpResponseMessage response = await _Client.GetAsync(url);
response.EnsureSuccessStatusCode();
System.Net.Http.HttpContent content = response.Content;
byte[] file = await content.ReadAsByteArrayAsync();
return file;
}
}
Any Idea on how to make the process faster? Right now it takes ~15 minutes for ~5.5mb of files, processor and network barely used, program is idling around, waiting for downloads.