diff options
author | danielhanrahan <daniel.hanrahan@est.tech> | 2024-06-10 21:32:12 +0100 |
---|---|---|
committer | danielhanrahan <daniel.hanrahan@est.tech> | 2024-06-14 16:17:13 +0100 |
commit | f66694a076be41d83693423dec818493bcf66715 (patch) | |
tree | 452e1d54ea3a8a9c288c69491a5498118b5f57a0 | |
parent | 35e0df312cbb2fd0a3740805636338713836b5e1 (diff) |
[k6] Refactoring k6 tests (#1)
This commit moves all common request logic into a common folder.
It is needed to avoid duplication before adding JVM warmup phase.
- move registration-related code into common folder
- move passthrough operations into common folder
Issue-ID: CPS-2208
Signed-off-by: danielhanrahan <daniel.hanrahan@est.tech>
Change-Id: Ia9ebf61d21044b43063bde153f9c526e67d607c8
-rw-r--r-- | k6-tests/ncmp/1-create-cmhandles.js | 33 | ||||
-rw-r--r-- | k6-tests/ncmp/10-mixed-load-test.js | 15 | ||||
-rw-r--r-- | k6-tests/ncmp/11-delete-cmhandles.js | 19 | ||||
-rw-r--r-- | k6-tests/ncmp/2-wait-for-cmhandles-to-be-ready.js | 34 | ||||
-rw-r--r-- | k6-tests/ncmp/3-passthrough-read.js | 16 | ||||
-rw-r--r-- | k6-tests/ncmp/4-id-search-no-filter.js | 4 | ||||
-rw-r--r-- | k6-tests/ncmp/5-search-no-filter.js | 4 | ||||
-rw-r--r-- | k6-tests/ncmp/6-id-search-public-property.js | 4 | ||||
-rw-r--r-- | k6-tests/ncmp/7-search-public-property.js | 4 | ||||
-rw-r--r-- | k6-tests/ncmp/8-id-search-module.js | 4 | ||||
-rw-r--r-- | k6-tests/ncmp/9-search-module.js | 4 | ||||
-rw-r--r-- | k6-tests/ncmp/common/cmhandle-crud.js | 90 | ||||
-rw-r--r-- | k6-tests/ncmp/common/passthrough-read.js | 36 | ||||
-rw-r--r-- | k6-tests/ncmp/common/search-base.js (renamed from k6-tests/ncmp/search-base.js) | 14 | ||||
-rw-r--r-- | k6-tests/ncmp/common/utils.js (renamed from k6-tests/ncmp/utils.js) | 10 |
15 files changed, 174 insertions, 117 deletions
diff --git a/k6-tests/ncmp/1-create-cmhandles.js b/k6-tests/ncmp/1-create-cmhandles.js index 60594c7127..9a5c22ad3f 100644 --- a/k6-tests/ncmp/1-create-cmhandles.js +++ b/k6-tests/ncmp/1-create-cmhandles.js @@ -18,16 +18,9 @@ * ============LICENSE_END========================================================= */ -import http from 'k6/http'; import exec from 'k6/execution'; -import { check } from 'k6'; -import { - NCMP_BASE_URL, - DMI_PLUGIN_URL, - TOTAL_CM_HANDLES, - makeBatchOfCmHandleIds, - makeCustomSummaryReport -} from './utils.js'; +import { TOTAL_CM_HANDLES, makeBatchOfCmHandleIds, makeCustomSummaryReport } from './common/utils.js'; +import { createCmHandles } from './common/cmhandle-crud.js'; const BATCH_SIZE = 100; export const options = { @@ -40,25 +33,9 @@ export const options = { }; export default function () { - const nextBatchOfCmHandleIds = makeBatchOfCmHandleIds(BATCH_SIZE, exec.scenario.iterationInTest); - const payload = { - "dmiPlugin": DMI_PLUGIN_URL, - "createdCmHandles": nextBatchOfCmHandleIds.map(cmHandleId => ({ - "cmHandle": cmHandleId, - "cmHandleProperties": {"neType": "RadioNode"}, - "publicCmHandleProperties": { - "Color": "yellow", - "Size": "small", - "Shape": "cube" - } - })), - }; - const response = http.post(NCMP_BASE_URL + '/ncmpInventory/v1/ch', JSON.stringify(payload), { - headers: {'Content-Type': 'application/json'}, - }); - check(response, { - 'status equals 200': (r) => r.status === 200, - }); + const batchNumber = exec.scenario.iterationInTest; + const nextBatchOfCmHandleIds = makeBatchOfCmHandleIds(BATCH_SIZE, batchNumber); + createCmHandles(nextBatchOfCmHandleIds); } export function handleSummary(data) { diff --git a/k6-tests/ncmp/10-mixed-load-test.js b/k6-tests/ncmp/10-mixed-load-test.js index 4bb0297d63..afa91af203 100644 --- a/k6-tests/ncmp/10-mixed-load-test.js +++ b/k6-tests/ncmp/10-mixed-load-test.js @@ -18,10 +18,9 @@ * ============LICENSE_END========================================================= */ -import http from 'k6/http'; -import { check } from 'k6'; -import { NCMP_BASE_URL, getRandomCmHandleId, makeCustomSummaryReport } from './utils.js' -import { executeCmHandleSearch, executeCmHandleIdSearch } from './search-base.js'; +import { makeCustomSummaryReport } from './common/utils.js' +import { executeCmHandleSearch, executeCmHandleIdSearch } from './common/search-base.js'; +import { passthroughRead } from './common/passthrough-read.js'; export const options = { scenarios: { @@ -56,13 +55,7 @@ export const options = { }; export function passthrough_read() { - const cmHandleId = getRandomCmHandleId(); - const datastoreName = 'ncmp-datastore%3Apassthrough-operational'; - const url = `${NCMP_BASE_URL}/ncmp/v1/ch/${cmHandleId}/data/ds/${datastoreName}?resourceIdentifier=x&include-descendants=true` - const response = http.get(url); - check(response, { - 'status equals 200': (r) => r.status === 200, - }); + passthroughRead(); } export function id_search_module() { diff --git a/k6-tests/ncmp/11-delete-cmhandles.js b/k6-tests/ncmp/11-delete-cmhandles.js index 073d1d0836..534b3de327 100644 --- a/k6-tests/ncmp/11-delete-cmhandles.js +++ b/k6-tests/ncmp/11-delete-cmhandles.js @@ -18,10 +18,9 @@ * ============LICENSE_END========================================================= */ -import http from 'k6/http'; import exec from 'k6/execution'; -import { check } from 'k6'; -import { NCMP_BASE_URL, TOTAL_CM_HANDLES, makeBatchOfCmHandleIds, makeCustomSummaryReport } from './utils.js'; +import { TOTAL_CM_HANDLES, makeBatchOfCmHandleIds, makeCustomSummaryReport } from './common/utils.js'; +import { deleteCmHandles } from './common/cmhandle-crud.js'; const BATCH_SIZE = 100; export const options = { @@ -34,17 +33,9 @@ export const options = { }; export default function () { - const nextBatchOfCmHandleIds = makeBatchOfCmHandleIds(BATCH_SIZE, exec.scenario.iterationInTest); - const payload = { - "dmiPlugin": "http://ncmp-dmi-plugin-demo-and-csit-stub:8092", - "removedCmHandles": nextBatchOfCmHandleIds, - }; - const response = http.post(NCMP_BASE_URL + '/ncmpInventory/v1/ch', JSON.stringify(payload), { - headers: {'Content-Type': 'application/json'}, - }); - check(response, { - 'status equals 200': (r) => r.status === 200, - }); + const batchNumber = exec.scenario.iterationInTest; + const nextBatchOfCmHandleIds = makeBatchOfCmHandleIds(BATCH_SIZE, batchNumber); + deleteCmHandles(nextBatchOfCmHandleIds); } export function handleSummary(data) { diff --git a/k6-tests/ncmp/2-wait-for-cmhandles-to-be-ready.js b/k6-tests/ncmp/2-wait-for-cmhandles-to-be-ready.js index 5d54c60f47..cce85ab7cb 100644 --- a/k6-tests/ncmp/2-wait-for-cmhandles-to-be-ready.js +++ b/k6-tests/ncmp/2-wait-for-cmhandles-to-be-ready.js @@ -18,9 +18,8 @@ * ============LICENSE_END========================================================= */ -import http from 'k6/http'; -import { sleep, fail } from 'k6'; -import { makeCustomSummaryReport, NCMP_BASE_URL, TOTAL_CM_HANDLES } from './utils.js'; +import { makeCustomSummaryReport } from './common/utils.js'; +import { waitForCmHandlesToBeReady } from './common/cmhandle-crud.js'; export const options = { vus: 1, @@ -32,35 +31,8 @@ export const options = { }; export default function () { - waitForCmHandlesToBeReady(TOTAL_CM_HANDLES); -} - -function waitForCmHandlesToBeReady(totalCmHandles) { const timeOutInSeconds = 6 * 60; - const pollingIntervalInSeconds = 10; - const maxRetries = Math.ceil(timeOutInSeconds / pollingIntervalInSeconds); - let cmHandlesReady = 0; - for (let currentTry = 0; currentTry <= maxRetries; currentTry++) { - sleep(pollingIntervalInSeconds); - try { - cmHandlesReady = getNumberOfReadyCmHandles(); - } catch (error) { - console.error(`Attempt ${currentTry + 1} - Error fetching CM handles: ${error.message}`); - } - console.log(`Attempt ${currentTry + 1} - ${cmHandlesReady}/${totalCmHandles} CM handles are READY`); - if (cmHandlesReady === totalCmHandles) { - console.log(`All ${totalCmHandles} CM handles are READY`); - return; - } - } - fail(`Timed out after ${timeoutInSeconds} seconds waiting for ${totalCmHandles} CM handles to be READY`); -} - -function getNumberOfReadyCmHandles() { - const endpointUrl = `${NCMP_BASE_URL}/cps/api/v2/dataspaces/NCMP-Admin/anchors/ncmp-dmi-registry/node?xpath=/dmi-registry&descendants=all`; - const jsonData = http.get(endpointUrl).json(); - const cmHandles = jsonData[0]["dmi-reg:dmi-registry"]["cm-handles"]; - return cmHandles.filter(cmhandle => cmhandle['state']['cm-handle-state'] === 'READY').length; + waitForCmHandlesToBeReady(timeOutInSeconds); } export function handleSummary(data) { diff --git a/k6-tests/ncmp/3-passthrough-read.js b/k6-tests/ncmp/3-passthrough-read.js index 84050b83ab..39eb4ad673 100644 --- a/k6-tests/ncmp/3-passthrough-read.js +++ b/k6-tests/ncmp/3-passthrough-read.js @@ -18,10 +18,9 @@ * ============LICENSE_END========================================================= */ -import http from 'k6/http'; -import { check } from 'k6'; -import { Trend } from "k6/metrics"; -import { NCMP_BASE_URL, getRandomCmHandleId, makeCustomSummaryReport } from './utils.js' +import { Trend } from 'k6/metrics'; +import { passthroughRead } from './common/passthrough-read.js' +import { makeCustomSummaryReport } from './common/utils.js' let ncmpOverheadTrend = new Trend("ncmp_overhead"); @@ -36,14 +35,7 @@ export const options = { // The function that defines VU logic. export default function () { - const cmHandleId = getRandomCmHandleId(); - const datastoreName = 'ncmp-datastore%3Apassthrough-operational'; - const url = `${NCMP_BASE_URL}/ncmp/v1/ch/${cmHandleId}/data/ds/${datastoreName}?resourceIdentifier=x&include-descendants=true` - const response = http.get(url); - check(response, { - 'status equals 200': (r) => r.status === 200, - }); - + const response = passthroughRead(); // Calculate overhead assuming DMI data delay is 2500ms. const dmiDelay = 2500; // This should be same as value DATA_FOR_CM_HANDLE_DELAY_MS in docker-compose.yml const overhead = response.timings.duration - dmiDelay; diff --git a/k6-tests/ncmp/4-id-search-no-filter.js b/k6-tests/ncmp/4-id-search-no-filter.js index ed8d77f2e7..3863b5d789 100644 --- a/k6-tests/ncmp/4-id-search-no-filter.js +++ b/k6-tests/ncmp/4-id-search-no-filter.js @@ -18,8 +18,8 @@ * ============LICENSE_END========================================================= */ -import { executeCmHandleIdSearch } from './search-base.js'; -import { makeCustomSummaryReport } from "./utils.js"; +import { executeCmHandleIdSearch } from './common/search-base.js'; +import { makeCustomSummaryReport } from './common/utils.js'; export const options = { vus: 5, diff --git a/k6-tests/ncmp/5-search-no-filter.js b/k6-tests/ncmp/5-search-no-filter.js index 73fa82787b..67c9d59827 100644 --- a/k6-tests/ncmp/5-search-no-filter.js +++ b/k6-tests/ncmp/5-search-no-filter.js @@ -18,8 +18,8 @@ * ============LICENSE_END========================================================= */ -import { executeCmHandleSearch } from './search-base.js'; -import { makeCustomSummaryReport } from "./utils.js"; +import { executeCmHandleSearch } from './common/search-base.js'; +import { makeCustomSummaryReport } from './common/utils.js'; export const options = { vus: 4, diff --git a/k6-tests/ncmp/6-id-search-public-property.js b/k6-tests/ncmp/6-id-search-public-property.js index 543ee1a87d..25bffaf25d 100644 --- a/k6-tests/ncmp/6-id-search-public-property.js +++ b/k6-tests/ncmp/6-id-search-public-property.js @@ -18,8 +18,8 @@ * ============LICENSE_END========================================================= */ -import { executeCmHandleIdSearch } from './search-base.js'; -import { makeCustomSummaryReport } from "./utils.js"; +import { executeCmHandleIdSearch } from './common/search-base.js'; +import { makeCustomSummaryReport } from './common/utils.js'; export const options = { vus: 5, diff --git a/k6-tests/ncmp/7-search-public-property.js b/k6-tests/ncmp/7-search-public-property.js index cb39b54d9c..53f069d022 100644 --- a/k6-tests/ncmp/7-search-public-property.js +++ b/k6-tests/ncmp/7-search-public-property.js @@ -18,8 +18,8 @@ * ============LICENSE_END========================================================= */ -import { executeCmHandleSearch } from './search-base.js'; -import { makeCustomSummaryReport } from "./utils.js"; +import { executeCmHandleSearch } from './common/search-base.js'; +import { makeCustomSummaryReport } from './common/utils.js'; export const options = { vus: 4, diff --git a/k6-tests/ncmp/8-id-search-module.js b/k6-tests/ncmp/8-id-search-module.js index d058a1bf14..8200ea28ca 100644 --- a/k6-tests/ncmp/8-id-search-module.js +++ b/k6-tests/ncmp/8-id-search-module.js @@ -18,8 +18,8 @@ * ============LICENSE_END========================================================= */ -import { executeCmHandleIdSearch } from './search-base.js'; -import { makeCustomSummaryReport } from "./utils.js"; +import { executeCmHandleIdSearch } from './common/search-base.js'; +import { makeCustomSummaryReport } from './common/utils.js'; export const options = { vus: 5, diff --git a/k6-tests/ncmp/9-search-module.js b/k6-tests/ncmp/9-search-module.js index c6bbb06db1..eafef99b34 100644 --- a/k6-tests/ncmp/9-search-module.js +++ b/k6-tests/ncmp/9-search-module.js @@ -18,8 +18,8 @@ * ============LICENSE_END========================================================= */ -import { executeCmHandleSearch } from './search-base.js'; -import { makeCustomSummaryReport } from "./utils.js"; +import { executeCmHandleSearch } from './common/search-base.js'; +import { makeCustomSummaryReport } from './common/utils.js'; export const options = { vus: 4, diff --git a/k6-tests/ncmp/common/cmhandle-crud.js b/k6-tests/ncmp/common/cmhandle-crud.js new file mode 100644 index 0000000000..0c3e116a19 --- /dev/null +++ b/k6-tests/ncmp/common/cmhandle-crud.js @@ -0,0 +1,90 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2024 Nordix Foundation + * ================================================================================ + * 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 + * + * http://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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +import http from 'k6/http'; +import { check, sleep, fail } from 'k6'; +import { NCMP_BASE_URL, DMI_PLUGIN_URL, TOTAL_CM_HANDLES } from './utils.js'; + +export function createCmHandles(cmHandleIds) { + const url = `${NCMP_BASE_URL}/ncmpInventory/v1/ch`; + const payload = { + "dmiPlugin": DMI_PLUGIN_URL, + "createdCmHandles": cmHandleIds.map(cmHandleId => ({ + "cmHandle": cmHandleId, + "cmHandleProperties": {"neType": "RadioNode"}, + "publicCmHandleProperties": { + "Color": "yellow", + "Size": "small", + "Shape": "cube" + } + })), + }; + const params = { + headers: {'Content-Type': 'application/json'} + }; + const response = http.post(url, JSON.stringify(payload), params); + check(response, { + 'status equals 200': (r) => r.status === 200, + }); + return response; +} + +export function deleteCmHandles(cmHandleIds) { + const url = `${NCMP_BASE_URL}/ncmpInventory/v1/ch`; + const payload = { + "dmiPlugin": DMI_PLUGIN_URL, + "removedCmHandles": cmHandleIds, + }; + const params = { + headers: {'Content-Type': 'application/json'} + }; + const response = http.post(url, JSON.stringify(payload), params); + check(response, { + 'status equals 200': (r) => r.status === 200, + }); + return response; +} + +export function waitForCmHandlesToBeReady(timeOutInSeconds) { + const pollingIntervalInSeconds = 10; + const maxRetries = Math.ceil(timeOutInSeconds / pollingIntervalInSeconds); + let cmHandlesReady = 0; + for (let currentTry = 0; currentTry <= maxRetries; currentTry++) { + sleep(pollingIntervalInSeconds); + try { + cmHandlesReady = getNumberOfReadyCmHandles(); + } catch (error) { + console.error(`Attempt ${currentTry + 1} - Error fetching CM handles: ${error.message}`); + } + console.log(`Attempt ${currentTry + 1} - ${cmHandlesReady}/${TOTAL_CM_HANDLES} CM handles are READY`); + if (cmHandlesReady === TOTAL_CM_HANDLES) { + console.log(`All ${TOTAL_CM_HANDLES} CM handles are READY`); + return; + } + } + fail(`Timed out after ${timeOutInSeconds} seconds waiting for ${TOTAL_CM_HANDLES} CM handles to be READY`); +} + +function getNumberOfReadyCmHandles() { + const endpointUrl = `${NCMP_BASE_URL}/cps/api/v2/dataspaces/NCMP-Admin/anchors/ncmp-dmi-registry/node?xpath=/dmi-registry&descendants=all`; + const jsonData = http.get(endpointUrl).json(); + const cmHandles = jsonData[0]["dmi-reg:dmi-registry"]["cm-handles"]; + return cmHandles.filter(cmhandle => cmhandle['state']['cm-handle-state'] === 'READY').length; +} diff --git a/k6-tests/ncmp/common/passthrough-read.js b/k6-tests/ncmp/common/passthrough-read.js new file mode 100644 index 0000000000..e4e937c9c0 --- /dev/null +++ b/k6-tests/ncmp/common/passthrough-read.js @@ -0,0 +1,36 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2024 Nordix Foundation + * ================================================================================ + * 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 + * + * http://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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +import http from 'k6/http'; +import { check } from 'k6'; +import { NCMP_BASE_URL, getRandomCmHandleId } from './utils.js'; + +export function passthroughRead() { + const cmHandleId = getRandomCmHandleId(); + const resourceIdentifier = 'my-resource-identifier'; + const includeDescendants = true; + const datastoreName = 'ncmp-datastore:passthrough-operational'; + const url = `${NCMP_BASE_URL}/ncmp/v1/ch/${cmHandleId}/data/ds/${datastoreName}?resourceIdentifier=${resourceIdentifier}&include-descendants=${includeDescendants}` + const response = http.get(url); + check(response, { + 'status equals 200': (r) => r.status === 200, + }); + return response; +} diff --git a/k6-tests/ncmp/search-base.js b/k6-tests/ncmp/common/search-base.js index 04b644425d..f833a5388a 100644 --- a/k6-tests/ncmp/search-base.js +++ b/k6-tests/ncmp/common/search-base.js @@ -51,14 +51,18 @@ export function executeCmHandleIdSearch(scenario) { } function executeSearchRequest(searchType, scenario) { - const searchParameter = JSON.stringify(SEARCH_PARAMETERS_PER_SCENARIO[scenario]); - const response = http.post(NCMP_BASE_URL + '/ncmp/v1/ch/' + searchType, searchParameter, { - headers: {'Content-Type': 'application/json'}, - }); + const searchParameters = SEARCH_PARAMETERS_PER_SCENARIO[scenario]; + const payload = JSON.stringify(searchParameters); + const url = `${NCMP_BASE_URL}/ncmp/v1/ch/${searchType}`; + const params = { + headers: {'Content-Type': 'application/json'} + }; + const response = http.post(url, payload, params); check(response, { 'status equals 200': (r) => r.status === 200, }); - check(JSON.parse(response.body), { + const responseData = JSON.parse(response.body); + check(responseData, { 'returned list has expected CM-handles': (arr) => arr.length === TOTAL_CM_HANDLES, }); } diff --git a/k6-tests/ncmp/utils.js b/k6-tests/ncmp/common/utils.js index 18a8940523..1fb9b8e670 100644 --- a/k6-tests/ncmp/utils.js +++ b/k6-tests/ncmp/common/utils.js @@ -22,9 +22,11 @@ export const NCMP_BASE_URL = 'http://localhost:8883'; export const DMI_PLUGIN_URL = 'http://ncmp-dmi-plugin-demo-and-csit-stub:8092'; export const TOTAL_CM_HANDLES = 20000 -/* - * Makes a batch of CM-handle IDs. - * Given a batchSize=100 and batchNumber=2, it will generate ['ch-201', 'ch-202' ... 'ch-300'] +/** + * Generates a batch of CM-handle IDs based on batch size and number. + * @param {number} batchSize - Size of each batch. + * @param {number} batchNumber - Number of the batch. + * @returns {string[]} Array of CM-handle IDs, for example ['ch-201', 'ch-202' ... 'ch-300'] */ export function makeBatchOfCmHandleIds(batchSize, batchNumber) { const batchOfIds = []; @@ -37,7 +39,7 @@ export function makeBatchOfCmHandleIds(batchSize, batchNumber) { } export function getRandomCmHandleId() { - return 'ch-' + (Math.floor(Math.random() * TOTAL_CM_HANDLES) + 1); + return `ch-${Math.floor(Math.random() * TOTAL_CM_HANDLES) + 1}`; } function removeBracketsAndQuotes(str) { |