Parallelise some troublesome loops

This commit is contained in:
Chomp 2021-08-31 18:52:28 +01:00
parent f0d48619bf
commit d38cc9c3ea
2 changed files with 14 additions and 14 deletions

View File

@ -8,6 +8,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Threading.Tasks;
namespace Generator namespace Generator
{ {
@ -28,15 +29,15 @@ namespace Generator
LoggingHelpers.LogToConsole("Started processing bot loot"); LoggingHelpers.LogToConsole("Started processing bot loot");
// Iterate over assault/raider etc // Iterate over assault/raider etc
foreach (var botToUpdate in _botsWithGear) Parallel.ForEach(_botsWithGear, botToUpdate =>
{ {
var rawBotsOfSameType = _rawParsedBots var rawBotsOfSameType = _rawParsedBots
.Where(x => x.Info.Settings.Role.Equals(botToUpdate.botType.ToString(), StringComparison.OrdinalIgnoreCase)) .Where(x => x.Info.Settings.Role.Equals(botToUpdate.botType.ToString(), StringComparison.OrdinalIgnoreCase))
.ToList(); .ToList();
if (rawBotsOfSameType.Count == 0) if (rawBotsOfSameType.Count == 0)
{ {
continue; return;
} }
foreach (var rawParsedBot in rawBotsOfSameType) foreach (var rawParsedBot in rawBotsOfSameType)
@ -48,7 +49,7 @@ namespace Generator
AddBackbackLoot(botToUpdate, rawBotsOfSameType); AddBackbackLoot(botToUpdate, rawBotsOfSameType);
AddSecureContainerLoot(botToUpdate, rawBotsOfSameType); AddSecureContainerLoot(botToUpdate, rawBotsOfSameType);
AddSpecialLoot(botToUpdate); AddSpecialLoot(botToUpdate);
} });
stopwatch.Stop(); stopwatch.Stop();
LoggingHelpers.LogToConsole($"Finished processing bot loot. Took: {LoggingHelpers.LogTimeTaken(stopwatch.Elapsed.TotalSeconds)} seconds"); LoggingHelpers.LogToConsole($"Finished processing bot loot. Took: {LoggingHelpers.LogTimeTaken(stopwatch.Elapsed.TotalSeconds)} seconds");

View File

@ -7,6 +7,7 @@ using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Threading.Tasks;
namespace Generator namespace Generator
{ {
@ -30,22 +31,20 @@ namespace Generator
Console.WriteLine($"{botFiles.Count} bot dump files found"); Console.WriteLine($"{botFiles.Count} bot dump files found");
var parsedBots = new List<Datum>(); var parsedBots = new List<Datum>();
foreach (var file in botFiles) Parallel.ForEach(botFiles, file => {
{
var splitFile = file.Split("\\"); var splitFile = file.Split("\\");
var json = File.ReadAllText(file); var json = File.ReadAllText(file);
try try
{ {
json = PruneMalformedBsgJson(json, splitFile.Last()); json = PruneMalformedBsgJson(json, splitFile.Last());
var bots = ParseJson(json, file); var bots = ParseJson(json);
if (bots == null || bots.Count == 0) if (bots == null || bots.Count == 0)
{ {
Console.WriteLine($"skipping file: {splitFile.Last()}. no bots found, "); Console.WriteLine($"skipping file: {splitFile.Last()}. no bots found, ");
continue; return;
} }
Console.WriteLine($"parsing: {bots.Count} bots in file {splitFile.Last()}"); Console.WriteLine($"parsing: {bots.Count} bots in file {splitFile.Last()}");
@ -59,7 +58,7 @@ namespace Generator
failedFilesCount++; failedFilesCount++;
Console.WriteLine($"JSON Error message: {jex.Message} || file: {splitFile.Last()}"); Console.WriteLine($"JSON Error message: {jex.Message} || file: {splitFile.Last()}");
} }
} });
stopwatch.Stop(); stopwatch.Stop();
LoggingHelpers.LogToConsole($"Cleaned and Parsed: {parsedBots.Count} bots. Failed: {failedFilesCount}. Took {LoggingHelpers.LogTimeTaken(stopwatch.Elapsed.TotalSeconds)} seconds"); LoggingHelpers.LogToConsole($"Cleaned and Parsed: {parsedBots.Count} bots. Failed: {failedFilesCount}. Took {LoggingHelpers.LogTimeTaken(stopwatch.Elapsed.TotalSeconds)} seconds");
@ -87,10 +86,10 @@ namespace Generator
return o.ToString(); return o.ToString();
} }
private static List<Datum> ParseJson(string json, string file) private static List<Datum> ParseJson(string json)
{ {
//Console.WriteLine($"parsing file {file}"); var serialisedObject = JsonConvert.DeserializeObject<Root>(json);
var serialisedObject = JsonConvert.DeserializeObject<Models.Input.Root>(json);
return serialisedObject.data; return serialisedObject.data;
} }
} }