DMI Data AVC to use kafka headers
[cps.git] / cps-ri / src / test / groovy / org / onap / cps / spi / performance / CpsDataPersistenceServicePerfTest.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.impl.CpsPersistencePerfSpecBase
24 import org.onap.cps.spi.CpsDataPersistenceService
25 import org.onap.cps.spi.repository.AnchorRepository
26 import org.onap.cps.spi.repository.DataspaceRepository
27 import org.onap.cps.spi.repository.FragmentRepository
28 import org.springframework.beans.factory.annotation.Autowired
29 import org.springframework.test.context.jdbc.Sql
30
31 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
32 import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS
33
34 class CpsDataPersistenceServicePerfTest extends CpsPersistencePerfSpecBase {
35
36     @Autowired
37     CpsDataPersistenceService objectUnderTest
38
39     @Autowired
40     DataspaceRepository dataspaceRepository
41
42     @Autowired
43     AnchorRepository anchorRepository
44
45     @Autowired
46     FragmentRepository fragmentRepository
47
48     static def NUMBER_OF_CHILDREN = 200
49     static def NUMBER_OF_GRAND_CHILDREN = 50
50
51     @Sql([CLEAR_DATA, PERF_TEST_DATA])
52     def 'Create a node with many descendants (please note, subsequent tests depend on this running first).'() {
53         given: 'a node with a large number of descendants is created'
54             stopWatch.start()
55             createLineage(objectUnderTest, NUMBER_OF_CHILDREN, NUMBER_OF_GRAND_CHILDREN, false)
56             stopWatch.stop()
57             def setupDurationInMillis = stopWatch.getTotalTimeMillis()
58         and: 'setup duration is under 10 seconds'
59             recordAndAssertPerformance('Setup', 10000, setupDurationInMillis)
60     }
61
62     def 'Update data nodes with descendants'() {
63         given: 'a list of xpaths to data nodes with descendants (xpath for each child)'
64             def xpaths = (1..20).collect {
65                 "${PERF_TEST_PARENT}/perf-test-child-${it}".toString()
66             }
67         and: 'the correct number of data nodes are fetched'
68             def dataNodes = objectUnderTest.getDataNodesForMultipleXpaths(PERF_DATASPACE, PERF_ANCHOR, xpaths, INCLUDE_ALL_DESCENDANTS)
69             assert dataNodes.size() == 20
70             assert countDataNodes(dataNodes) == 20 + 20 * 50
71         when: 'the fragment entities are updated by the data nodes'
72             stopWatch.start()
73             objectUnderTest.updateDataNodesAndDescendants(PERF_DATASPACE, PERF_ANCHOR, dataNodes)
74             stopWatch.stop()
75             def updateDurationInMillis = stopWatch.getTotalTimeMillis()
76         then: 'update duration is under 600 milliseconds'
77             recordAndAssertPerformance('Update data nodes with descendants', 600, updateDurationInMillis)
78     }
79
80     def 'Update data nodes without descendants'() {
81         given: 'a list of xpaths to data nodes without descendants (xpath for each grandchild)'
82             def xpaths = []
83             for (int childIndex = 21; childIndex <= 40; childIndex++) {
84                 xpaths.addAll((1..50).collect {
85                     "${PERF_TEST_PARENT}/perf-test-child-${childIndex}/perf-test-grand-child-${it}".toString()
86                 })
87             }
88         and: 'the correct number of data nodes are fetched'
89             def dataNodes = objectUnderTest.getDataNodesForMultipleXpaths(PERF_DATASPACE, PERF_ANCHOR, xpaths, OMIT_DESCENDANTS)
90             assert dataNodes.size() == 20 * 50
91             assert countDataNodes(dataNodes) == 20 * 50
92         when: 'the fragment entities are updated by the data nodes'
93             stopWatch.start()
94             objectUnderTest.updateDataNodesAndDescendants(PERF_DATASPACE, PERF_ANCHOR, dataNodes)
95             stopWatch.stop()
96             def updateDurationInMillis = stopWatch.getTotalTimeMillis()
97         then: 'update duration is under 900 milliseconds'
98             recordAndAssertPerformance('Update data nodes without descendants', 900, updateDurationInMillis)
99     }
100 }