-
Notifications
You must be signed in to change notification settings - Fork 26
/
cache_test.go
75 lines (67 loc) · 1.65 KB
/
cache_test.go
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
package main
import (
"io"
"net/http"
"net/http/httptest"
"strconv"
"testing"
"time"
"github.com/stretchr/testify/assert"
"go.goblog.app/app/pkgs/bodylimit"
cpkg "go.goblog.app/app/pkgs/cache"
)
func Benchmark_cacheItem_cost(b *testing.B) {
ci := &cacheItem{
eTag: "abc",
code: 200,
header: http.Header{
"Content-Type": []string{"text/html"},
},
body: []byte("<html>abcdefghijklmnopqrstuvwxyz</html>"),
}
b.RunParallel(func(p *testing.PB) {
for p.Next() {
ci.cost()
}
})
}
func Test_cacheItem_cost(t *testing.T) {
ci := &cacheItem{
header: http.Header{
"Content-Type": []string{"text/html"},
},
body: []byte("<html>abcdefghijklmnopqrstuvwxyz</html>"),
eTag: "abc",
}
bodyLen := int64(len(ci.body))
assert.Equal(t, int64(39), bodyLen)
eTagLen := int64(len(ci.eTag))
assert.Equal(t, int64(3), eTagLen)
assert.Greater(t, ci.cost(), bodyLen+eTagLen)
}
func Benchmark_cacheKey(b *testing.B) {
req := httptest.NewRequest(http.MethodGet, "/abc?abc=def&hij=klm", nil)
b.RunParallel(func(p *testing.PB) {
for p.Next() {
generateCacheKey(req)
}
})
}
func Benchmark_cache_getCache(b *testing.B) {
c := &cache{}
c.c = cpkg.New[string, *cacheItem](time.Minute, 10*bodylimit.MB)
req := httptest.NewRequest(http.MethodGet, "/abc?abc=def&hij=klm", nil)
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = io.WriteString(w, "abcdefghijklmnopqrstuvwxyz")
_, _ = w.Write([]byte("abcdefghijklmnopqrstuvwxyz"))
})
for i := 0; i < b.N; i++ {
c.getOrCreateCache(strconv.Itoa(i), handler, req)
}
}
func Test_cache_purge_nil(t *testing.T) {
var c *cache = nil
c.purge()
c = &cache{c: nil}
c.purge()
}