Use constants for magic numbers in perf tests
[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 class CpsPerfTestBase extends PerfTestBase {
28
29     static final def CPS_PERFORMANCE_TEST_DATASPACE = 'cpsPerformanceDataspace'
30     static final def OPENROADM_ANCHORS = 5
31     static final def OPENROADM_DEVICES_PER_ANCHOR = 50
32     static final def OPENROADM_DATANODES_PER_DEVICE = 86
33
34     def printTitle() {
35         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          ##')
36     }
37
38     def isInitialised() {
39         return dataspaceExists(CPS_PERFORMANCE_TEST_DATASPACE)
40     }
41
42     def setupPerformanceInfraStructure() {
43         cpsAdminService.createDataspace(CPS_PERFORMANCE_TEST_DATASPACE)
44         def modelAsString = readResourceDataFile('bookstore/bookstore.yang')
45         cpsModuleService.createSchemaSet(CPS_PERFORMANCE_TEST_DATASPACE, BOOKSTORE_SCHEMA_SET, [bookstore: modelAsString])
46     }
47
48     def createInitialData() {
49         createWarmupData()
50         createLargeBookstoresData()
51         addOpenRoadModel()
52         addOpenRoadData()
53     }
54
55     def createWarmupData() {
56         def data = "{\"bookstore\":{}}"
57         stopWatch.start()
58         addAnchorsWithData(1,  CPS_PERFORMANCE_TEST_DATASPACE, BOOKSTORE_SCHEMA_SET, 'warmup', data)
59         stopWatch.stop()
60         def durationInMillis = stopWatch.getTotalTimeMillis()
61         recordAndAssertPerformance('Creating warmup anchor with tiny data tree', 500, durationInMillis)
62     }
63
64     def createLargeBookstoresData() {
65         def data = readResourceDataFile('bookstore/largeModelData.json')
66         stopWatch.start()
67         addAnchorsWithData(5, CPS_PERFORMANCE_TEST_DATASPACE, BOOKSTORE_SCHEMA_SET, 'bookstore', data)
68         stopWatch.stop()
69         def durationInMillis = stopWatch.getTotalTimeMillis()
70         recordAndAssertPerformance('Creating bookstore anchors with large data tree', 1_500, durationInMillis)
71     }
72
73     def addOpenRoadModel() {
74         def file = new File('src/test/resources/data/openroadm/correctedModel.zip')
75         def multipartFile = Mock(MultipartFile)
76         multipartFile.getOriginalFilename() >> file.getName()
77         multipartFile.getInputStream() >> new FileInputStream(file)
78         cpsModuleService.createSchemaSet(CPS_PERFORMANCE_TEST_DATASPACE, LARGE_SCHEMA_SET, MultipartFileUtil.extractYangResourcesMap(multipartFile))
79     }
80
81     def addOpenRoadData() {
82         def data = generateOpenRoadData(OPENROADM_DEVICES_PER_ANCHOR)
83         stopWatch.start()
84         addAnchorsWithData(OPENROADM_ANCHORS, CPS_PERFORMANCE_TEST_DATASPACE, LARGE_SCHEMA_SET, 'openroadm', data)
85         stopWatch.stop()
86         def durationInMillis = stopWatch.getTotalTimeMillis()
87         recordAndAssertPerformance('Creating openroadm anchors with large data tree', 20_000, durationInMillis)
88     }
89
90     def generateOpenRoadData(numberOfNodes) {
91         def innerNode = readResourceDataFile('openroadm/innerNode.json')
92         return '{ "openroadm-devices": { "openroadm-device": [' +
93             (1..numberOfNodes).collect { innerNode.replace('NODE_ID_HERE', it.toString()) }.join(',') +
94             ']}}'
95     }
96
97     def 'Warm the database'() {
98         when: 'get data nodes for warmup anchor'
99             stopWatch.start()
100             def result = cpsDataService.getDataNodes(CPS_PERFORMANCE_TEST_DATASPACE, 'warmup1', '/', FetchDescendantsOption.OMIT_DESCENDANTS)
101             assert countDataNodesInTree(result) == 1
102             stopWatch.stop()
103             def durationInMillis = stopWatch.getTotalTimeMillis()
104         then: 'all data is read within 20 seconds'
105             recordAndAssertPerformance("Warming database", 20_000, durationInMillis)
106     }
107
108 }