diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..d390f86 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,42 @@ +name: Build +on: + push: + branches: + - master + - develop + pull_request: + types: [opened, synchronize, reopened] +jobs: + docker-build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Build the Docker image + run: cd app && docker build . -t ${{ secrets.IMAGE_NAME }}:$(date +%s) + sonarqube: + name: Sonarqube + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Set up JDK 11 + uses: actions/setup-java@v3 + with: + distribution: 'temurin' + java-version: 11 + - name: Cache SonarCloud packages + uses: actions/cache@v3 + with: + path: ~/.sonar/cache + key: ${{ runner.os }}-sonar + restore-keys: ${{ runner.os }}-sonar + - name: Cache Gradle packages + uses: actions/cache@v3 + with: + path: ~/.gradle/caches + key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle') }} + restore-keys: ${{ runner.os }}-gradle + - name: Build and analyze + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} + run: ./gradlew build sonarqube --info \ No newline at end of file diff --git a/.gitignore b/.gitignore index 66fd13c..2bcf72a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,15 +1,55 @@ -# Binaries for programs and plugins -*.exe -*.exe~ -*.dll -*.so -*.dylib +build +.gradle +.vscode +.idea -# Test binary, built with `go test -c` -*.test +*.log +*.jar +*.war +*.nar +*.ear +*.zip +*.tar.gz +*.rar -# Output of the go coverage tool, specifically when used with LiteIDE -*.out +# go +vendor +echo +main -# Dependency directories (remove the comment below to include it) -# vendor/ +# MAC +.DS_Store + +# internal +*internal* + +# k8s template output +# go-echo-api-onepod-template.yaml +# go-echo-api-template.yaml +**/go-echo-api-onepod.yaml +**/go-echo-api.yaml + +# test +2023* + +# GCP +## .sa .readonly-sa +.*sa +## google-github-actions/auth@v1 +gha-creds-*.json + +# terraform +.terraform +*.tfstate +*.tfstate.* +crash.log +crash.*.log +*.tfvars +*.tfvars.json +override.tf +override.tf.json +*_override.tf +*_override.tf.json +.terraformrc +terraform.rc +*.hcl \ No newline at end of file diff --git a/README.md b/README.md index c55eccb..5e891c8 100644 --- a/README.md +++ b/README.md @@ -1 +1,197 @@ -# gcp-golang-performance-test \ No newline at end of file +# Performance testing on GKE using labstack eacho application + +[![Build](https://github.com/DevSecOpsSamples/gcp-golang-performance-test/actions/workflows/build.yml/badge.svg?branch=master)](https://github.com/DevSecOpsSamples/gcp-golang-performance-test/actions/workflows/build.yml) +[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=DevSecOpsSamples_gcp-golang-performance-test&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=DevSecOpsSamples_gcp-golang-performance-test) [![Lines of Code](https://sonarcloud.io/api/project_badges/measure?project=DevSecOpsSamples_gcp-golang-performance-test&metric=ncloc)](https://sonarcloud.io/summary/new_code?id=DevSecOpsSamples_gcp-golang-performance-test) + +Performance testing on GKE using the https://echo.labstack.com application. + + +## Table of Contents + +- [1. Create a GKE cluster](#1-create-a-gke-cluster) +- [2. Deploy two applications for checking the performance per Pod and scaling](#2-deploy-two-applications-for-checking-the-performance-per-pod-and-scaling) + - [2.1. Deploy for performance of one Pod](#21-deploy-for-performance-of-one-pod) + - [2.2. Deploy for Scaling Test](#22-deploy-for-scaling-test) +- [3. Performance Testing](#3-performance-testing) + - [3.1. Install the Taurus](#31-install-the-taurus) + - [3.2. Test for performance of one Pod](#32-test-for-performance-of-one-pod) + - [3.3. Test with auto scaling](#33-test-with-auto-scaling) +- [Cleanup](#6-cleanup) + +--- + +## Prerequisites + +### Installation + +- [Install the gcloud CLI](https://cloud.google.com/sdk/docs/install) +- [Install kubectl and configure cluster access](https://cloud.google.com/kubernetes-engine/docs/how-to/cluster-access-for-kubectl) +- [Installing and Upgrading for the Taurus ](https://gettaurus.org/install/Installation/) + +### Set environment variables + +```bash +COMPUTE_ZONE="us-central1" +# replace with your project +PROJECT_ID="sample-project" +``` + +### Set GCP project + +```bash +gcloud config set project ${PROJECT_ID} +gcloud config set compute/zone ${COMPUTE_ZONE} +``` + +--- + +## 1. Create a GKE cluster + +Create an Autopilot GKE cluster. It may take around 9 minutes. + +```bash +gcloud container clusters create-auto sample-cluster --region=${COMPUTE_ZONE} +gcloud container clusters get-credentials sample-cluster +``` + +## 2. Deploy two applications for checking the performance per Pod and scaling + +Build and push to GCR: + +```bash +cd app +docker build -t go-echo-api . --platform linux/amd64 +docker tag go-echo-api:latest gcr.io/${PROJECT_ID}/go-echo-api:latest + +gcloud auth configure-docker +docker push gcr.io/${PROJECT_ID}/go-echo-api:latest +``` + +```bash +kubectl get namespaces + +kubectl create namespace echo-test +``` + +Two deployments may take around 5 minutes to create a load balancer, including health checking. + +## 2.1. Deploy for performance of one Pod + +To check request per seconds(RPS) WITHOUT scaling, create and deploy K8s Deployment, Service, HorizontalPodAutoscaler, Ingress, and GKE BackendConfig using the [go-echo-api-onepod-template.yaml](app/go-echo-api-onepod-template.yaml) template file: + +```bash +sed -e "s||${PROJECT_ID}|g" go-echo-api-onepod-template.yaml > go-echo-api-onepod.yaml +cat go-echo-api-onepod.yaml + +kubectl get namespaces +kubectl apply -f go-echo-api-onepod.yaml -n echo-test --dry-run=client +``` + +Confirm Pod logs and configuration after deployment: + +```bash +kubectl logs -l app=go-echo-api-onepod -n echo-test + +kubectl describe pods -n echo-test + +kubectl get ingress go-echo-api-onepod-ingress -n echo-test +``` + +## 2.2. Deploy for Scaling Test + +To check request per seconds(RPS) with scaling, create and deploy K8s Deployment, Service, HorizontalPodAutoscaler, Ingress, and GKE BackendConfig using the [go-echo-api-template.yaml](app/go-echo-api-template.yaml) template file: + +```bash +sed -e "s||${PROJECT_ID}|g" go-echo-api-template.yaml > go-echo-api.yaml +cat go-echo-api.yaml + +kubectl apply -f go-echo-api.yaml -n echo-test --dry-run=client +``` + +```bash +kubectl apply -f go-echo-api.yaml -n echo-test +``` + +Confirm Pod logs and configuration after deployment: + +```bash +kubectl logs -l app=go-echo-api -n echo-test + +kubectl describe pods -n echo-test + +kubectl get ingress go-echo-api-ingress -n echo-test +``` + +Confirm that response of `/` API. + +```bash +LB_IP_ADDRESS=$(gcloud compute forwarding-rules list | grep go-echo-api | awk '{ print $2 }') +echo ${LB_IP_ADDRESS} +``` + +```bash +curl http://${LB_IP_ADDRESS}/ +``` + +## 3. Performance Testing + +### 3.1. Install the Taurus + +https://gettaurus.org/install/Installation/ + +```bash +sudo apt-get update -y +sudo apt-get install python3 default-jre-headless python3-tk python3-pip python3-dev libxml2-dev libxslt-dev zlib1g-dev net-tools -y +sudo python3 -m pip install bzt +sudo apt-get install htop -y +``` + +### 3.2. Test for performance of one Pod + +```bash +cd test +# test with 300 threads and connection:close option +bzt echo-bzt-onepod.yaml +``` + +[test/echo-bzt-onepod.yaml](./test/echo-bzt-onepod.yaml) + +```bash +kubectl describe hpa go-echo-api-onepod-hpa -n echo-test + +kubectl get hpa go-echo-api-onepod-hpa -n echo-test -w +``` + +### 3.3. Test with auto scaling + +```bash +cd test +# test with 2000 threads and connection:close option +bzt echo-bzt.yaml +``` + +[test/echo-bzt.yaml](./test/echo-bzt.yaml) + +```bash +kubectl describe hpa go-echo-api-hpa -n echo-test + +kubectl get hpa go-echo-api-hpa -n echo-test -w +``` + +## Cleanup + +```bash +kubectl scale deployment go-echo-api-onepod -n echo-test --replicas=0 +kubectl scale deployment go-echo-api -n echo-test --replicas=0 + +kubectl delete -f app/go-echo-api-onepod.yaml -n echo-test +kubectl delete -f app/go-echo-api.yaml -n echo-test +``` + +## References + +- https://echo.labstack.com + +- [Cloud SDK > Documentation > Reference > gcloud container clusters](https://cloud.google.com/sdk/gcloud/reference/container/clusters) + +- [Google Kubernetes Engine (GKE) > Documentation > Guides > GKE Ingress for HTTP(S) Load Balancing](https://cloud.google.com/kubernetes-engine/docs/concepts/ingress) diff --git a/app/.dockerignore b/app/.dockerignore new file mode 100644 index 0000000..96f0fd5 --- /dev/null +++ b/app/.dockerignore @@ -0,0 +1,3 @@ +vendor +echo +main \ No newline at end of file diff --git a/app/Dockerfile b/app/Dockerfile new file mode 100644 index 0000000..8b198da --- /dev/null +++ b/app/Dockerfile @@ -0,0 +1,20 @@ +FROM golang:1.18 AS builder + +RUN mkdir /app + +COPY ./go.mod /app/ +COPY ./go.sum /app/ +COPY ./main.go /app/ +WORKDIR /app + +RUN go install +RUN go build main.go + +RUN adduser go +RUN chown go ./main + +USER go + +EXPOSE 8000 + +CMD ["./main"] \ No newline at end of file diff --git a/app/Makefile b/app/Makefile new file mode 100644 index 0000000..fe850e3 --- /dev/null +++ b/app/Makefile @@ -0,0 +1,11 @@ +build: + go build ./... + +install: + go install -mod=vendor -v ./... + +test: + go test ./... + +clean: + go clean ./... \ No newline at end of file diff --git a/app/build-multi-arch.sh b/app/build-multi-arch.sh new file mode 100755 index 0000000..d0d97aa --- /dev/null +++ b/app/build-multi-arch.sh @@ -0,0 +1,8 @@ +#!/bin/bash +set -e + +echo "PROJECT_ID: ${PROJECT_ID}" + +docker buildx ls +docker buildx create --name builder --use builder +time docker buildx build -t gcr.io/${PROJECT_ID}/go-echo-api:latest . --platform linux/amd64,linux/arm/v7 --push \ No newline at end of file diff --git a/app/build.sh b/app/build.sh new file mode 100755 index 0000000..6756c17 --- /dev/null +++ b/app/build.sh @@ -0,0 +1,12 @@ +#!/bin/bash +set -e + +echo "PROJECT_ID: ${PROJECT_ID}" + +docker build -t go-echo-api . --platform linux/amd64 +docker tag go-echo-api:latest gcr.io/${PROJECT_ID}/go-echo-api:latest +docker push gcr.io/${PROJECT_ID}/go-echo-api:latest + +# docker run -it -p 8000:8000 go-echo-api:latest + +# docker run -it -p 8000:8000 gcr.io/${PROJECT_ID}/go-echo-api:latest \ No newline at end of file diff --git a/app/go-echo-api-onepod-template.yaml b/app/go-echo-api-onepod-template.yaml new file mode 100644 index 0000000..b913145 --- /dev/null +++ b/app/go-echo-api-onepod-template.yaml @@ -0,0 +1,102 @@ +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: go-echo-api-onepod + namespace: echo-test + annotations: + app: go-echo-api-onepod +spec: + replicas: 1 + selector: + matchLabels: + app: go-echo-api-onepod + template: + metadata: + labels: + app: go-echo-api-onepod + spec: + containers: + - name: go-echo-api-onepod + image: gcr.io//go-echo-api:latest + imagePullPolicy: Always + ports: + - containerPort: 8000 + resources: + requests: + cpu: "1" + memory: "256Mi" +--- +apiVersion: v1 +kind: Service +metadata: + name: go-echo-api-onepod + namespace: echo-test + annotations: + app: go-echo-api-onepod + cloud.google.com/backend-config: '{"default": "go-echo-api-onepod-backend-config"}' +spec: + selector: + app: go-echo-api-onepod + type: ClusterIP + ports: + - port: 30000 + targetPort: 8000 + protocol: TCP +--- +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + name: go-echo-api-onepod-ingress + namespace: echo-test + annotations: + app: go-echo-api-onepod + kubernetes.io/ingress.class: gce +spec: + rules: + - http: + paths: + - path: /* + pathType: ImplementationSpecific + backend: + service: + name: go-echo-api-onepod + port: + number: 30000 +--- +apiVersion: cloud.google.com/v1 +kind: BackendConfig +metadata: + name: go-echo-api-onepod-backend-config + namespace: echo-test +spec: + logging: + enable: false + healthCheck: + checkIntervalSec: 30 + timeoutSec: 30 + healthyThreshold: 1 + unhealthyThreshold: 3 + port: 8000 + type: HTTP + requestPath: / +--- +apiVersion: autoscaling/v2beta2 +kind: HorizontalPodAutoscaler +metadata: + name: go-echo-api-onepod-hpa + namespace: echo-test +spec: + scaleTargetRef: + apiVersion: apps/v1 + kind: Deployment + name: go-echo-api-onepod + minReplicas: 1 + maxReplicas: 1 + metrics: + - type: Resource + resource: + name: cpu + target: + type: Utilization + averageUtilization: 90 \ No newline at end of file diff --git a/app/go-echo-api-template.yaml b/app/go-echo-api-template.yaml new file mode 100644 index 0000000..431f1f5 --- /dev/null +++ b/app/go-echo-api-template.yaml @@ -0,0 +1,102 @@ +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: go-echo-api + namespace: echo-test + annotations: + app: go-echo-api +spec: + replicas: 2 + selector: + matchLabels: + app: go-echo-api + template: + metadata: + labels: + app: go-echo-api + spec: + containers: + - name: go-echo-api + image: gcr.io//go-echo-api:latest + imagePullPolicy: Always + ports: + - containerPort: 8000 + resources: + requests: + cpu: "1" + memory: "256Mi" +--- +apiVersion: v1 +kind: Service +metadata: + name: go-echo-api + namespace: echo-test + annotations: + app: go-echo-api + cloud.google.com/backend-config: '{"default": "go-echo-api-backend-config"}' +spec: + selector: + app: go-echo-api + type: ClusterIP + ports: + - port: 30001 + targetPort: 8000 + protocol: TCP +--- +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + name: go-echo-api-ingress + namespace: echo-test + annotations: + app: go-echo-api + kubernetes.io/ingress.class: gce +spec: + rules: + - http: + paths: + - path: /* + pathType: ImplementationSpecific + backend: + service: + name: go-echo-api + port: + number: 30001 +--- +apiVersion: cloud.google.com/v1 +kind: BackendConfig +metadata: + name: go-echo-api-backend-config + namespace: echo-test +spec: + logging: + enable: false + healthCheck: + checkIntervalSec: 30 + timeoutSec: 30 + healthyThreshold: 1 + unhealthyThreshold: 3 + port: 8000 + type: HTTP + requestPath: / +--- +apiVersion: autoscaling/v2beta2 +kind: HorizontalPodAutoscaler +metadata: + name: go-echo-api-hpa + namespace: echo-test +spec: + scaleTargetRef: + apiVersion: apps/v1 + kind: Deployment + name: 'go-echo-api' + minReplicas: 1 + maxReplicas: 1000 + metrics: + - type: Resource + resource: + name: cpu + target: + type: Utilization + averageUtilization: 90 \ No newline at end of file diff --git a/app/go.mod b/app/go.mod new file mode 100644 index 0000000..dd8a0af --- /dev/null +++ b/app/go.mod @@ -0,0 +1,16 @@ +module echo + +go 1.18 + +require ( + github.com/labstack/echo/v4 v4.10.0 // indirect + github.com/labstack/gommon v0.4.0 // indirect + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.16 // indirect + github.com/valyala/bytebufferpool v1.0.0 // indirect + github.com/valyala/fasttemplate v1.2.2 // indirect + golang.org/x/crypto v0.2.0 // indirect + golang.org/x/net v0.4.0 // indirect + golang.org/x/sys v0.3.0 // indirect + golang.org/x/text v0.5.0 // indirect +) diff --git a/app/go.sum b/app/go.sum new file mode 100644 index 0000000..b462108 --- /dev/null +++ b/app/go.sum @@ -0,0 +1,35 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/labstack/echo/v4 v4.10.0 h1:5CiyngihEO4HXsz3vVsJn7f8xAlWwRr3aY6Ih280ZKA= +github.com/labstack/echo/v4 v4.10.0/go.mod h1:S/T/5fy/GigaXnHTkh0ZGe4LpkkQysvRjFMSUTkDRNQ= +github.com/labstack/gommon v0.4.0 h1:y7cvthEAEbU0yHOf4axH8ZG2NH8knB9iNSoTO8dyIk8= +github.com/labstack/gommon v0.4.0/go.mod h1:uW6kP17uPlLJsD3ijUYn3/M5bAxtlZhMI6m3MFxTMTM= +github.com/mattn/go-colorable v0.1.11/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4= +github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= +github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= +github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ= +github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= +github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= +github.com/valyala/fasttemplate v1.2.1/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ= +github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo= +github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ= +golang.org/x/crypto v0.2.0 h1:BRXPfhNivWL5Yq0BGQ39a2sW6t44aODpfxkWjYdzewE= +golang.org/x/crypto v0.2.0/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4= +golang.org/x/net v0.4.0 h1:Q5QPcMlvfxFTAPV0+07Xz/MpK9NTXu2VDUuy0FeMfaU= +golang.org/x/net v0.4.0/go.mod h1:MBQ8lrhLObU/6UmLb4fmbmk5OcyYmqtbGd/9yIeKjEE= +golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20211103235746-7861aae1554b/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ= +golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/text v0.5.0 h1:OLmvp0KP+FVG99Ct/qFiL/Fhk4zp4QQnZ7b2U+5piUM= +golang.org/x/text v0.5.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/app/main.go b/app/main.go new file mode 100644 index 0000000..4a470c5 --- /dev/null +++ b/app/main.go @@ -0,0 +1,27 @@ +package main + +import ( + "net/http" + + "github.com/labstack/echo/v4" +) + +func main() { + e := echo.New() + e.GET("/", func(c echo.Context) error { + return c.String(http.StatusOK, "OK") + }) + e.GET("/health", func(c echo.Context) error { + return c.String(http.StatusOK, "OK") + }) + e.GET("/:path1/:path2", func(c echo.Context) error { + return c.String(http.StatusOK, "OK") + }) + e.GET("/:path1/:path2/:path3", func(c echo.Context) error { + return c.String(http.StatusOK, "OK") + }) + e.GET("/:path1/:path2/:path3/:path4", func(c echo.Context) error { + return c.String(http.StatusOK, "OK") + }) + e.Logger.Fatal(e.Start(":8000")) +} diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..21af14f --- /dev/null +++ b/build.gradle @@ -0,0 +1,29 @@ +plugins { + id 'base' + id "org.sonarqube" version "3.4.0.2513" +} + +sonarqube { + properties { + property "sonar.projectName", "gcp-golang-performance-test" + property "sonar.projectKey", "DevSecOpsSamples_gcp-golang-performance-test" + property "sonar.organization", "devsecopssamples" + // property "sonar.host.url", "http://127.0.0.1:9000" + property "sonar.host.url", "https://sonarcloud.io" + property "sonar.sourceEncoding", "UTF-8" + property "sonar.sources", "app" + property "sonar.python.version", "3.9" + property "sonar.coverage.jacoco.xmlReportPaths", "build/test-result.xml" + property "sonar.python.coverage.reportPaths", "build/test-coverage.xml" + property "sonar.exclusions", "build/**, gha-creds-*.json, .*sa" + property "sonar.issue.ignore.multicriteria", "e1" + property "sonar.issue.ignore.multicriteria.e1.ruleKey", "terraform:S6404" + property "sonar.issue.ignore.multicriteria.e1.resourceKey", "**/*.tf" + property "sonar.links.ci", "https://github.com/DevSecOpsSamples/gcp-golang-performance-test/actions" + } +} + +clean.doLast { + delete "${rootDir}/app/go-echo-api.yaml" + delete "${rootDir}/app/main" +} \ No newline at end of file diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..943f0cb Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..f398c33 --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip +networkTimeout=10000 +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew new file mode 100755 index 0000000..65dcd68 --- /dev/null +++ b/gradlew @@ -0,0 +1,244 @@ +#!/bin/sh + +# +# Copyright © 2015-2021 the original authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +# This is normally unused +# shellcheck disable=SC2034 +APP_BASE_NAME=${0##*/} +APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC3045 + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC3045 + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + +# Collect all arguments for the java command; +# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of +# shell script including quotes and variable substitutions, so put them in +# double quotes to make sure that they get re-expanded; and +# * put everything else in single quotes, so that it's not re-expanded. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" +fi + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000..6689b85 --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,92 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + +@if "%DEBUG%"=="" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if %ERRORLEVEL% equ 0 goto execute + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if %ERRORLEVEL% equ 0 goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/test/curl.sh b/test/curl.sh new file mode 100755 index 0000000..9a6875f --- /dev/null +++ b/test/curl.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +LB_IP_ADDRESS=$(gcloud compute forwarding-rules list | grep go-echo-api | awk '{ print $2 }') +echo ${LB_IP_ADDRESS} + +a=0 +while [ "$a" -lt 100000 ] +do + curl ${LB_IP_ADDRESS} + sleep 0.1 +done \ No newline at end of file diff --git a/test/echo-bzt-onepod.yaml b/test/echo-bzt-onepod.yaml new file mode 100644 index 0000000..d1bf843 --- /dev/null +++ b/test/echo-bzt-onepod.yaml @@ -0,0 +1,14 @@ +# bzt echo-bzt-onepod.yaml +--- +execution: +- concurrency: 300 + ramp-up: 1s + hold-for: 10m + scenario: echo-api-onepod + +scenarios: + echo-api-onepod: + think-time: 0 + keepalive: false + requests: + - http://{extternal-ip}}/ \ No newline at end of file diff --git a/test/echo-bzt.yaml b/test/echo-bzt.yaml new file mode 100644 index 0000000..4925d68 --- /dev/null +++ b/test/echo-bzt.yaml @@ -0,0 +1,15 @@ +# bzt echo-bzt.yaml +--- +execution: +- concurrency: 2000 +# - concurrency: 100 + ramp-up: 1s + hold-for: 10m + scenario: echo-api + +scenarios: + echo-api: + think-time: 0 + keepalive: false + requests: + - http://{extternal-ip}}/ \ No newline at end of file