Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logging Enhancements #1521

Merged
merged 9 commits into from
Sep 13, 2022
11 changes: 5 additions & 6 deletions API.Benchmark/ArchiveSerivceBenchmark.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
namespace API.Benchmark
namespace API.Benchmark;

public class ArchiveSerivceBenchmark
{
public class ArchiveSerivceBenchmark
{
// Benchmark to test default GetNumberOfPages from archive
// vs a new method where I try to open the archive and return said stream
}
// Benchmark to test default GetNumberOfPages from archive
// vs a new method where I try to open the archive and return said stream
}
95 changes: 47 additions & 48 deletions API.Benchmark/ParserBenchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,75 +5,74 @@
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Order;

namespace API.Benchmark
namespace API.Benchmark;

[MemoryDiagnoser]
[Orderer(SummaryOrderPolicy.FastestToSlowest)]
[RankColumn]
public class ParserBenchmarks
{
[MemoryDiagnoser]
[Orderer(SummaryOrderPolicy.FastestToSlowest)]
[RankColumn]
public class ParserBenchmarks
{
private readonly IList<string> _names;
private readonly IList<string> _names;

private static readonly Regex NormalizeRegex = new Regex(@"[^a-zA-Z0-9]",
RegexOptions.IgnoreCase | RegexOptions.Compiled,
TimeSpan.FromMilliseconds(300));
private static readonly Regex NormalizeRegex = new Regex(@"[^a-zA-Z0-9]",
RegexOptions.IgnoreCase | RegexOptions.Compiled,
TimeSpan.FromMilliseconds(300));

private static readonly Regex IsEpub = new Regex(@"\.epub",
RegexOptions.IgnoreCase | RegexOptions.Compiled,
TimeSpan.FromMilliseconds(300));
private static readonly Regex IsEpub = new Regex(@"\.epub",
RegexOptions.IgnoreCase | RegexOptions.Compiled,
TimeSpan.FromMilliseconds(300));

public ParserBenchmarks()
{
// Read all series from SeriesNamesForNormalization.txt
_names = File.ReadAllLines("Data/SeriesNamesForNormalization.txt");
Console.WriteLine($"Performing benchmark on {_names.Count} series");
}
public ParserBenchmarks()
{
// Read all series from SeriesNamesForNormalization.txt
_names = File.ReadAllLines("Data/SeriesNamesForNormalization.txt");
Console.WriteLine($"Performing benchmark on {_names.Count} series");
}

private static string Normalize(string name)
{
// ReSharper disable once UnusedVariable
var ret = NormalizeRegex.Replace(name, string.Empty).ToLower();
var normalized = NormalizeRegex.Replace(name, string.Empty).ToLower();
return string.IsNullOrEmpty(normalized) ? name : normalized;
}
private static string Normalize(string name)
{
// ReSharper disable once UnusedVariable
var ret = NormalizeRegex.Replace(name, string.Empty).ToLower();
var normalized = NormalizeRegex.Replace(name, string.Empty).ToLower();
return string.IsNullOrEmpty(normalized) ? name : normalized;
}



[Benchmark]
public void TestNormalizeName()
[Benchmark]
public void TestNormalizeName()
{
foreach (var name in _names)
{
foreach (var name in _names)
{
Normalize(name);
}
Normalize(name);
}
}


[Benchmark]
public void TestIsEpub()
[Benchmark]
public void TestIsEpub()
{
foreach (var name in _names)
{
foreach (var name in _names)
if ((name).ToLower() == ".epub")
{
if ((name).ToLower() == ".epub")
{
/* No Operation */
}
/* No Operation */
}
}
}

[Benchmark]
public void TestIsEpub_New()
[Benchmark]
public void TestIsEpub_New()
{
foreach (var name in _names)
{
foreach (var name in _names)
{

if (Path.GetExtension(name).Equals(".epub", StringComparison.InvariantCultureIgnoreCase))
{
/* No Operation */
}
if (Path.GetExtension(name).Equals(".epub", StringComparison.InvariantCultureIgnoreCase))
{
/* No Operation */
}
}
}


}
}
29 changes: 14 additions & 15 deletions API.Benchmark/Program.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
using BenchmarkDotNet.Running;

namespace API.Benchmark
namespace API.Benchmark;

/// <summary>
/// To build this, cd into API.Benchmark directory and run
/// dotnet build -c Release
/// then copy the outputted dll
/// dotnet copied_string\API.Benchmark.dll
/// </summary>
public static class Program
{
/// <summary>
/// To build this, cd into API.Benchmark directory and run
/// dotnet build -c Release
/// then copy the outputted dll
/// dotnet copied_string\API.Benchmark.dll
/// </summary>
public static class Program
private static void Main(string[] args)
{
private static void Main(string[] args)
{
//BenchmarkRunner.Run<ParseScannedFilesBenchmarks>();
//BenchmarkRunner.Run<TestBenchmark>();
//BenchmarkRunner.Run<ParserBenchmarks>();
BenchmarkRunner.Run<EpubBenchmark>();
//BenchmarkRunner.Run<ParseScannedFilesBenchmarks>();
//BenchmarkRunner.Run<TestBenchmark>();
//BenchmarkRunner.Run<ParserBenchmarks>();
BenchmarkRunner.Run<EpubBenchmark>();

}
}
}
83 changes: 41 additions & 42 deletions API.Benchmark/TestBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,61 +6,60 @@
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Order;

namespace API.Benchmark
namespace API.Benchmark;

/// <summary>
/// This is used as a scratchpad for testing
/// </summary>
[MemoryDiagnoser]
[Orderer(SummaryOrderPolicy.FastestToSlowest)]
[RankColumn]
public class TestBenchmark
{
/// <summary>
/// This is used as a scratchpad for testing
/// </summary>
[MemoryDiagnoser]
[Orderer(SummaryOrderPolicy.FastestToSlowest)]
[RankColumn]
public class TestBenchmark
private static IEnumerable<VolumeDto> GenerateVolumes(int max)
{
private static IEnumerable<VolumeDto> GenerateVolumes(int max)
var random = new Random();
var maxIterations = random.Next(max) + 1;
var list = new List<VolumeDto>();
for (var i = 0; i < maxIterations; i++)
{
var random = new Random();
var maxIterations = random.Next(max) + 1;
var list = new List<VolumeDto>();
for (var i = 0; i < maxIterations; i++)
list.Add(new VolumeDto()
{
list.Add(new VolumeDto()
{
Number = random.Next(10) > 5 ? 1 : 0,
Chapters = GenerateChapters()
});
}

return list;
Number = random.Next(10) > 5 ? 1 : 0,
Chapters = GenerateChapters()
});
}

private static List<ChapterDto> GenerateChapters()
{
var list = new List<ChapterDto>();
for (var i = 1; i < 40; i++)
{
list.Add(new ChapterDto()
{
Range = i + string.Empty
});
}

return list;
}
return list;
}

private static void SortSpecialChapters(IEnumerable<VolumeDto> volumes)
private static List<ChapterDto> GenerateChapters()
{
var list = new List<ChapterDto>();
for (var i = 1; i < 40; i++)
{
foreach (var v in volumes.Where(vDto => vDto.Number == 0))
list.Add(new ChapterDto()
{
v.Chapters = v.Chapters.OrderByNatural(x => x.Range).ToList();
}
Range = i + string.Empty
});
}

[Benchmark]
public void TestSortSpecialChapters()
return list;
}

