Create Base and Sample Performance Integration Tests
[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     static def TOTAL_NUMBER_OF_NODES = 1 + NUMBER_OF_CHILDREN + (NUMBER_OF_CHILDREN * NUMBER_OF_GRAND_CHILDREN)  //  Parent + Children +  Grand-children
51
52     @Sql([CLEAR_DATA, PERF_TEST_DATA])
53     def 'Create a node with many descendants (please note, subsequent tests depend on this running first).'() {
54         given: 'a node with a large number of descendants is created'
55             stopWatch.start()
56             createLineage(objectUnderTest, NUMBER_OF_CHILDREN, NUMBER_OF_GRAND_CHILDREN, false)
57             stopWatch.stop()
58             def setupDurationInMillis = stopWatch.getTotalTimeMillis()
59         and: 'setup duration is under 10 seconds'
60             recordAndAssertPerformance('Setup', 10000, setupDurationInMillis)
61     }
62
63     def 'Get data node with many descendants by xpath #scenario'() {
64         when: 'get parent is executed with all descendants'
65             stopWatch.start()
66             def result = objectUnderTest.getDataNodes(PERF_DATASPACE, PERF_ANCHOR, xpath, INCLUDE_ALL_DESCENDANTS)
67             stopWatch.stop()
68             def readDurationInMillis = stopWatch.getTotalTimeMillis()
69         then: 'read duration is under #allowedDuration milliseconds'
70             recordAndAssertPerformance("Get ${scenario}", allowedDuration, readDurationInMillis)
71         and: 'data node is returned with all the descendants populated'
72             assert countDataNodes(result[0]) == TOTAL_NUMBER_OF_NODES
73         where: 'the following xPaths are used'
74             scenario | xpath            || allowedDuration
75             'parent' | PERF_TEST_PARENT || 5000
76             'root'   | ''               || 500
77     }
78
79     def 'Query parent data node with many descendants by cps-path'() {
80         when: 'query is executed with all descendants'
81             stopWatch.start()
82             def result = objectUnderTest.queryDataNodes(PERF_DATASPACE, PERF_ANCHOR, '//perf-parent-1' , INCLUDE_ALL_DESCENDANTS)
83             stopWatch.stop()
84             def readDurationInMillis = stopWatch.getTotalTimeMillis()
85         then: 'read duration is under 500 milliseconds'
86             recordAndAssertPerformance('Query with many descendants', 500, readDurationInMillis)
87         and: 'data node is returned with all the descendants populated'
88             assert countDataNodes(result) == TOTAL_NUMBER_OF_NODES
89     }
90
91     def 'Performance of finding multiple xpaths'() {
92         when: 'we query for all grandchildren (except 1 for fun) with the new native method'
93             xpathsToAllGrandChildren.remove(0)
94             stopWatch.start()
95             def result = objectUnderTest.getDataNodesForMultipleXpaths(PERF_DATASPACE, PERF_ANCHOR, xpathsToAllGrandChildren, INCLUDE_ALL_DESCENDANTS)
96             stopWatch.stop()
97             def readDurationInMillis = stopWatch.getTotalTimeMillis()
98         then: 'the returned number of entities equal to the number of children * number of grandchildren'
99             assert result.size() == xpathsToAllGrandChildren.size()
100         and: 'it took less then 5000ms'
101             recordAndAssertPerformance('Find multiple xpaths', 5000, readDurationInMillis)
102     }
103
104     def 'Query many descendants by cps-path with #scenario'() {
105         when: 'query is executed with all descendants'
106             stopWatch.start()
107             def result = objectUnderTest.queryDataNodes(PERF_DATASPACE, PERF_ANCHOR,  '//perf-test-grand-child-1', descendantsOption)
108             stopWatch.stop()
109             def readDurationInMillis = stopWatch.getTotalTimeMillis()
110         then: 'read duration is under #allowedDuration milliseconds'
111             recordAndAssertPerformance("Query many descendants by cpspath (${scenario})", allowedDuration, readDurationInMillis)
112         and: 'data node is returned with all the descendants populated'
113             assert result.size() == NUMBER_OF_CHILDREN
114         where: 'the following options are used'
115             scenario                                        | descendantsOption        || allowedDuration
116             'omit descendants                             ' | OMIT_DESCENDANTS         || 150
117             'include descendants (although there are none)' | INCLUDE_ALL_DESCENDANTS  || 150
118     }
119
120     def 'Update data nodes with descendants'() {
121         given: 'a list of xpaths to data nodes with descendants (xpath for each child)'
122             def xpaths = (1..20).collect {
123                 "${PERF_TEST_PARENT}/perf-test-child-${it}".toString()
124             }
125         and: 'the correct number of data nodes are fetched'
126             def dataNodes = objectUnderTest.getDataNodesForMultipleXpaths(PERF_DATASPACE, PERF_ANCHOR, xpaths, INCLUDE_ALL_DESCENDANTS)
127             assert dataNodes.size() == 20
128             assert countDataNodes(dataNodes) == 20 + 20 * 50
129         when: 'the fragment entities are updated by the data nodes'
130             stopWatch.start()
131             objectUnderTest.updateDataNodesAndDescendants(PERF_DATASPACE, PERF_ANCHOR, dataNodes)
132             stopWatch.stop()
133             def updateDurationInMillis = stopWatch.getTotalTimeMillis()
134         then: 'update duration is under 900 milliseconds'
135             recordAndAssertPerformance('Update data nodes with descendants', 900, updateDurationInMillis)
136     }
137
138     def 'Update data nodes without descendants'() {
139         given: 'a list of xpaths to data nodes without descendants (xpath for each grandchild)'
140             def xpaths = []
141             for (int childIndex = 21; childIndex <= 40; childIndex++) {
142                 xpaths.addAll((1..50).collect {
143                     "${PERF_TEST_PARENT}/perf-test-child-${childIndex}/perf-test-grand-child-${it}".toString()
144                 })
145             }
146         and: 'the correct number of data nodes are fetched'
147             def dataNodes = objectUnderTest.getDataNodesForMultipleXpaths(PERF_DATASPACE, PERF_ANCHOR, xpaths, OMIT_DESCENDANTS)
148             assert dataNodes.size() == 20 * 50
149             assert countDataNodes(dataNodes) == 20 * 50
150         when: 'the fragment entities are updated by the data nodes'
151             stopWatch.start()
152             objectUnderTest.updateDataNodesAndDescendants(PERF_DATASPACE, PERF_ANCHOR, dataNodes)
153             stopWatch.stop()
154             def updateDurationInMillis = stopWatch.getTotalTimeMillis()
155         then: 'update duration is under 900 milliseconds'
156             recordAndAssertPerformance('Update data nodes without descendants', 900, updateDurationInMillis)
157     }
158 }