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