private static void SortSpecialChapters(IEnumerable<VolumeDto> volumes)
{
foreach (var v in volumes.Where(vDto => vDto.Number == 0))
{
var volumes = GenerateVolumes(10);
SortSpecialChapters(volumes);
v.Chapters = v.Chapters.OrderByNatural(x => x.Range).ToList();
}
}

[Benchmark]
public void TestSortSpecialChapters()
{
var volumes = GenerateVolumes(10);
SortSpecialChapters(volumes);
}

}
23 changes: 11 additions & 12 deletions API.Tests/Comparers/ChapterSortComparerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@
using API.Comparators;
using Xunit;

namespace API.Tests.Comparers
namespace API.Tests.Comparers;

public class ChapterSortComparerTest
{
public class ChapterSortComparerTest
[Theory]
[InlineData(new[] {1, 2, 0}, new[] {1, 2, 0})]
[InlineData(new[] {3, 1, 2}, new[] {1, 2, 3})]
[InlineData(new[] {1, 0, 0}, new[] {1, 0, 0})]
public void ChapterSortTest(int[] input, int[] expected)
{
[Theory]
[InlineData(new[] {1, 2, 0}, new[] {1, 2, 0})]
[InlineData(new[] {3, 1, 2}, new[] {1, 2, 3})]
[InlineData(new[] {1, 0, 0}, new[] {1, 0, 0})]
public void ChapterSortTest(int[] input, int[] expected)
{
Assert.Equal(expected, input.OrderBy(f => f, new ChapterSortComparer()).ToArray());
}

Assert.Equal(expected, input.OrderBy(f => f, new ChapterSortComparer()).ToArray());
}
}

}
47 changes: 23 additions & 24 deletions API.Tests/Comparers/StringLogicalComparerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,32 @@
using API.Comparators;
using Xunit;

