#2: Used async version of web client for single cm handle read operation
[cps.git] / integration-test / src / test / groovy / org / onap / cps / integration / performance / base / NcmpPerfTestBase.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2023-2024 Nordix Foundation
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the 'License');
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an 'AS IS' BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.integration.performance.base
22
23 import static org.onap.cps.ncmp.api.impl.ncmppersistence.NcmpPersistence.NCMP_DATASPACE_NAME
24 import static org.onap.cps.ncmp.api.impl.ncmppersistence.NcmpPersistence.NCMP_DMI_REGISTRY_ANCHOR
25
26 import org.onap.cps.integration.ResourceMeter
27 import org.onap.cps.spi.FetchDescendantsOption
28
29 class NcmpPerfTestBase extends PerfTestBase {
30
31     def static NCMP_PERFORMANCE_TEST_DATASPACE = 'ncmpPerformance'
32     def static REGISTRY_ANCHOR = 'ncmp-registry'
33     def static REGISTRY_SCHEMA_SET = 'registrySchemaSet'
34     def static TOTAL_CM_HANDLES = 20_000
35     def static CM_DATA_SUBSCRIPTIONS_ANCHOR = 'cm-data-subscriptions'
36     def static CM_DATA_SUBSCRIPTIONS_SCHEMA_SET = 'cmDataSubscriptionsSchemaSet'
37
38     def datastore1cmHandlePlaceHolder = '{"datastores":{"datastore":[{"name":"ds-1","cm-handles":{"cm-handle":[]}}]}}'
39     def xPathForDataStore1CmHandles = '/datastores/datastore[@name="ds-1"]/cm-handles'
40     def numberOfCmDataSubscribers = 200
41     def numberOfFiltersPerCmHandle = 10
42     def numberOfCmHandlesPerCmDataSubscription = 200
43
44     ResourceMeter resourceMeter = new ResourceMeter()
45
46     def subscriberIdPrefix = 'some really long subscriber id to see if this makes any difference to the performance'
47     def xpathPrefix = 'some really long xpath/with/loads/of/children/grandchildren/and/whatever/else/I/can/think/of to see if this makes any difference to the performance'
48     def cmHandlePrefix = 'some really long cm handle id to see if this makes any difference to the performance'
49
50     def printTitle() {
51         println('##                  N C M P   P E R F O R M A N C E   T E S T   R E S U L T S                   ##')
52     }
53
54     def isInitialised() {
55         return dataspaceExists(NCMP_PERFORMANCE_TEST_DATASPACE)
56     }
57
58     def setupPerformanceInfraStructure() {
59         cpsDataspaceService.createDataspace(NCMP_PERFORMANCE_TEST_DATASPACE)
60         createRegistrySchemaSet()
61         createCmDataSubscriptionsSchemaSet()
62     }
63
64     def createInitialData() {
65         addRegistryData()
66         addRegistryDataWithAlternateIdAsPath()
67         addCmSubscriptionData()
68     }
69
70     def createRegistrySchemaSet() {
71         def modelAsString = readResourceDataFile('ncmp-registry/dmi-registry@2024-02-23.yang')
72         cpsModuleService.createSchemaSet(NCMP_PERFORMANCE_TEST_DATASPACE, REGISTRY_SCHEMA_SET, [registry: modelAsString])
73     }
74
75     def addRegistryData() {
76         cpsAnchorService.createAnchor(NCMP_PERFORMANCE_TEST_DATASPACE, REGISTRY_SCHEMA_SET, REGISTRY_ANCHOR)
77         cpsDataService.saveData(NCMP_PERFORMANCE_TEST_DATASPACE, REGISTRY_ANCHOR, '{"dmi-registry": []}', now)
78         def innerNodeJsonTemplate = readResourceDataFile('ncmp-registry/innerNode.json')
79         def batchSize = 100
80         for (def i = 0; i < TOTAL_CM_HANDLES; i += batchSize) {
81             def data = '{ "cm-handles": [' + (1..batchSize).collect { innerNodeJsonTemplate.replace('CMHANDLE_ID_HERE', (it + i).toString()) }.join(',') + ']}'
82             cpsDataService.saveListElements(NCMP_PERFORMANCE_TEST_DATASPACE, REGISTRY_ANCHOR, '/dmi-registry', data, now)
83         }
84     }
85
86     def addRegistryDataWithAlternateIdAsPath() {
87         def innerNodeJsonTemplate = readResourceDataFile('ncmp-registry/innerCmHandleNode.json')
88         def batchSize = 10
89         for (def i = 0; i < TOTAL_CM_HANDLES; i += batchSize) {
90             def data = '{ "cm-handles": [' + (1..batchSize).collect {
91                 innerNodeJsonTemplate.replace('CM_HANDLE_ID_HERE', (it + i).toString())
92                         .replace('ALTERNATE_ID_AS_PATH', (it + i).toString())
93             }.join(',') + ']}'
94             cpsDataService.saveListElements(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, '/dmi-registry', data, now)
95         }
96     }
97
98     def createCmDataSubscriptionsSchemaSet() {
99         def modelAsString = readResourceDataFile('cm-data-subscriptions/cm-data-subscriptions@2023-09-21.yang')
100         cpsModuleService.createSchemaSet(NCMP_PERFORMANCE_TEST_DATASPACE, CM_DATA_SUBSCRIPTIONS_SCHEMA_SET, [registry: modelAsString])
101     }
102
103     def addCmSubscriptionData() {
104         cpsAnchorService.createAnchor(NCMP_PERFORMANCE_TEST_DATASPACE, CM_DATA_SUBSCRIPTIONS_SCHEMA_SET, CM_DATA_SUBSCRIPTIONS_ANCHOR)
105         cpsDataService.saveData(NCMP_PERFORMANCE_TEST_DATASPACE, CM_DATA_SUBSCRIPTIONS_ANCHOR, datastore1cmHandlePlaceHolder, now)
106         def subscribers = createLeafList('subscribers',numberOfCmDataSubscribers, subscriberIdPrefix)
107         def filters = '"filters":' + createJsonArray('filter',numberOfFiltersPerCmHandle,'xpath',xpathPrefix,subscribers)
108         def cmHandles = createJsonArray('cm-handle',numberOfCmHandlesPerCmDataSubscription,'id',cmHandlePrefix, filters)
109         cpsDataService.saveData(NCMP_PERFORMANCE_TEST_DATASPACE, CM_DATA_SUBSCRIPTIONS_ANCHOR, xPathForDataStore1CmHandles, cmHandles, now)
110     }
111
112     def 'NCMP pre-load test data'() {
113         when: 'dummy get data nodes runs so that populating the DB does not get included in other test timings'
114             resourceMeter.start()
115             def result = cpsDataService.getDataNodes(NCMP_PERFORMANCE_TEST_DATASPACE, REGISTRY_ANCHOR, '/', FetchDescendantsOption.OMIT_DESCENDANTS)
116             resourceMeter.stop()
117         then: 'expected data exists'
118             assert result.xpath == ['/dmi-registry']
119         and: 'operation completes within expected time'
120             recordAndAssertResourceUsage('NCMP pre-load test data',
121                     15, resourceMeter.totalTimeInSeconds,
122                     600, resourceMeter.totalMemoryUsageInMB)
123     }
124
125 }