|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// // Unless required by applicable law or agreed to in writing, |
| 11 | +// software distributed under the License is distributed on an |
| 12 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 13 | +// KIND, either express or implied. See the License for the |
| 14 | +// specific language governing permissions and limitations |
| 15 | +// under the License. |
| 16 | + |
| 17 | +package load |
| 18 | + |
| 19 | +import ( |
| 20 | + "bytes" |
| 21 | + "context" |
| 22 | + "fmt" |
| 23 | + "net/http" |
| 24 | + "time" |
| 25 | + |
| 26 | + "github.com/api7/gopkg/pkg/log" |
| 27 | + . "github.com/onsi/ginkgo/v2" |
| 28 | + . "github.com/onsi/gomega" |
| 29 | + "go.uber.org/zap" |
| 30 | + "k8s.io/apimachinery/pkg/util/wait" |
| 31 | + |
| 32 | + "github.com/apache/apisix-ingress-controller/test/e2e/framework" |
| 33 | + "github.com/apache/apisix-ingress-controller/test/e2e/scaffold" |
| 34 | +) |
| 35 | + |
| 36 | +const gatewayProxyYaml = ` |
| 37 | +apiVersion: apisix.apache.org/v1alpha1 |
| 38 | +kind: GatewayProxy |
| 39 | +metadata: |
| 40 | + name: apisix-proxy-config |
| 41 | +spec: |
| 42 | + provider: |
| 43 | + type: ControlPlane |
| 44 | + controlPlane: |
| 45 | + service: |
| 46 | + name: %s |
| 47 | + port: 9180 |
| 48 | + auth: |
| 49 | + type: AdminKey |
| 50 | + adminKey: |
| 51 | + value: "%s" |
| 52 | +` |
| 53 | + |
| 54 | +const ingressClassYaml = ` |
| 55 | +apiVersion: networking.k8s.io/v1 |
| 56 | +kind: IngressClass |
| 57 | +metadata: |
| 58 | + name: apisix |
| 59 | +spec: |
| 60 | + controller: "apisix.apache.org/apisix-ingress-controller" |
| 61 | + parameters: |
| 62 | + apiGroup: "apisix.apache.org" |
| 63 | + kind: "GatewayProxy" |
| 64 | + name: "apisix-proxy-config" |
| 65 | + namespace: %s |
| 66 | + scope: "Namespace" |
| 67 | +` |
| 68 | + |
| 69 | +var _ = Describe("Load Test", func() { |
| 70 | + var ( |
| 71 | + s = scaffold.NewScaffold(&scaffold.Options{ |
| 72 | + ControllerName: "apisix.apache.org/apisix-ingress-controller", |
| 73 | + }) |
| 74 | + controlAPIClient scaffold.ControlAPIClient |
| 75 | + err error |
| 76 | + ) |
| 77 | + |
| 78 | + BeforeEach(func() { |
| 79 | + By("create GatewayProxy") |
| 80 | + gatewayProxy := fmt.Sprintf(gatewayProxyYaml, framework.ProviderType, s.AdminKey()) |
| 81 | + err = s.CreateResourceFromStringWithNamespace(gatewayProxy, s.Namespace()) |
| 82 | + Expect(err).NotTo(HaveOccurred(), "creating GatewayProxy") |
| 83 | + time.Sleep(5 * time.Second) |
| 84 | + |
| 85 | + By("create IngressClass") |
| 86 | + err = s.CreateResourceFromStringWithNamespace(fmt.Sprintf(ingressClassYaml, s.Namespace()), "") |
| 87 | + Expect(err).NotTo(HaveOccurred(), "creating IngressClass") |
| 88 | + time.Sleep(5 * time.Second) |
| 89 | + |
| 90 | + By("port-forward to control api service") |
| 91 | + controlAPIClient, err = s.ControlAPIClient() |
| 92 | + Expect(err).NotTo(HaveOccurred(), "create control api client") |
| 93 | + }) |
| 94 | + |
| 95 | + Context("Load Test 2000 ApisixRoute", func() { |
| 96 | + It("test 2000 ApisixRoute", func() { |
| 97 | + const total = 2000 |
| 98 | + |
| 99 | + const apisixRouteSpec = ` |
| 100 | +apiVersion: apisix.apache.org/v2 |
| 101 | +kind: ApisixRoute |
| 102 | +metadata: |
| 103 | + name: %s |
| 104 | +spec: |
| 105 | + ingressClassName: apisix |
| 106 | + http: |
| 107 | + - name: rule0 |
| 108 | + match: |
| 109 | + paths: |
| 110 | + - /get |
| 111 | + exprs: |
| 112 | + - subject: |
| 113 | + scope: Header |
| 114 | + name: X-Route-Name |
| 115 | + op: Equal |
| 116 | + value: %s |
| 117 | + backends: |
| 118 | + - serviceName: httpbin-service-e2e-test |
| 119 | + servicePort: 80 |
| 120 | +` |
| 121 | + |
| 122 | + By(fmt.Sprintf("prepare %d ApisixRoutes", total)) |
| 123 | + var text = bytes.NewBuffer(nil) |
| 124 | + for i := range total { |
| 125 | + name := getRouteName(i) |
| 126 | + _, err := fmt.Fprintf(text, apisixRouteSpec, name, name) |
| 127 | + Expect(err).NotTo(HaveOccurred()) |
| 128 | + text.WriteString("\n---\n") |
| 129 | + } |
| 130 | + err := s.CreateResourceFromString(text.String()) |
| 131 | + Expect(err).NotTo(HaveOccurred(), "creating ApisixRoutes") |
| 132 | + log.Infof("created %d ApisixRoutes", total) |
| 133 | + |
| 134 | + var ( |
| 135 | + results []TestResult |
| 136 | + now = time.Now() |
| 137 | + ) |
| 138 | + log.Infof("start calc time, now: %v", now) |
| 139 | + By("Test the time required for applying a large number of ApisixRoutes to take effect") |
| 140 | + var times int |
| 141 | + err = wait.PollUntilContextTimeout(context.Background(), 100*time.Millisecond, 10*time.Minute, true, func(ctx context.Context) (done bool, err error) { |
| 142 | + times++ |
| 143 | + results, _, err := controlAPIClient.ListServices() |
| 144 | + if err != nil { |
| 145 | + log.Errorw("failed to ListServices", zap.Error(err)) |
| 146 | + return false, nil |
| 147 | + } |
| 148 | + if len(results) != total { |
| 149 | + log.Debugw("number of effective services", zap.Int("number", len(results)), zap.Int("times", times)) |
| 150 | + return false, nil |
| 151 | + } |
| 152 | + return len(results) == total, nil |
| 153 | + }) |
| 154 | + Expect(err).ShouldNot(HaveOccurred()) |
| 155 | + costTime := time.Since(now) |
| 156 | + log.Infof("end calc time, now: %v, costTime: %v", time.Now(), costTime) |
| 157 | + results = append(results, TestResult{ |
| 158 | + CaseName: fmt.Sprintf("Apply %d ApisixRoutes", total), |
| 159 | + CostTime: costTime, |
| 160 | + }) |
| 161 | + |
| 162 | + By("Test the time required for an ApisixRoute update to take effect") |
| 163 | + var apisixRouteSpec0 = ` |
| 164 | +apiVersion: apisix.apache.org/v2 |
| 165 | +kind: ApisixRoute |
| 166 | +metadata: |
| 167 | + name: %s |
| 168 | +spec: |
| 169 | + ingressClassName: apisix |
| 170 | + http: |
| 171 | + - name: rule0 |
| 172 | + match: |
| 173 | + paths: |
| 174 | + - /headers |
| 175 | + exprs: |
| 176 | + - subject: |
| 177 | + scope: Header |
| 178 | + name: X-Route-Name |
| 179 | + op: Equal |
| 180 | + value: %s |
| 181 | + backends: |
| 182 | + - serviceName: httpbin-service-e2e-test |
| 183 | + servicePort: 80 |
| 184 | +` |
| 185 | + name := getRouteName(10) |
| 186 | + err = s.CreateResourceFromString(fmt.Sprintf(apisixRouteSpec0, name, name)) |
| 187 | + Expect(err).NotTo(HaveOccurred()) |
| 188 | + now = time.Now() |
| 189 | + Eventually(func() int { |
| 190 | + return s.NewAPISIXClient().GET("/headers").WithHeader("X-Route-Name", name).Expect().Raw().StatusCode |
| 191 | + }).WithTimeout(time.Minute).ProbeEvery(100 * time.Millisecond).Should(Equal(http.StatusOK)) |
| 192 | + results = append(results, TestResult{ |
| 193 | + CaseName: fmt.Sprintf("Update a single ApisixRoute base on %d ApisixRoutes", total), |
| 194 | + CostTime: time.Since(now), |
| 195 | + }) |
| 196 | + |
| 197 | + PrintResults(results) |
| 198 | + }) |
| 199 | + }) |
| 200 | +}) |
| 201 | + |
| 202 | +func getRouteName(i int) string { |
| 203 | + return fmt.Sprintf("test-route-%04d", i) |
| 204 | +} |
| 205 | + |
| 206 | +type TestResult struct { |
| 207 | + CaseName string |
| 208 | + CostTime time.Duration |
| 209 | +} |
| 210 | + |
| 211 | +func (tr TestResult) String() string { |
| 212 | + return fmt.Sprintf("%s takes effect for %s", tr.CaseName, tr.CostTime) |
| 213 | +} |
| 214 | + |
| 215 | +func PrintResults(results []TestResult) { |
| 216 | + fmt.Printf("\n======================TEST RESULT ProviderSyncPeriod %s===============================\n", framework.ProviderSyncPeriod) |
| 217 | + fmt.Printf("%-70s", "Test Case") |
| 218 | + fmt.Printf("%-70s\n", "Time Required") |
| 219 | + fmt.Printf("%-70s\n", "--------------------------------------------------------------------------------------") |
| 220 | + for _, result := range results { |
| 221 | + fmt.Printf("%-70s", result.CaseName) |
| 222 | + fmt.Printf("%-70s\n", result.CostTime) |
| 223 | + } |
| 224 | + fmt.Println("======================================================================================") |
| 225 | + fmt.Println() |
| 226 | +} |
0 commit comments