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

[PPANTT-73] feat: Performance tests #9

Merged
merged 8 commits into from
Oct 2, 2024
Merged
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
113 changes: 0 additions & 113 deletions .devops/performance-test-pipelines.yaml

This file was deleted.

59 changes: 59 additions & 0 deletions .devops/performance-test-pipelines.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# azure-pipelines.yml
trigger: none

parameters:
- name: "ENVIRONMENT"
displayName: "Environment"
type: string
values:
- "dev"
- "uat"
default: "uat"
- name: "TEST_TYPE"
displayName: "Test type"
type: string
values:
- "load"
- "spike"
- "stress"
- "constant"
default: "constant"
- name: "NOTICE_TYPE"
displayName: "Notice type"
type: string
values:
- "single_opt"
- "single_and_many_opt"
- "single_and_multy_opt"
- "single_and_co_opt"
- "all"
default: "all"
- name: "DB_NAME"
displayName: "DB name"
type: string
values:
- pagopa_payment_options_servicek6
variables:
${{ if eq(parameters['ENVIRONMENT'], 'dev') }}:
poolImage: 'pagopa-dev-loadtest-linux'
API_SUBSCRIPTION_KEY: "$(DEV_API_SUBSCRIPTION_KEY)"
${{ if eq(parameters['ENVIRONMENT'], 'uat') }}:
poolImage: 'pagopa-uat-loadtest-linux'
API_SUBSCRIPTION_KEY: "$(UAT_API_SUBSCRIPTION_KEY)"

pool:
name: $(poolImage)

steps:
- script: |
cd ./performance-test/src
docker pull grafana/k6
displayName: Pull k6 image
- script: |
cd ./performance-test
sh ./run_performance_test.sh ${{ parameters.ENVIRONMENT }} ${{ parameters.TEST_TYPE }} payment_options_service_test ${{ parameters.DB_NAME}} $OCP_APIM_SUBSCRIPTION_KEY
displayName: Run k6 payment_options_service_test with notice type ${{ parameters.NOTICE_TYPE}} on ${{ parameters.ENVIRONMENT }}
env:
OCP_APIM_SUBSCRIPTION_KEY: ${{ variables.API_SUBSCRIPTION_KEY }}
NOTICE_TYPE: ${{ parameters.NOTICE_TYPE }}

1 change: 0 additions & 1 deletion perfomance-test/README.md

This file was deleted.

14 changes: 14 additions & 0 deletions performance-test/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# K6 tests

This is a set of [k6](https://k6.io) tests.

To invoke k6 tests use `run_performance_test.sh` script.


## How to run 🚀

Use this command to launch the tests:

``` shell
sh run_performance_test.sh <local|dev|uat> <load|stress|spike|...> <script-filename> <DB-name> <apim_subscription_key>
```
27 changes: 27 additions & 0 deletions performance-test/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
version: '3.3'
services:
k6:
image: grafana/k6
container_name: k6
volumes:
- '${PWD}/src:/scripts'
environment:
- OCP_APIM_SUBSCRIPTION_KEY=${ocp_apim_subscription_key}
- VARS=${env}.environment.json
- TEST_TYPE=/scripts/test-types/${type}.json
- K6_OUT=influxdb=http://nginx:8086/${db_name}
command: run /scripts/${script}.js
depends_on:
- nginx

nginx:
image: nginx
container_name: nginx
volumes:
- '${PWD}/nginx/nginx.conf:/etc/nginx/nginx.conf'
environment:
- ENVIRONMENT=${env}
ports:
- "8086:8086"
- "80:80"

16 changes: 16 additions & 0 deletions performance-test/nginx/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
events {
worker_connections 1024;
}

http {
server {
listen 8086;
location / {
proxy_pass https://api.uat.platform.pagopa.it/shared/influxdb/v1/;
proxy_http_version 1.1;
proxy_set_header Host api.uat.platform.pagopa.it;
proxy_pass_request_headers on;
}
}
}

35 changes: 35 additions & 0 deletions performance-test/run_performance_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# sh run_performance_test.sh <local|dev|uat|prod> <load|stress|spike|soak|...> <script-name> <db-name> <apim_subscription_key>

ENVIRONMENT=$1
TYPE=$2
SCRIPT=$3
DB_NAME=$4
OCP_APIM_SUBSCRIPTION_KEY=$5

if [ -z "$ENVIRONMENT" ]
then
echo "No env specified: sh run_performance_test.sh <local|dev|uat|prod> <load|stress|spike|soak|...> <script-name> <db-name> <apim_subscription_key>"
exit 1
fi

if [ -z "$TYPE" ]
then
echo "No test type specified: sh run_performance_test.sh <local|dev|uat|prod> <load|stress|spike|soak|...> <script-name> <db-name> <apim_subscription_key>"
exit 1
fi
if [ -z "$SCRIPT" ]
then
echo "No script name specified: sh run_performance_test.sh <local|dev|uat|prod> <load|stress|spike|soak|...> <script-name> <db-name> <apim_subscription_key>"
exit 1
fi

export env=${ENVIRONMENT}
export type=${TYPE}
export script=${SCRIPT}
export db_name=${DB_NAME}
export ocp_apim_subscription_key=${OCP_APIM_SUBSCRIPTION_KEY}

stack_name=$(cd .. && basename "$PWD")
docker compose -p "${stack_name}-k6" up -d --remove-orphans --force-recreate --build
docker logs -f k6
docker stop nginx
8 changes: 8 additions & 0 deletions performance-test/src/dev.environment.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"environment": [
{
"env": "dev",
"paymentOptionsServiceURIBasePath": "https://api.dev.platform.pagopa.it/payment-options/service/v1"
}
]
}
8 changes: 8 additions & 0 deletions performance-test/src/local.environment.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"environment": [
{
"env": "local",
"paymentOptionsServiceURIBasePath": "http://localhost:8080"
}
]
}
19 changes: 19 additions & 0 deletions performance-test/src/modules/payment_options_client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import http from 'k6/http';
const subKey = `${__ENV.OCP_APIM_SUBSCRIPTION_KEY}`;

