Add a benchmark for the discovery cache RoundTripper

This benchmark is intended to demonstrate a performance improvement
gained by removing fsyncs. Refer to the below issue for more detail.

https://github.com/kubernetes/kubernetes/issues/110753

Signed-off-by: Nic Cope <nicc@rk0n.org>

Kubernetes-commit: eace46906512b99c23ad9635edc2ea055363a602
This commit is contained in:
Nic Cope 2022-06-28 19:40:58 -07:00 committed by Kubernetes Publisher
parent ec0f33729d
commit 76fccca0ea

View File

@ -25,6 +25,8 @@ import (
"path/filepath" "path/filepath"
"testing" "testing"
"github.com/gregjones/httpcache/diskcache"
"github.com/peterbourgon/diskv"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -40,6 +42,38 @@ func (rt *testRoundTripper) RoundTrip(req *http.Request) (*http.Response, error)
return rt.Response, rt.Err return rt.Response, rt.Err
} }
// NOTE(negz): We're adding a benchmark for an external dependency in order to
// prove that one that will be added in a subsequent commit improves write
// performance.
func BenchmarkDiskCache(b *testing.B) {
cacheDir, err := ioutil.TempDir("", "cache-rt")
if err != nil {
b.Fatal(err)
}
defer os.RemoveAll(cacheDir)
d := diskv.New(diskv.Options{
PathPerm: os.FileMode(0750),
FilePerm: os.FileMode(0660),
BasePath: cacheDir,
TempDir: filepath.Join(cacheDir, ".diskv-temp"),
})
k := "localhost:8080/apis/batch/v1.json"
v, err := ioutil.ReadFile("../../testdata/apis/batch/v1.json")
if err != nil {
b.Fatal(err)
}
c := diskcache.NewWithDiskv(d)
for n := 0; n < b.N; n++ {
c.Set(k, v)
c.Get(k)
c.Delete(k)
}
}
func TestCacheRoundTripper(t *testing.T) { func TestCacheRoundTripper(t *testing.T) {
rt := &testRoundTripper{} rt := &testRoundTripper{}
cacheDir, err := ioutil.TempDir("", "cache-rt") cacheDir, err := ioutil.TempDir("", "cache-rt")