C
C#13mo ago
SWEETPONY

✅ How to improve Parallel.ForEachAsync usage?

private async Task<ConcurrentDictionary<string, string>> GetIpAddresses( HashSet<string> deviceIds )
{
var devicesIpAddresses = new ConcurrentDictionary<string, string>();

await Parallel.ForEachAsync(deviceIds, async (deviceId, _) =>
{
var archiveQuery = await _archiveClient
.Query( new QueryArguments
{
DeviceIDs = new HashSet<string> {deviceId},
Order = OrderType.Desc,
EventNames = new() {"nc100k:connected", "nc60k:connected", "nc8k:connected"},
Pagination = new() {PageSize = 1}
} )
.Unwrap();

var ip = archiveQuery.Items
.Select(verbalizedEvent => verbalizedEvent.Items
.FirstOrDefault(item => item.Type == "endpoint" )?.Title)
.FirstOrDefault() ?? "";

devicesIpAddresses[deviceId] = ip;
} );

return devicesIpAddresses;
}
private async Task<ConcurrentDictionary<string, string>> GetIpAddresses( HashSet<string> deviceIds )
{
var devicesIpAddresses = new ConcurrentDictionary<string, string>();

await Parallel.ForEachAsync(deviceIds, async (deviceId, _) =>
{
var archiveQuery = await _archiveClient
.Query( new QueryArguments
{
DeviceIDs = new HashSet<string> {deviceId},
Order = OrderType.Desc,
EventNames = new() {"nc100k:connected", "nc60k:connected", "nc8k:connected"},
Pagination = new() {PageSize = 1}
} )
.Unwrap();

var ip = archiveQuery.Items
.Select(verbalizedEvent => verbalizedEvent.Items
.FirstOrDefault(item => item.Type == "endpoint" )?.Title)
.FirstOrDefault() ?? "";

devicesIpAddresses[deviceId] = ip;
} );

return devicesIpAddresses;
}
6 Replies
SWEETPONY
SWEETPONY13mo ago
everything works for me, but I'm not sure if it's all well written
JakenVeina
JakenVeina13mo ago
I question the value of using parallelism here at all I question the idea of returning a ConcurrentDictionary as opposed to an IDictionary or just Dictionary otherwise, seems fine
chef drone builder
As with your last question, this does not look like CPU Bound work to me, so you shouldn’t be the Parallel class.
SWEETPONY
SWEETPONY13mo ago
I tried simple foreach and parallel version second one was x2 faster 53 ms vs 106 Hmm
JakenVeina
JakenVeina13mo ago
measured how? what did the foreach version look like?
SWEETPONY
SWEETPONY13mo ago
1.
var timeWatch = new StopWatch();
timeWatch.Start();
foreach(var deviceIdin deviceIds)
{
await blah blah..
}
timeWatch.Stop();
2.
var timeWatch2 = new StopWatch();
timeWatch2.Start();
await Parallel.ForEachAsync(deviceIds, async (deviceId, _) =>
{
await blah blah..
}
timeWatch2.Stop();
1.
var timeWatch = new StopWatch();
timeWatch.Start();
foreach(var deviceIdin deviceIds)
{
await blah blah..
}
timeWatch.Stop();
2.
var timeWatch2 = new StopWatch();
timeWatch2.Start();
await Parallel.ForEachAsync(deviceIds, async (deviceId, _) =>
{
await blah blah..
}
timeWatch2.Stop();
and the second one is faster ..