Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

server and cache benchmarks #362

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ build:
test:
@go test ./pkg/...

.PHONY: benchmark
benchmark:
@go test ./pkg/... -bench=.

.PHONY: cover
cover:
@build/coverage.sh
Expand Down
51 changes: 51 additions & 0 deletions pkg/cache/v2/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,54 @@ func TestPassthroughResponseGetDiscoveryResponse(t *testing.T) {
assert.Equal(t, r.Name, resourceName)
assert.Equal(t, discoveryResponse, dr)
}

// BENCHMARKS =====================================================================================================

func BenchmarkResponseGetDiscoveryResponse(b *testing.B) {
routes := []types.Resource{&route.RouteConfiguration{Name: resourceName}}
resp := cache.RawResponse{
Request: &discovery.DiscoveryRequest{TypeUrl: resource.RouteType},
Version: "v",
Resources: routes,
}

discoveryResponse, err := resp.GetDiscoveryResponse()
assert.Nil(b, err)
assert.Equal(b, discoveryResponse.VersionInfo, resp.Version)
assert.Equal(b, len(discoveryResponse.Resources), 1)

cachedResponse, err := resp.GetDiscoveryResponse()
assert.Nil(b, err)
assert.Same(b, discoveryResponse, cachedResponse)

r := &route.RouteConfiguration{}
err = ptypes.UnmarshalAny(discoveryResponse.Resources[0], r)
assert.Nil(b, err)
assert.Equal(b, r.Name, resourceName)
}

func BenchmarkPassthroughResponseGetDiscoveryResponse(b *testing.B) {
routes := []types.Resource{&route.RouteConfiguration{Name: resourceName}}
rsrc, err := ptypes.MarshalAny(routes[0])
assert.Nil(b, err)
dr := &discovery.DiscoveryResponse{
TypeUrl: resource.RouteType,
Resources: []*any.Any{rsrc},
VersionInfo: "v",
}
resp := cache.PassthroughResponse{
Request: &discovery.DiscoveryRequest{TypeUrl: resource.RouteType},
DiscoveryResponse: dr,
}

discoveryResponse, err := resp.GetDiscoveryResponse()
assert.Nil(b, err)
assert.Equal(b, discoveryResponse.VersionInfo, resp.DiscoveryResponse.VersionInfo)
assert.Equal(b, len(discoveryResponse.Resources), 1)

r := &route.RouteConfiguration{}
err = ptypes.UnmarshalAny(discoveryResponse.Resources[0], r)
assert.Nil(b, err)
assert.Equal(b, r.Name, resourceName)
assert.Equal(b, discoveryResponse, dr)
}
116 changes: 116 additions & 0 deletions pkg/cache/v2/simple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ var (

type logger struct {
t *testing.T
b *testing.B
}

func (log logger) Debugf(format string, args ...interface{}) { log.t.Logf(format, args...) }
Expand Down Expand Up @@ -275,3 +276,118 @@ func TestSnapshotClear(t *testing.T) {
t.Errorf("keys should be empty")
}
}

// BENCHMARKS =====================================================================================================

func BenchmarkSnapshotCache(b *testing.B) {
c := cache.NewSnapshotCache(true, group{}, nil)

if _, err := c.GetSnapshot(key); err == nil {
b.Errorf("unexpected snapshot found for key %q", key)
}

if err := c.SetSnapshot(key, snapshot); err != nil {
b.Fatal(err)
}

snap, err := c.GetSnapshot(key)
if err != nil {
b.Fatal(err)
}
if !reflect.DeepEqual(snap, snapshot) {
b.Errorf("expect snapshot: %v, got: %v", snapshot, snap)
}

// try to get endpoints with incorrect list of names
// should not receive response
value, _ := c.CreateWatch(&discovery.DiscoveryRequest{TypeUrl: rsrc.EndpointType, ResourceNames: []string{"none"}})
select {
case out := <-value:
b.Errorf("watch for endpoints and mismatched names => got %v, want none", out)
case <-time.After(time.Second / 4):
}

for _, typ := range testTypes {
b.Run(typ, func(b *testing.B) {
value, _ := c.CreateWatch(&discovery.DiscoveryRequest{TypeUrl: typ, ResourceNames: names[typ]})
select {
case out := <-value:
if gotVersion, _ := out.GetVersion(); gotVersion != version {
b.Errorf("got version %q, want %q", gotVersion, version)
}
if !reflect.DeepEqual(cache.IndexResourcesByName(out.(*cache.RawResponse).Resources), snapshot.GetResources(typ)) {
b.Errorf("get resources %v, want %v", out.(*cache.RawResponse).Resources, snapshot.GetResources(typ))
}
case <-time.After(time.Second):
b.Fatal("failed to receive snapshot response")
}
})
}
}

func BenchmarkSnapshotCacheFetch(b *testing.B) {
c := cache.NewSnapshotCache(true, group{}, nil)
if err := c.SetSnapshot(key, snapshot); err != nil {
b.Fatal(err)
}

for _, typ := range testTypes {
b.Run(typ, func(b *testing.B) {
resp, err := c.Fetch(context.Background(), &discovery.DiscoveryRequest{TypeUrl: typ, ResourceNames: names[typ]})
if err != nil || resp == nil {
b.Fatal("unexpected error or null response")
}
if gotVersion, _ := resp.GetVersion(); gotVersion != version {
b.Errorf("got version %q, want %q", gotVersion, version)
}
})
}

// no response for missing snapshot
if resp, err := c.Fetch(context.Background(),
&discovery.DiscoveryRequest{TypeUrl: rsrc.ClusterType, Node: &core.Node{Id: "oof"}}); resp != nil || err == nil {
b.Errorf("missing snapshot: response is not nil %v", resp)
}

// no response for latest version
if resp, err := c.Fetch(context.Background(),
&discovery.DiscoveryRequest{TypeUrl: rsrc.ClusterType, VersionInfo: version}); resp != nil || err == nil {
b.Errorf("latest version: response is not nil %v", resp)
}
}

func BenchmarkSnapshotClear(b *testing.B) {
c := cache.NewSnapshotCache(true, group{}, nil)
if err := c.SetSnapshot(key, snapshot); err != nil {
b.Fatal(err)
}
c.ClearSnapshot(key)
if empty := c.GetStatusInfo(key); empty != nil {
b.Errorf("cache should be cleared")
}
if keys := c.GetStatusKeys(); len(keys) != 0 {
b.Errorf("keys should be empty")
}
}

func BenchmarkSnapshotCacheWatchCancel(b *testing.B) {
c := cache.NewSnapshotCache(true, group{}, nil)
for _, typ := range testTypes {
_, cancel := c.CreateWatch(&discovery.DiscoveryRequest{TypeUrl: typ, ResourceNames: names[typ]})
cancel()
}
// should be status info for the node
if keys := c.GetStatusKeys(); len(keys) == 0 {
b.Error("got 0, want status info for the node")
}

for _, typ := range testTypes {
if count := c.GetStatusInfo(key).GetNumWatches(); count > 0 {
b.Errorf("watches should be released for %s", typ)
}
}

if empty := c.GetStatusInfo("missing"); empty != nil {
b.Errorf("should not return a status for unknown key: got %#v", empty)
}
}
51 changes: 51 additions & 0 deletions pkg/cache/v3/cache_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

116 changes: 116 additions & 0 deletions pkg/cache/v3/simple_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading