diff --git a/OsuParsers.Tests/Beatmaps.cs b/OsuParsers.Tests/Beatmaps.cs index 0e6f318..5823d11 100644 --- a/OsuParsers.Tests/Beatmaps.cs +++ b/OsuParsers.Tests/Beatmaps.cs @@ -73,6 +73,29 @@ public void ParseAll() } } + public void ParallelParseAll() + { + var timerAll = new Stopwatch(); + timerAll.Start(); + Parallel.ForEach(RawFiles, file => + { + var timer = new Stopwatch(); + timer.Start(); + var beatmap = BeatmapDecoder.Decode(file); + timer.Stop(); + Maps.Add(beatmap); + Trace.WriteLine(string.Format( + "Beatmap parsed in {0}ms: {1} - {2} [{3}] created by {4}.", + timer.Elapsed.Milliseconds, + beatmap.MetadataSection.Artist, + beatmap.MetadataSection.Title, + beatmap.MetadataSection.Version, + beatmap.MetadataSection.Creator)); + }); + timerAll.Stop(); + Trace.WriteLine($"All beatmaps parsed in {timerAll.Elapsed.Milliseconds}ms."); + } + private string BaseUrl => @"https://osu.ppy.sh/osu/"; private List BeatmapIDs = new List @@ -103,6 +126,15 @@ public async Task ParseAllTestingBeatmaps() ParseAll(); } + [TestMethod] + public async Task ParseAllTestingBeatmapsParallel() + { + RawFiles.Clear(); + await GetTestingBeatmaps(); + Trace.WriteLine("Parsing beatmaps in parallel..."); + ParallelParseAll(); + } + [DataTestMethod] [DataRow(10)] public async Task ParseRandomBeatmaps(int amount) diff --git a/OsuParsers/Decoders/BeatmapDecoder.cs b/OsuParsers/Decoders/BeatmapDecoder.cs index 38d4e6b..3c71da1 100644 --- a/OsuParsers/Decoders/BeatmapDecoder.cs +++ b/OsuParsers/Decoders/BeatmapDecoder.cs @@ -18,10 +18,6 @@ namespace OsuParsers.Decoders { public static class BeatmapDecoder { - private static Beatmap Beatmap; - private static FileSections currentSection = FileSections.None; - private static List sbLines = new List(); - /// /// Parses .osu file. /// @@ -35,12 +31,31 @@ public static Beatmap Decode(string path) throw new FileNotFoundException(); } + /// + /// Parses .osu file. + /// + /// Stream containing beatmap data. + /// A usable beatmap. + public static Beatmap Decode(Stream stream) => Decode(stream.ReadAllLines()); + /// /// Parses .osu file. /// /// Array of text lines containing beatmap data. /// A usable beatmap. public static Beatmap Decode(IEnumerable lines) + { + return new BeatmapDecodeTask().Run(lines); + } + } + + internal class BeatmapDecodeTask + { + private Beatmap Beatmap; + private FileSections currentSection = FileSections.None; + private List sbLines = new List(); + + public Beatmap Run(IEnumerable lines) { Beatmap = new Beatmap(); currentSection = FileSections.Format; @@ -68,14 +83,7 @@ public static Beatmap Decode(IEnumerable lines) return Beatmap; } - /// - /// Parses .osu file. - /// - /// Stream containing beatmap data. - /// A usable beatmap. - public static Beatmap Decode(Stream stream) => Decode(stream.ReadAllLines()); - - private static void ParseLine(string line) + private void ParseLine(string line) { switch (currentSection) { @@ -109,7 +117,7 @@ private static void ParseLine(string line) } } - private static void ParseGeneral(string line) + private void ParseGeneral(string line) { int index = line.IndexOf(':'); string variable = line.Remove(index).Trim(); @@ -160,7 +168,7 @@ private static void ParseGeneral(string line) } } - private static void ParseEditor(string line) + private void ParseEditor(string line) { int index = line.IndexOf(':'); string variable = line.Remove(index).Trim(); @@ -186,7 +194,7 @@ private static void ParseEditor(string line) } } - private static void ParseMetadata(string line) + private void ParseMetadata(string line) { int index = line.IndexOf(':'); string variable = line.Remove(index).Trim(); @@ -227,7 +235,7 @@ private static void ParseMetadata(string line) } } - private static void ParseDifficulty(string line) + private void ParseDifficulty(string line) { int index = line.IndexOf(':'); string variable = line.Remove(index).Trim(); @@ -256,7 +264,7 @@ private static void ParseDifficulty(string line) } } - private static void ParseEvents(string line) + private void ParseEvents(string line) { string[] tokens = line.Split(','); @@ -290,7 +298,7 @@ private static void ParseEvents(string line) } } - private static void ParseTimingPoints(string line) + private void ParseTimingPoints(string line) { string[] tokens = line.Split(','); @@ -334,7 +342,7 @@ private static void ParseTimingPoints(string line) }); } - private static void ParseColours(string line) + private void ParseColours(string line) { int index = line.IndexOf(':'); string variable = line.Remove(index).Trim(); @@ -354,7 +362,7 @@ private static void ParseColours(string line) } } - private static void ParseHitObjects(string line) + private void ParseHitObjects(string line) { string[] tokens = line.Split(','); diff --git a/OsuParsers/Decoders/StoryboardDecoder.cs b/OsuParsers/Decoders/StoryboardDecoder.cs index 423bf4e..9e99bb4 100644 --- a/OsuParsers/Decoders/StoryboardDecoder.cs +++ b/OsuParsers/Decoders/StoryboardDecoder.cs @@ -15,10 +15,6 @@ namespace OsuParsers.Decoders { public static class StoryboardDecoder { - private static Storyboard storyboard; - private static IStoryboardObject lastDrawable; - private static CommandGroup commandGroup; - /// /// Parses .osb file. /// @@ -32,12 +28,31 @@ public static Storyboard Decode(string path) throw new FileNotFoundException(); } + /// + /// Parses .osb file. + /// + /// Stream containing storyboard data. + /// A usable storyboard. + public static Storyboard Decode(Stream stream) => Decode(stream.ReadAllLines()); + /// /// Parses .osb file. /// /// Array of text lines containing storyboard data. /// A usable storyboard. public static Storyboard Decode(IEnumerable lines) + { + return new StoryboardDecodeTask().Run(lines); + } + } + + internal class StoryboardDecodeTask + { + private Storyboard storyboard; + private IStoryboardObject lastDrawable; + private CommandGroup commandGroup; + + public Storyboard Run(IEnumerable lines) { storyboard = new Storyboard(); lastDrawable = null; @@ -65,14 +80,7 @@ public static Storyboard Decode(IEnumerable lines) return storyboard; } - /// - /// Parses .osb file. - /// - /// Stream containing storyboard data. - /// A usable storyboard. - public static Storyboard Decode(Stream stream) => Decode(stream.ReadAllLines()); - - private static string ParseVariables(string line) + private string ParseVariables(string line) { if (storyboard.Variables == null || line.IndexOf('$') < 0) return line; @@ -83,7 +91,7 @@ private static string ParseVariables(string line) return line; } - private static void ParseSbObject(string line) + private void ParseSbObject(string line) { string[] tokens = line.Split(','); @@ -129,7 +137,7 @@ private static void ParseSbObject(string line) } } - private static void ParseSbCommand(string line) + private void ParseSbCommand(string line) { int depth = 0; while (line.StartsWith(" ") || line.StartsWith("_"))