summaryrefslogtreecommitdiffstats
path: root/cps-ri/src/test/groovy/org/onap/cps/spi/performance/CpsModuleReferenceRepositoryPerfTest.groovy
blob: 222a828b9f65b3830300396e51df495edb22065a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
 *  ============LICENSE_START=======================================================
 *  Copyright (C) 2022-2023 Nordix Foundation
 *  ================================================================================
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *  SPDX-License-Identifier: Apache-2.0
 *  ============LICENSE_END=========================================================
 */

package org.onap.cps.spi.performance

import org.onap.cps.spi.CpsModulePersistenceService
import org.onap.cps.spi.entities.SchemaSetEntity
import org.onap.cps.spi.impl.CpsPersistenceSpecBase
import org.onap.cps.spi.model.ModuleReference
import org.onap.cps.spi.repository.ModuleReferenceRepository
import org.onap.cps.spi.repository.SchemaSetRepository
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.test.context.jdbc.Sql
import org.springframework.util.StopWatch

import java.util.concurrent.ThreadLocalRandom

class CpsModuleReferenceRepositoryPerfTest extends CpsPersistenceSpecBase {

    static final String PERF_TEST_DATA = '/data/perf-test.sql'

    def NEW_RESOURCE_CONTENT = 'module stores {\n' +
        '    yang-version 1.1;\n' +
        '    namespace "org:onap:ccsdk:sample";\n' +
        '\n' +
        '    prefix book-store;\n' +
        '\n' +
        '    revision "2020-09-15" {\n' +
        '        description\n' +
        '        "Sample Model";\n' +
        '    }' +
        '}'

    @Autowired
    CpsModulePersistenceService objectUnderTest

    @Autowired
    SchemaSetRepository schemaSetRepository

    @Autowired
    ModuleReferenceRepository moduleReferenceRepository

    @Sql([CLEAR_DATA, PERF_TEST_DATA])
    def 'Store new schema set with many modules'() {
        when: 'a new schema set with 200 modules is stored'
            def newYangResourcesNameToContentMap = [:]
            (1..200).each {
                def year = 2000 + it
                def resourceName = "module${it}".toString()
                def moduleName = "stores${it}"
                def content = NEW_RESOURCE_CONTENT.replace('2020',String.valueOf(year)).replace('stores',moduleName)
                newYangResourcesNameToContentMap.put(resourceName, content)
            }
            objectUnderTest.storeSchemaSet('PERF-DATASPACE', 'perfSchemaSet', newYangResourcesNameToContentMap)
        then: 'the schema set is persisted correctly'
            def dataspaceEntity = dataspaceRepository.getByName('PERF-DATASPACE')
            SchemaSetEntity result = schemaSetRepository.getByDataspaceAndName(dataspaceEntity, 'perfSchemaSet')
            result.yangResources.size() == 200
        and: 'identification of new module resources is fast enough (1,000 executions less then 6,000 milliseconds)'
            def stopWatch = new StopWatch()
            1000.times() {
                def moduleReferencesToCheck = createModuleReferencesWithRandomMatchingExistingModuleReferences()
                stopWatch.start()
                def newModuleReferences = moduleReferenceRepository.identifyNewModuleReferences(moduleReferencesToCheck)
                stopWatch.stop()
                assert newModuleReferences.size() > 0 && newModuleReferences.size() < 300
            }
            assert stopWatch.getTotalTimeMillis() < 6000
    }

    def createModuleReferencesWithRandomMatchingExistingModuleReferences() {
        def moduleReferences = []
        (1..250).each {
            def randomNumber = ThreadLocalRandom.current().nextInt(1, 300)
            def year = 2000 + randomNumber
            def moduleName = "stores${randomNumber}"
            moduleReferences.add(new ModuleReference(moduleName, "${year}-09-15"))
        }
        return moduleReferences
    }

}