1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
package redirects
import (
"context"
"io/ioutil"
"net/url"
"path"
"strings"
"testing"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/gitlab-pages/internal/testhelpers"
)
func generateRedirectsFile(dirPath string, count int) error {
content := "/start.html /redirect.html 301\n"
if placeholdersEnabled() {
content += strings.Repeat("/foo/*/bar /foo/:splat/qux 200\n", count/2)
content += strings.Repeat("/foo/:placeholder /qux/:placeholder 200\n", count/2)
} else {
content += strings.Repeat("/goto.html /target.html 301\n", count)
}
content += "/entrance.html /exit.html 301\n"
return ioutil.WriteFile(path.Join(dirPath, ConfigFile), []byte(content), 0600)
}
func benchmarkRedirectsRewrite(b *testing.B, redirectsCount int) {
ctx := context.Background()
root, tmpDir, cleanup := testhelpers.TmpDir(nil, "ParseRedirects_benchmarks")
defer cleanup()
err := generateRedirectsFile(tmpDir, redirectsCount)
require.NoError(b, err)
url, err := url.Parse("/entrance.html")
require.NoError(b, err)
redirects := ParseRedirects(ctx, root)
require.NoError(b, redirects.error)
for i := 0; i < b.N; i++ {
_, _, err := redirects.Rewrite(url)
require.NoError(b, err)
}
}
func BenchmarkRedirectsRewrite_withoutPlaceholders(b *testing.B) {
b.Run("10 redirects", func(b *testing.B) { benchmarkRedirectsRewrite(b, 10) })
b.Run("100 redirects", func(b *testing.B) { benchmarkRedirectsRewrite(b, 100) })
b.Run("1000 redirects", func(b *testing.B) { benchmarkRedirectsRewrite(b, 1000) })
}
func BenchmarkRedirectsRewrite_PlaceholdersEnabled(b *testing.B) {
enablePlaceholders(b)
b.Run("10 redirects", func(b *testing.B) { benchmarkRedirectsRewrite(b, 10) })
b.Run("100 redirects", func(b *testing.B) { benchmarkRedirectsRewrite(b, 100) })
b.Run("1000 redirects", func(b *testing.B) { benchmarkRedirectsRewrite(b, 1000) })
}
func benchmarkRedirectsParseRedirects(b *testing.B, redirectsCount int) {
ctx := context.Background()
root, tmpDir, cleanup := testhelpers.TmpDir(nil, "ParseRedirects_benchmarks")
defer cleanup()
err := generateRedirectsFile(tmpDir, redirectsCount)
require.NoError(b, err)
for i := 0; i < b.N; i++ {
redirects := ParseRedirects(ctx, root)
require.NoError(b, redirects.error)
}
}
func BenchmarkRedirectsParseRedirects(b *testing.B) {
b.Run("10 redirects", func(b *testing.B) { benchmarkRedirectsParseRedirects(b, 10) })
b.Run("100 redirects", func(b *testing.B) { benchmarkRedirectsParseRedirects(b, 100) })
b.Run("1000 redirects", func(b *testing.B) { benchmarkRedirectsParseRedirects(b, 1000) })
}
|