internal/providers/benchmark_test.go

213 lines · 5491 bytes

  1package providers_test
  2
  3import (
  4	"net/http"
  5	"os"
  6	"sort"
  7	"testing"
  8	"time"
  9
 10	"github.com/wraient/curd/internal/curdhost"
 11	_ "github.com/wraient/curd/internal/loadproviders"
 12	"github.com/wraient/curd/internal/providers"
 13	"github.com/wraient/curd/internal/providers/anipub"
 14	"github.com/wraient/curd/internal/providers/anineko"
 15	"github.com/wraient/curd/internal/providers/senshi"
 16)
 17
 18const liveBenchQuery = "frieren"
 19
 20var liveBenchCases = []struct {
 21	name      string
 22	provider  string
 23	showID    string
 24	epNo      int
 25	mode      string
 26	searchFn  func(string, string) ([]providers.SelectionOption, error)
 27	episodes  func(string, string) ([]string, error)
 28	streams   func(string, providers.PlaybackConfig, int) ([]string, map[string]providers.StreamPlaybackHint, error)
 29}{
 30	{
 31		name:     "senshi",
 32		provider: "senshi",
 33		showID:   "52991",
 34		epNo:     1,
 35		mode:     "sub",
 36		searchFn: func(query, mode string) ([]providers.SelectionOption, error) {
 37			return senshiSearch(query, mode)
 38		},
 39		episodes: senshiEpisodes,
 40		streams:  senshiStreams,
 41	},
 42	{
 43		name:     "anipub",
 44		provider: "anipub",
 45		showID:   "2454",
 46		epNo:     1,
 47		mode:     "sub",
 48		searchFn: anipubSearch,
 49		episodes: anipubEpisodes,
 50		streams:  anipubStreams,
 51	},
 52	{
 53		name:     "anineko",
 54		provider: "anineko",
 55		showID:   "",
 56		epNo:     1,
 57		mode:     "sub",
 58		searchFn: aninekoSearch,
 59		episodes: nil,
 60		streams:  nil,
 61	},
 62}
 63
 64func senshiSearch(query, mode string) ([]providers.SelectionOption, error) {
 65	p := &senshi.Provider{}
 66	return p.SearchAnime(query, mode)
 67}
 68
 69func senshiEpisodes(showID, mode string) ([]string, error) {
 70	p := &senshi.Provider{}
 71	return p.EpisodesList(showID, mode)
 72}
 73
 74func senshiStreams(showID string, config providers.PlaybackConfig, epNo int) ([]string, map[string]providers.StreamPlaybackHint, error) {
 75	p := &senshi.Provider{}
 76	return p.GetEpisodeURLForModeWithHints(config, showID, epNo, config.SubOrDub)
 77}
 78
 79func anipubSearch(query, mode string) ([]providers.SelectionOption, error) {
 80	p := &anipub.Provider{}
 81	return p.SearchAnime(query, mode)
 82}
 83
 84func anipubEpisodes(showID, mode string) ([]string, error) {
 85	p := &anipub.Provider{}
 86	return p.EpisodesList(showID, mode)
 87}
 88
 89func anipubStreams(showID string, config providers.PlaybackConfig, epNo int) ([]string, map[string]providers.StreamPlaybackHint, error) {
 90	p := &anipub.Provider{}
 91	return p.GetEpisodeURLForModeWithHints(config, showID, epNo, config.SubOrDub)
 92}
 93
 94func aninekoSearch(query, mode string) ([]providers.SelectionOption, error) {
 95	p := &anineko.Provider{}
 96	return p.SearchAnime(query, mode)
 97}
 98
 99func initLiveBench(tb testing.TB) {
100	tb.Helper()
101	if os.Getenv("CURD_LIVE_BENCH") == "" {
102		tb.Skip("set CURD_LIVE_BENCH=1 to run live benchmarks")
103	}
104	if curdhost.HTTPClient == nil {
105		curdhost.HTTPClient = func() *http.Client { return http.DefaultClient }
106	}
107}
108
109func BenchmarkProviderSearch(b *testing.B) {
110	initLiveBench(b)
111	for _, tc := range liveBenchCases {
112		if tc.searchFn == nil {
113			continue
114		}
115		b.Run(tc.name, func(b *testing.B) {
116			b.ResetTimer()
117			for i := 0; i < b.N; i++ {
118				if _, err := tc.searchFn(liveBenchQuery, tc.mode); err != nil {
119					b.Fatal(err)
120				}
121			}
122		})
123	}
124}
125
126func BenchmarkProviderEpisodeStream(b *testing.B) {
127	initLiveBench(b)
128	for _, tc := range liveBenchCases {
129		if tc.episodes == nil || tc.streams == nil {
130			continue
131		}
132		b.Run(tc.name, func(b *testing.B) {
133			b.ResetTimer()
134			for i := 0; i < b.N; i++ {
135				if _, err := tc.episodes(tc.showID, tc.mode); err != nil {
136					b.Fatal(err)
137				}
138				if _, _, err := tc.streams(tc.showID, providers.PlaybackConfig{SubOrDub: tc.mode}, tc.epNo); err != nil {
139					b.Fatal(err)
140				}
141			}
142		})
143	}
144}
145
146func TestLiveProviderFlowLatency(t *testing.T) {
147	initLiveBench(t)
148
149	type result struct {
150		name   string
151		search time.Duration
152		flow   time.Duration
153	}
154
155	results := make([]result, 0, len(liveBenchCases))
156	for _, tc := range liveBenchCases {
157		if tc.searchFn == nil || tc.episodes == nil || tc.streams == nil {
158			continue
159		}
160
161		searchStart := time.Now()
162		options, err := tc.searchFn(liveBenchQuery, tc.mode)
163		if err != nil {
164			t.Fatalf("%s search: %v", tc.name, err)
165		}
166		searchLatency := time.Since(searchStart)
167
168		showID := tc.showID
169		if showID == "" {
170			for _, option := range options {
171				if option.Key != "" {
172					showID = option.Key
173					break
174				}
175			}
176		}
177		if showID == "" {
178			t.Fatalf("%s: no show id for flow benchmark", tc.name)
179		}
180
181		flowStart := time.Now()
182		if _, err := tc.episodes(showID, tc.mode); err != nil {
183			t.Fatalf("%s episodes: %v", tc.name, err)
184		}
185		links, _, err := tc.streams(showID, providers.PlaybackConfig{SubOrDub: tc.mode}, tc.epNo)
186		if err != nil {
187			t.Fatalf("%s streams: %v", tc.name, err)
188		}
189		if len(links) == 0 {
190			t.Fatalf("%s: no stream links", tc.name)
191		}
192		flowLatency := time.Since(flowStart)
193
194		results = append(results, result{name: tc.name, search: searchLatency, flow: searchLatency + flowLatency})
195		t.Logf("%s search=%s full_flow=%s", tc.name, searchLatency, searchLatency+flowLatency)
196	}
197
198	if len(results) < 2 {
199		t.Skip("not enough providers with full flow support")
200	}
201
202	sort.Slice(results, func(i, j int) bool {
203		return results[i].flow < results[j].flow
204	})
205	fastest := results[0].name
206	t.Logf("fastest full flow: %s (%s)", fastest, results[0].flow)
207
208	for _, item := range results[1:] {
209		if item.flow > results[0].flow*3 {
210			t.Logf("warning: %s full flow is much slower than %s (%s vs %s)", item.name, fastest, item.flow, results[0].flow)
211		}
212	}
213}