internal/bench_test.go

93 lines · 2197 bytes

 1package internal
 2
 3import (
 4	"fmt"
 5	"testing"
 6	"time"
 7)
 8
 9func benchmarkAnimeList(size int) AnimeList {
10	list := AnimeList{}
11	for i := 0; i < size; i++ {
12		entry := Entry{
13			Media: Media{
14				ID:    i + 1,
15				MalID: 1000 + i,
16				Title: AnimeTitle{
17					English: fmt.Sprintf("Anime %d", i+1),
18					Romaji:  fmt.Sprintf("Anime %d", i+1),
19				},
20			},
21			Status:    "CURRENT",
22			Progress:  i % 24,
23			Score:     float64(i % 10),
24			UpdatedAt: time.Date(2026, 1, 1, 0, 0, i%60, 0, time.UTC),
25		}
26		switch i % 5 {
27		case 0:
28			list.Watching = append(list.Watching, entry)
29		case 1:
30			entry.Status = "COMPLETED"
31			list.Completed = append(list.Completed, entry)
32		case 2:
33			entry.Status = "PAUSED"
34			list.Paused = append(list.Paused, entry)
35		case 3:
36			entry.Status = "PLANNING"
37			list.Planning = append(list.Planning, entry)
38		default:
39			entry.Status = "DROPPED"
40			list.Dropped = append(list.Dropped, entry)
41		}
42	}
43	return list
44}
45
46func BenchmarkBuildDualRemoteSyncPlan(b *testing.B) {
47	aniList := benchmarkAnimeList(1000)
48	myAnimeList := benchmarkAnimeList(1000)
49	for i := range myAnimeList.Watching {
50		myAnimeList.Watching[i].Progress += 1
51		myAnimeList.Watching[i].UpdatedAt = myAnimeList.Watching[i].UpdatedAt.Add(time.Hour)
52	}
53
54	b.ReportAllocs()
55	b.ResetTimer()
56	for i := 0; i < b.N; i++ {
57		_ = buildDualRemoteSyncPlan(aniList, myAnimeList)
58	}
59}
60
61func BenchmarkMergeAnimeLists(b *testing.B) {
62	aniList := benchmarkAnimeList(1000)
63	myAnimeList := benchmarkAnimeList(1000)
64
65	b.ReportAllocs()
66	b.ResetTimer()
67	for i := 0; i < b.N; i++ {
68		_ = mergeAnimeLists(aniList, myAnimeList)
69	}
70}
71
72func BenchmarkSortAllanimeSourcesByPriority(b *testing.B) {
73	sources := make([]allanimeSource, 0, 1000)
74	for i := 0; i < 1000; i++ {
75		sources = append(sources, allanimeSource{
76			SourceUrl:  fmt.Sprintf("https://example.com/%d.mp4", i),
77			SourceName: "Yt-mp4",
78			Priority:   float64((i * 37) % 100),
79		})
80	}
81
82	b.ReportAllocs()
83	b.ResetTimer()
84	for i := 0; i < b.N; i++ {
85		_ = sortAllanimeSourcesByPriority(sources)
86	}
87}
88
89func BenchmarkLevenshteinAnimeTitles(b *testing.B) {
90	for i := 0; i < b.N; i++ {
91		_ = levenshtein("Sousou no Frieren Beyond Journey's End", "Frieren Beyond Journey's End")
92	}
93}