Create Base and Sample Performance Integration 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 java.time.OffsetDateTime
24 import org.onap.cps.integration.base.CpsIntegrationSpecBase
25 import org.onap.cps.rest.utils.MultipartFileUtil
26 import org.springframework.web.multipart.MultipartFile
27
28 class CpsPerfTestBase extends PerfTestBase {
29
30     static def CPS_PERFORMANCE_TEST_DATASPACE = 'cpsPerformanceDataspace'
31
32     def printTitle() {
33         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          ##')
34     }
35
36     def isInitialised() {
37         return dataspaceExists(CPS_PERFORMANCE_TEST_DATASPACE)
38     }
39
40     def setupPerformanceInfraStructure() {
41         cpsAdminService.createDataspace(CPS_PERFORMANCE_TEST_DATASPACE)
42         def modelAsString = CpsIntegrationSpecBase.readResourceDataFile('bookstore/bookstore.yang')
43         cpsModuleService.createSchemaSet(CPS_PERFORMANCE_TEST_DATASPACE, CpsIntegrationSpecBase.BOOKSTORE_SCHEMA_SET, [bookstore: modelAsString])
44     }
45
46     def createInitialData() {
47         createLargeBookstoresData()
48         addOpenRoadModel()
49         addOpenRoadData()
50     }
51
52     def createLargeBookstoresData() {
53         def data = CpsIntegrationSpecBase.readResourceDataFile('bookstore/largeModelData.json')
54         stopWatch.start()
55         addAnchorsWithData(5, CpsIntegrationSpecBase.BOOKSTORE_SCHEMA_SET, 'bookstore', data)
56         stopWatch.stop()
57         def durationInMillis = stopWatch.getTotalTimeMillis()
58         recordAndAssertPerformance('Creating bookstore anchors with large data tree', 3_000, durationInMillis)
59     }
60
61     def addOpenRoadModel() {
62         def file = new File('src/test/resources/data/openroadm/correctedModel.zip')
63         def multipartFile = Mock(MultipartFile)
64         multipartFile.getOriginalFilename() >> file.getName()
65         multipartFile.getInputStream() >> new FileInputStream(file)
66         cpsModuleService.createSchemaSet(CPS_PERFORMANCE_TEST_DATASPACE, PerfTestBase.LARGE_SCHEMA_SET, MultipartFileUtil.extractYangResourcesMap(multipartFile))
67     }
68
69     def addOpenRoadData() {
70         def data = CpsIntegrationSpecBase.readResourceDataFile('openroadm/innerNode.json')
71         stopWatch.start()
72         addAnchorsWithData(5, PerfTestBase.LARGE_SCHEMA_SET, 'openroadm', data)
73         stopWatch.stop()
74         def durationInMillis = stopWatch.getTotalTimeMillis()
75         recordAndAssertPerformance('Creating openroadm anchors with large data tree', 25_000, durationInMillis)
76     }
77
78     def addAnchorsWithData(numberOfAnchors, schemaSetName, anchorNamePrefix, data) {
79         (1..numberOfAnchors).each {
80             cpsAdminService.createAnchor(CPS_PERFORMANCE_TEST_DATASPACE, schemaSetName, anchorNamePrefix + it)
81             cpsDataService.saveData(CPS_PERFORMANCE_TEST_DATASPACE, anchorNamePrefix + it, data, OffsetDateTime.now())
82         }
83
84     }
85
86 }