Temp Table Creation improvements
[cps.git] / cps-ri / src / test / groovy / org / onap / cps / spi / performance / CpsModuleReferenceRepositoryPerfTest.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2022 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.spi.performance
22
23 import org.onap.cps.spi.CpsModulePersistenceService
24 import org.onap.cps.spi.entities.SchemaSetEntity
25 import org.onap.cps.spi.impl.CpsPersistenceSpecBase
26 import org.onap.cps.spi.model.ModuleReference
27 import org.onap.cps.spi.repository.DataspaceRepository
28 import org.onap.cps.spi.repository.ModuleReferenceRepository
29 import org.onap.cps.spi.repository.SchemaSetRepository
30 import org.springframework.beans.factory.annotation.Autowired
31 import org.springframework.test.context.jdbc.Sql
32 import org.springframework.util.StopWatch
33
34 import java.util.concurrent.ThreadLocalRandom
35
36 class CpsModuleReferenceRepositoryPerfTest extends CpsPersistenceSpecBase {
37
38     static final String PERF_TEST_DATA = '/data/perf-test.sql'
39
40     def NEW_RESOURCE_CONTENT = 'module stores {\n' +
41         '    yang-version 1.1;\n' +
42         '    namespace "org:onap:ccsdk:sample";\n' +
43         '\n' +
44         '    prefix book-store;\n' +
45         '\n' +
46         '    revision "2020-09-15" {\n' +
47         '        description\n' +
48         '        "Sample Model";\n' +
49         '    }' +
50         '}'
51
52     @Autowired
53     CpsModulePersistenceService objectUnderTest
54
55     @Autowired
56     DataspaceRepository dataspaceRepository
57
58     @Autowired
59     SchemaSetRepository schemaSetRepository
60
61     @Autowired
62     ModuleReferenceRepository moduleReferenceRepository
63
64     @Sql([CLEAR_DATA, PERF_TEST_DATA])
65     def 'Store new schema set with many modules'() {
66         when: 'a new schema set with 200 modules is stored'
67             def newYangResourcesNameToContentMap = [:]
68             (1..200).each {
69                 def year = 2000 + it
70                 def resourceName = "module${it}".toString()
71                 def moduleName = "stores${it}"
72                 def content = NEW_RESOURCE_CONTENT.replace('2020',String.valueOf(year)).replace('stores',moduleName)
73                 newYangResourcesNameToContentMap.put(resourceName, content)
74             }
75             objectUnderTest.storeSchemaSet('PERF-DATASPACE', 'perfSchemaSet', newYangResourcesNameToContentMap)
76         then: 'the schema set is persisted correctly'
77             def dataspaceEntity = dataspaceRepository.getByName('PERF-DATASPACE')
78             SchemaSetEntity result = schemaSetRepository.getByDataspaceAndName(dataspaceEntity, 'perfSchemaSet')
79             result.yangResources.size() == 200
80         and: 'identification of new module resources is fast enough (1,000 executions less then 5,000 milliseconds)'
81             def stopWatch = new StopWatch()
82             1000.times() {
83                 def moduleReferencesToCheck = createModuleReferencesWithRandomMatchingExistingModuleReferences()
84                 stopWatch.start()
85                 def newModuleReferences = moduleReferenceRepository.identifyNewModuleReferences(moduleReferencesToCheck)
86                 stopWatch.stop()
87                 assert newModuleReferences.size() > 0 && newModuleReferences.size() < 300
88             }
89             assert stopWatch.getTotalTimeMillis() < 5000
90     }
91
92     def createModuleReferencesWithRandomMatchingExistingModuleReferences() {
93         def moduleReferences = []
94         (1..250).each {
95             def randomNumber = ThreadLocalRandom.current().nextInt(1, 300)
96             def year = 2000 + randomNumber
97             def moduleName = "stores${randomNumber}"
98             moduleReferences.add(new ModuleReference(moduleName, "${year}-09-15"))
99         }
100         return moduleReferences
101     }
102
103 }