910d8a46014f642711bad70dcadb73edc7d14581
[cps.git] / cps-ri / src / test / groovy / org / onap / cps / spi / performance / CpsDataPersistenceServicePerfTest.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.springframework.util.StopWatch
24 import org.onap.cps.spi.CpsDataPersistenceService
25 import org.onap.cps.spi.impl.CpsPersistenceSpecBase
26 import org.onap.cps.spi.model.DataNode
27 import org.onap.cps.spi.model.DataNodeBuilder
28 import org.springframework.beans.factory.annotation.Autowired
29 import org.springframework.test.context.jdbc.Sql
30
31 import java.util.concurrent.TimeUnit
32
33 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
34 import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS
35
36 class CpsDataPersistenceServicePerfTest extends CpsPersistenceSpecBase {
37
38     static final String PERF_TEST_DATA = '/data/perf-test.sql'
39
40     @Autowired
41     CpsDataPersistenceService objectUnderTest
42
43     static def PERF_TEST_PARENT = '/perf-parent-1'
44     static def NUMBER_OF_CHILDREN = 200
45     static def NUMBER_OF_GRAND_CHILDREN = 50
46     static def TOTAL_NUMBER_OF_NODES = 1 + NUMBER_OF_CHILDREN + (NUMBER_OF_CHILDREN * NUMBER_OF_GRAND_CHILDREN)  //  Parent + Children +  Grand-children
47     static def ALLOWED_SETUP_TIME_MS = TimeUnit.SECONDS.toMillis(10)
48     static def ALLOWED_READ_TIME_AL_NODES_MS = 500
49
50     def stopWatch = new StopWatch()
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()
57             stopWatch.stop()
58             def setupDurationInMillis = stopWatch.getTotalTimeMillis()
59         and: 'setup duration is under #ALLOWED_SETUP_TIME_MS milliseconds'
60             assert setupDurationInMillis < ALLOWED_SETUP_TIME_MS
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.getDataNode('PERF-DATASPACE', 'PERF-ANCHOR', xpath, INCLUDE_ALL_DESCENDANTS)
67             stopWatch.stop()
68             def readDurationInMillis = stopWatch.getTotalTimeMillis()
69         then: 'read duration is under 500 milliseconds'
70             assert readDurationInMillis < ALLOWED_READ_TIME_AL_NODES_MS
71         and: 'data node is returned with all the descendants populated'
72             assert countDataNodes(result) == TOTAL_NUMBER_OF_NODES
73         where: 'the following xPaths are used'
74             scenario || xpath
75             'parent' || PERF_TEST_PARENT
76             'root'   || ''
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             assert readDurationInMillis < ALLOWED_READ_TIME_AL_NODES_MS
87         and: 'data node is returned with all the descendants populated'
88             assert countDataNodes(result) == TOTAL_NUMBER_OF_NODES
89     }
90
91     def 'Query many descendants by cps-path with #scenario'() {
92         when: 'query is executed with all descendants'
93             stopWatch.start()
94             def result = objectUnderTest.queryDataNodes('PERF-DATASPACE', 'PERF-ANCHOR',  '//perf-test-grand-child-1', descendantsOption)
95             stopWatch.stop()
96             def readDurationInMillis = stopWatch.getTotalTimeMillis()
97         then: 'read duration is under 500 milliseconds'
98             assert readDurationInMillis < alowedDuration
99         and: 'data node is returned with all the descendants populated'
100             assert result.size() == NUMBER_OF_CHILDREN
101         where: 'the following options are used'
102             scenario                                        | descendantsOption        || alowedDuration
103             'omit descendants                             ' | OMIT_DESCENDANTS         || 150
104             'include descendants (although there are none)' | INCLUDE_ALL_DESCENDANTS  || 150
105     }
106
107     def 'Delete 50 grandchildren (that have no descendants)'() {
108         when: 'target nodes are deleted'
109             stopWatch.start()
110             (1..50).each {
111                 def grandchildPath = "${PERF_TEST_PARENT}/perf-test-child-1/perf-test-grand-child-${it}".toString();
112                 objectUnderTest.deleteDataNode('PERF-DATASPACE', 'PERF-ANCHOR', grandchildPath)
113             }
114             stopWatch.stop()
115             def deleteDurationInMillis = stopWatch.getTotalTimeMillis()
116         then: 'delete duration is under 1000 milliseconds'
117             assert deleteDurationInMillis < 1000
118     }
119
120     def 'Delete 5 children with grandchildren'() {
121         when: 'child nodes are deleted'
122             stopWatch.start()
123             (1..5).each {
124                 def childPath = "${PERF_TEST_PARENT}/perf-test-child-${it}".toString();
125                 objectUnderTest.deleteDataNode('PERF-DATASPACE', 'PERF-ANCHOR', childPath)
126             }
127             stopWatch.stop()
128             def deleteDurationInMillis = stopWatch.getTotalTimeMillis()
129         then: 'delete duration is under 10000 milliseconds'
130             assert deleteDurationInMillis < 10000
131     }
132
133     def 'Delete 1 large data node with many descendants'() {
134         when: 'parent node is deleted'
135             stopWatch.start()
136             objectUnderTest.deleteDataNode('PERF-DATASPACE', 'PERF-ANCHOR', PERF_TEST_PARENT)
137             stopWatch.stop()
138             def deleteDurationInMillis = stopWatch.getTotalTimeMillis()
139         then: 'delete duration is under 5000 milliseconds'
140             assert deleteDurationInMillis < 5000
141     }
142
143     def createLineage() {
144         (1..NUMBER_OF_CHILDREN).each {
145             def childName = "perf-test-child-${it}".toString()
146             def child = goForthAndMultiply(PERF_TEST_PARENT, childName)
147             objectUnderTest.addChildDataNode('PERF-DATASPACE', 'PERF-ANCHOR', PERF_TEST_PARENT, child)
148         }
149     }
150
151     def goForthAndMultiply(parentXpath, childName) {
152         def grandChildren = []
153         (1..NUMBER_OF_GRAND_CHILDREN).each {
154             def grandChild = new DataNodeBuilder().withXpath("${parentXpath}/${childName}/perf-test-grand-child-${it}").build()
155             grandChildren.add(grandChild)
156         }
157         return new DataNodeBuilder().withXpath("${parentXpath}/${childName}").withChildDataNodes(grandChildren).build()
158     }
159
160     def countDataNodes(dataNodes) {
161         int nodeCount = 1
162         for (DataNode parent : dataNodes) {
163             for (DataNode child : parent.childDataNodes) {
164                 nodeCount = nodeCount + (countDataNodes(child))
165             }
166         }
167         return nodeCount
168     }
169 }