mirror of
https://github.com/Kareadita/Kavita.git
synced 2026-03-10 03:55:45 -04:00
Co-authored-by: Joseph Milazzo <joseph.v.milazzo@gmail.com> Co-authored-by: Joe Milazzo <josephmajora@gmail.com>
95 lines
2.2 KiB
C#
95 lines
2.2 KiB
C#
using System.Text.RegularExpressions;
|
|
using BenchmarkDotNet.Attributes;
|
|
using BenchmarkDotNet.Order;
|
|
|
|
namespace Kavita.Benchmark;
|
|
|
|
[MemoryDiagnoser]
|
|
[Orderer(SummaryOrderPolicy.FastestToSlowest)]
|
|
[RankColumn]
|
|
public class ParserBenchmarks
|
|
{
|
|
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 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");
|
|
}
|
|
|
|
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()
|
|
{
|
|
foreach (var name in _names)
|
|
{
|
|
Normalize(name);
|
|
}
|
|
}
|
|
|
|
|
|
[Benchmark]
|
|
public void TestIsEpub()
|
|
{
|
|
foreach (var name in _names)
|
|
{
|
|
if ((name).ToLower() == ".epub")
|
|
{
|
|
/* No Operation */
|
|
}
|
|
}
|
|
}
|
|
|
|
[Benchmark]
|
|
public void TestIsEpub_New()
|
|
{
|
|
foreach (var name in _names)
|
|
{
|
|
|
|
if (Path.GetExtension(name).Equals(".epub", StringComparison.InvariantCultureIgnoreCase))
|
|
{
|
|
/* No Operation */
|
|
}
|
|
}
|
|
}
|
|
|
|
[Benchmark]
|
|
public void Test_CharacterReplace()
|
|
{
|
|
foreach (var name in _names)
|
|
{
|
|
var d = name.Contains('a');
|
|
}
|
|
}
|
|
|
|
[Benchmark]
|
|
public void Test_StringReplace()
|
|
{
|
|
foreach (var name in _names)
|
|
{
|
|
|
|
var d = name.Contains("a");
|
|
}
|
|
}
|
|
|
|
|
|
}
|