namespace API.Tests.Comparers
namespace API.Tests.Comparers;

public class StringLogicalComparerTest
{
public class StringLogicalComparerTest
[Theory]
[InlineData(
new[] {"x1.jpg", "x10.jpg", "x3.jpg", "x4.jpg", "x11.jpg"},
new[] {"x1.jpg", "x3.jpg", "x4.jpg", "x10.jpg", "x11.jpg"}
)]
[InlineData(
new[] {"a.jpg", "aaa.jpg", "1.jpg", },
new[] {"1.jpg", "a.jpg", "aaa.jpg"}
)]
[InlineData(
new[] {"a.jpg", "aaa.jpg", "1.jpg", "!cover.png"},
new[] {"!cover.png", "1.jpg", "a.jpg", "aaa.jpg"}
)]
public void StringComparer(string[] input, string[] expected)
{
[Theory]
[InlineData(
new[] {"x1.jpg", "x10.jpg", "x3.jpg", "x4.jpg", "x11.jpg"},
new[] {"x1.jpg", "x3.jpg", "x4.jpg", "x10.jpg", "x11.jpg"}
)]
[InlineData(
new[] {"a.jpg", "aaa.jpg", "1.jpg", },
new[] {"1.jpg", "a.jpg", "aaa.jpg"}
)]
[InlineData(
new[] {"a.jpg", "aaa.jpg", "1.jpg", "!cover.png"},
new[] {"!cover.png", "1.jpg", "a.jpg", "aaa.jpg"}
)]
public void StringComparer(string[] input, string[] expected)
{
Array.Sort(input, StringLogicalComparer.Compare);
Array.Sort(input, StringLogicalComparer.Compare);

var i = 0;
foreach (var s in input)
{
Assert.Equal(s, expected[i]);
i++;
}
var i = 0;
foreach (var s in input)
{
Assert.Equal(s, expected[i]);
i++;
}
}
}
23 changes: 11 additions & 12 deletions API.Tests/Converters/CronConverterTests.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
using API.Helpers.Converters;
using Xunit;

namespace API.Tests.Converters
namespace API.Tests.Converters;

public class CronConverterTests
{
public class CronConverterTests
[Theory]
[InlineData("daily", "0 0 * * *")]
[InlineData("disabled", "0 0 31 2 *")]
[InlineData("weekly", "0 0 * * 1")]
[InlineData("", "0 0 31 2 *")]
[InlineData("sdfgdf", "")]
public void ConvertTest(string input, string expected)
{
[Theory]
[InlineData("daily", "0 0 * * *")]
[InlineData("disabled", "0 0 31 2 *")]
[InlineData("weekly", "0 0 * * 1")]
[InlineData("", "0 0 31 2 *")]
[InlineData("sdfgdf", "")]
public void ConvertTest(string input, string expected)
{
Assert.Equal(expected, CronConverter.ConvertToCronNotation(input));
}
Assert.Equal(expected, CronConverter.ConvertToCronNotation(input));
}
}
Loading