From 8097ccb638b98b4db0cbf211d66e9afb5345ff32 Mon Sep 17 00:00:00 2001 From: Jakub Jarosz Date: Fri, 13 Sep 2024 16:54:21 +0100 Subject: [PATCH 1/2] Add benchmark tests for configurator --- internal/configs/configurator_bench_test.go | 237 ++++++++++++++++++++ 1 file changed, 237 insertions(+) create mode 100644 internal/configs/configurator_bench_test.go diff --git a/internal/configs/configurator_bench_test.go b/internal/configs/configurator_bench_test.go new file mode 100644 index 0000000000..86d24ed1cf --- /dev/null +++ b/internal/configs/configurator_bench_test.go @@ -0,0 +1,237 @@ +package configs + +import ( + "testing" + + meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1" + + "github.com/nginxinc/kubernetes-ingress/internal/configs/version1" + "github.com/nginxinc/kubernetes-ingress/internal/configs/version2" + "github.com/nginxinc/kubernetes-ingress/internal/nginx" + conf_v1 "github.com/nginxinc/kubernetes-ingress/pkg/apis/configuration/v1" +) + +func createTestConfiguratorBench() (*Configurator, error) { + templateExecutor, err := version1.NewTemplateExecutor("version1/nginx-plus.tmpl", "version1/nginx-plus.ingress.tmpl") + if err != nil { + return nil, err + } + + templateExecutorV2, err := version2.NewTemplateExecutor("version2/nginx-plus.virtualserver.tmpl", "version2/nginx-plus.transportserver.tmpl") + if err != nil { + return nil, err + } + + manager := nginx.NewFakeManager("/etc/nginx") + cnf := NewConfigurator(ConfiguratorParams{ + NginxManager: manager, + StaticCfgParams: createTestStaticConfigParams(), + Config: NewDefaultConfigParams(false), + TemplateExecutor: templateExecutor, + TemplateExecutorV2: templateExecutorV2, + LatencyCollector: nil, + LabelUpdater: nil, + IsPlus: false, + IsWildcardEnabled: false, + IsPrometheusEnabled: false, + IsLatencyMetricsEnabled: false, + NginxVersion: nginx.NewVersion("nginx version: nginx/1.25.3 (nginx-plus-r31)"), + }) + cnf.isReloadsEnabled = true + return cnf, nil +} + +func BenchmarkAddOrUpdateIngress(b *testing.B) { + cnf, err := createTestConfiguratorBench() + if err != nil { + b.Fatal(err) + } + ingress := createCafeIngressEx() + + b.ResetTimer() + for range b.N { + _, err := cnf.AddOrUpdateIngress(&ingress) + if err != nil { + b.Fatal(err) + } + } +} + +func BenchmarkAddOrUpdateMergeableIngress(b *testing.B) { + cnf, err := createTestConfiguratorBench() + if err != nil { + b.Fatal(err) + } + mergeableIngress := createMergeableCafeIngress() + + b.ResetTimer() + for range b.N { + _, err := cnf.AddOrUpdateMergeableIngress(mergeableIngress) + if err != nil { + b.Fatal(err) + } + } +} + +func BenchUpdateEndpoints(b *testing.B) { + cnf, err := createTestConfiguratorBench() + if err != nil { + b.Fatal(err) + } + ingress := createCafeIngressEx() + ingresses := []*IngressEx{&ingress} + + b.ResetTimer() + for range b.N { + err := cnf.UpdateEndpoints(ingresses) + if err != nil { + b.Fatal(err) + } + } + +} + +func BenchmarkUpdateEndpointsMergeableIngress(b *testing.B) { + cnf, err := createTestConfiguratorBench() + if err != nil { + b.Fatal(err) + } + mergeableIngress := createMergeableCafeIngress() + mergeableIngresses := []*MergeableIngresses{mergeableIngress} + + b.ResetTimer() + for range b.N { + err := cnf.UpdateEndpointsMergeableIngress(mergeableIngresses) + if err != nil { + b.Fatal(err) + } + } +} + +func BenchmarkAddVirtualServerMetricsLabels(b *testing.B) { + cnf, err := createTestConfiguratorBench() + if err != nil { + b.Fatal(err) + } + + cnf.isPlus = true + cnf.labelUpdater = newFakeLabelUpdater() + testLatencyCollector := newMockLatencyCollector() + cnf.latencyCollector = testLatencyCollector + + vsEx := &VirtualServerEx{ + VirtualServer: &conf_v1.VirtualServer{ + ObjectMeta: meta_v1.ObjectMeta{ + Name: "test-vs", + Namespace: "default", + }, + Spec: conf_v1.VirtualServerSpec{ + Host: "example.com", + }, + }, + PodsByIP: map[string]PodInfo{ + "10.0.0.1:80": {Name: "pod-1"}, + "10.0.0.2:80": {Name: "pod-2"}, + }, + } + + upstreams := []version2.Upstream{ + { + Name: "upstream-1", + Servers: []version2.UpstreamServer{ + { + Address: "10.0.0.1:80", + }, + }, + UpstreamLabels: version2.UpstreamLabels{ + Service: "service-1", + ResourceType: "virtualserver", + ResourceName: vsEx.VirtualServer.Name, + ResourceNamespace: vsEx.VirtualServer.Namespace, + }, + }, + { + Name: "upstream-2", + Servers: []version2.UpstreamServer{ + { + Address: "10.0.0.2:80", + }, + }, + UpstreamLabels: version2.UpstreamLabels{ + Service: "service-2", + ResourceType: "virtualserver", + ResourceName: vsEx.VirtualServer.Name, + ResourceNamespace: vsEx.VirtualServer.Namespace, + }, + }, + } + + b.ResetTimer() + for range b.N { + cnf.updateVirtualServerMetricsLabels(vsEx, upstreams) + } +} + +func BenchmarkAddTransportServerMetricsLabels(b *testing.B) { + cnf, err := createTestConfiguratorBench() + if err != nil { + b.Fatal(err) + } + cnf.isPlus = true + cnf.labelUpdater = newFakeLabelUpdater() + + tsEx := &TransportServerEx{ + TransportServer: &conf_v1.TransportServer{ + ObjectMeta: meta_v1.ObjectMeta{ + Name: "test-transportserver", + Namespace: "default", + }, + Spec: conf_v1.TransportServerSpec{ + Listener: conf_v1.TransportServerListener{ + Name: "dns-tcp", + Protocol: "TCP", + }, + }, + }, + PodsByIP: map[string]string{ + "10.0.0.1:80": "pod-1", + "10.0.0.2:80": "pod-2", + }, + } + + streamUpstreams := []version2.StreamUpstream{ + { + Name: "upstream-1", + Servers: []version2.StreamUpstreamServer{ + { + Address: "10.0.0.1:80", + }, + }, + UpstreamLabels: version2.UpstreamLabels{ + Service: "service-1", + ResourceType: "transportserver", + ResourceName: tsEx.TransportServer.Name, + ResourceNamespace: tsEx.TransportServer.Namespace, + }, + }, + { + Name: "upstream-2", + Servers: []version2.StreamUpstreamServer{ + { + Address: "10.0.0.2:80", + }, + }, + UpstreamLabels: version2.UpstreamLabels{ + Service: "service-2", + ResourceType: "transportserver", + ResourceName: tsEx.TransportServer.Name, + ResourceNamespace: tsEx.TransportServer.Namespace, + }, + }, + } + + b.ResetTimer() + for range b.N { + cnf.updateTransportServerMetricsLabels(tsEx, streamUpstreams) + } +} From 49861e90a98fc820024922e2f1fea276e65b8b4d Mon Sep 17 00:00:00 2001 From: Jakub Jarosz Date: Fri, 13 Sep 2024 17:06:24 +0100 Subject: [PATCH 2/2] Add benchmark tests for configurator --- internal/configs/configurator_bench_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/internal/configs/configurator_bench_test.go b/internal/configs/configurator_bench_test.go index 86d24ed1cf..970c569675 100644 --- a/internal/configs/configurator_bench_test.go +++ b/internal/configs/configurator_bench_test.go @@ -88,7 +88,6 @@ func BenchUpdateEndpoints(b *testing.B) { b.Fatal(err) } } - } func BenchmarkUpdateEndpointsMergeableIngress(b *testing.B) {