Kavita/API.Benchmark/TestBenchmark.cs
Joseph Milazzo 591b574706
Unit Tests & New Natural Sort (#941)
* Added a lot of tests

* More tests! Added a Parser.NormalizePath to normalize all paths within Kavita.

* Fixed a bug where MarkChaptersAsUnread implementation wasn't consistent between different files and lead to extra row generation for no reason.

* Added more unit tests

* Found a better implementation for Natural Sorting. Added tests and validate it works. Next commit will swap out natural Sort for new Extension.

* Replaced NaturalSortComparer with OrderByNatural.

* Drastically simplified and sped up FindFirstEntry for finding cover images in archives

* Initial fix for a epub bug where metadata defines key as absolute path but document uses a relative path. We now have a hack to correct for the epub.
2022-01-15 07:39:34 -08:00

68 lines
1.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using API.Comparators;
using API.DTOs;
using API.Extensions;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Order;
namespace API.Benchmark
{
/// <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)
{
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()
{
Number = random.Next(10) > 5 ? 1 : 0,
Chapters = GenerateChapters()
});
}
return list;
}
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;
}
private static void SortSpecialChapters(IEnumerable<VolumeDto> volumes)
{
foreach (var v in volumes.Where(vDto => vDto.Number == 0))
{
v.Chapters = v.Chapters.OrderByNatural(x => x.Range).ToList();
}
}
[Benchmark]
public void TestSortSpecialChapters()
{
var volumes = GenerateVolumes(10);
SortSpecialChapters(volumes);
}
}
}