5fd27476c03677035d79fc928a8b7e4ef57fc55a
[cps.git] / integration-test / src / test / groovy / org / onap / cps / integration / performance / base / CpsPerfTestBase.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2023 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 org.onap.cps.rest.utils.MultipartFileUtil
24 import org.onap.cps.spi.FetchDescendantsOption
25 import org.springframework.web.multipart.MultipartFile
26
27 import java.util.concurrent.TimeUnit
28
29 class CpsPerfTestBase extends PerfTestBase {
30
31     static final def CPS_PERFORMANCE_TEST_DATASPACE = 'cpsPerformanceDataspace'
32     static final def OPENROADM_ANCHORS = 3
33     static final def OPENROADM_DEVICES_PER_ANCHOR = 1000
34     static final def OPENROADM_DATANODES_PER_DEVICE = 86
35
36     def printTitle() {
37         println('##        C P S   P E R F O R M A N C E   T E S T   R E S U L T S          ##')
38     }
39
40     def isInitialised() {
41         return dataspaceExists(CPS_PERFORMANCE_TEST_DATASPACE)
42     }
43
44     def setupPerformanceInfraStructure() {
45         cpsAdminService.createDataspace(CPS_PERFORMANCE_TEST_DATASPACE)
46         def modelAsString = readResourceDataFile('bookstore/bookstore.yang')
47         cpsModuleService.createSchemaSet(CPS_PERFORMANCE_TEST_DATASPACE, BOOKSTORE_SCHEMA_SET, [bookstore: modelAsString])
48     }
49
50     def createInitialData() {
51         addOpenRoadModel()
52         addOpenRoadData()
53     }
54
55     def addOpenRoadModel() {
56         def file = new File('src/test/resources/data/openroadm/correctedModel.zip')
57         def multipartFile = Mock(MultipartFile)
58         multipartFile.getOriginalFilename() >> file.getName()
59         multipartFile.getInputStream() >> new FileInputStream(file)
60         cpsModuleService.createSchemaSet(CPS_PERFORMANCE_TEST_DATASPACE, LARGE_SCHEMA_SET, MultipartFileUtil.extractYangResourcesMap(multipartFile))
61     }
62
63     def addOpenRoadData() {
64         def data = generateOpenRoadData(OPENROADM_DEVICES_PER_ANCHOR)
65         stopWatch.start()
66         addAnchorsWithData(OPENROADM_ANCHORS, CPS_PERFORMANCE_TEST_DATASPACE, LARGE_SCHEMA_SET, 'openroadm', data)
67         stopWatch.stop()
68         def durationInMillis = stopWatch.getTotalTimeMillis()
69         recordAndAssertPerformance('Creating openroadm anchors with large data tree', TimeUnit.SECONDS.toMillis(200), durationInMillis)
70     }
71
72     def generateOpenRoadData(numberOfNodes) {
73         def innerNode = readResourceDataFile('openroadm/innerNode.json')
74         return '{ "openroadm-devices": { "openroadm-device": [' +
75             (1..numberOfNodes).collect { innerNode.replace('NODE_ID_HERE', it.toString()) }.join(',') +
76             ']}}'
77     }
78
79     def 'Warm the database'() {
80         when: 'dummy get data nodes runs so that populating the DB does not get included in other test timings'
81             stopWatch.start()
82             def result = cpsDataService.getDataNodes(CPS_PERFORMANCE_TEST_DATASPACE, 'openroadm1', '/', FetchDescendantsOption.OMIT_DESCENDANTS)
83             assert countDataNodesInTree(result) == 1
84             stopWatch.stop()
85             def durationInMillis = stopWatch.getTotalTimeMillis()
86         then: 'all data is read within expected time'
87             recordAndAssertPerformance("Warming database", TimeUnit.SECONDS.toMillis(200), durationInMillis)
88     }
89
90 }