export function getToService(endpoint, params) {
let url = endpoint;
let headers = {
'Ocp-Apim-Subscription-Key': subKey,
"Content-Type": "application/json"
};

const queryParams = params ? Object.entries(params) : [];
if (queryParams && queryParams.length) {
queryParams.forEach((el, index) => {
url = url.concat(index === 0 ? "?" : "&", el[0], "=", el[1]);
});
}

return http.get(url, { headers, responseType: "text", params });
}
8 changes: 8 additions & 0 deletions performance-test/src/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"type": "module",
"dependencies": {
"axios": "^0.27.2"
},
"scripts" : {
}
}
57 changes: 57 additions & 0 deletions performance-test/src/payment_options_service_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { getToService } from "./modules/payment_options_client.js";
import { SharedArray } from 'k6/data';
import { check } from 'k6';

const varsArray = new SharedArray('vars', function () {
return JSON.parse(open(`./${__ENV.VARS}`)).environment;
});
export const ENV_VARS = varsArray[0];

const paymentOptionsServiceURIBasePath = `${ENV_VARS.paymentOptionsServiceURIBasePath}`;
const noticeType = `${__ENV.NOTICE_TYPE}`;

const ORGANIZATIONAL_FISCAL_CODE = "77777777777";

const SINGLE_OPT_NOTICE_NUMBER = "311111111111111111";
const SINGLE_AND_MANY_OPT_NOTICE_NUMBER = "311111111111111112";
const SINGLE_AND_MULTI_OPT_NOTICE_NUMBER = "311111111111111116";
const SINGLE_AND_CO_OPT_NOTICE_NUMBER = "311111111112222225";

const VALID_PSP = "99999000001";

const getSelectedNoticeNumbers = () => {
const noticeTypeAll = noticeType === "undefined" || noticeType === "all";

const selectedNotices = [];
if (noticeType === "single_opt" || noticeTypeAll) {
selectedNotices.push(SINGLE_OPT_NOTICE_NUMBER);
}
if (noticeType === "single_and_many_opt" || noticeTypeAll) {
selectedNotices.push(SINGLE_AND_MANY_OPT_NOTICE_NUMBER);
}
if (noticeType === "single_and_multy_opt" || noticeTypeAll) {
selectedNotices.push(SINGLE_AND_MULTI_OPT_NOTICE_NUMBER);
}
if (noticeType === "single_and_co_opt" || noticeTypeAll) {
selectedNotices.push(SINGLE_AND_CO_OPT_NOTICE_NUMBER);
}
console.log(`Selected the following notices: ${JSON.stringify(selectedNotices)}`);
return selectedNotices;
}

export default function () {
const selectedNotices = getSelectedNoticeNumbers();
for (let i = 0; i < selectedNotices.length; i++) {
const el = selectedNotices[i];
let response = getToService(`${paymentOptionsServiceURIBasePath}/payment-options/organizations/${ORGANIZATIONAL_FISCAL_CODE}/notices/${el}`, { idPsp: VALID_PSP });
console.info(`Payment Options Service getPaymentOptions with notice number ${el} call, Status ${response.status}`);

let responseBody = JSON.parse(response.body);

check(response, {
'Payment Options Service getPaymentOptions status is 200': () => response.status === 200,
'Payment Options Service getPaymentOptions body has list of payment options': () =>
Boolean(responseBody && responseBody.paymentOptions && responseBody.paymentOptions.length)
});
}
}
Loading
Loading