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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.spi.performance
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.onap.cps.spi.repository.AnchorRepository
29 import org.onap.cps.spi.repository.DataspaceRepository
30 import org.onap.cps.spi.repository.FragmentRepository
31 import org.springframework.beans.factory.annotation.Autowired
32 import org.springframework.test.context.jdbc.Sql
34 import java.util.concurrent.TimeUnit
36 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
37 import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS
39 class CpsDataPersistenceServicePerfTest extends CpsPersistenceSpecBase {
41 static final String PERF_TEST_DATA = '/data/perf-test.sql'
44 CpsDataPersistenceService objectUnderTest
47 DataspaceRepository dataspaceRepository
50 AnchorRepository anchorRepository
53 FragmentRepository fragmentRepository
55 static def PERF_TEST_PARENT = '/perf-parent-1'
56 static def NUMBER_OF_CHILDREN = 200
57 static def NUMBER_OF_GRAND_CHILDREN = 50
58 static def TOTAL_NUMBER_OF_NODES = 1 + NUMBER_OF_CHILDREN + (NUMBER_OF_CHILDREN * NUMBER_OF_GRAND_CHILDREN) // Parent + Children + Grand-children
59 static def ALLOWED_SETUP_TIME_MS = TimeUnit.SECONDS.toMillis(10)
60 static def ALLOWED_READ_TIME_AL_NODES_MS = 500
62 def stopWatch = new StopWatch()
63 def readStopWatch = new StopWatch()
64 static def xpathsToAllGrandChildren = []
66 @Sql([CLEAR_DATA, PERF_TEST_DATA])
67 def 'Create a node with many descendants (please note, subsequent tests depend on this running first).'() {
68 given: 'a node with a large number of descendants is created'
72 def setupDurationInMillis = stopWatch.getTotalTimeMillis()
73 and: 'setup duration is under #ALLOWED_SETUP_TIME_MS milliseconds'
74 assert setupDurationInMillis < ALLOWED_SETUP_TIME_MS
77 def 'Get data node with many descendants by xpath #scenario'() {
78 when: 'get parent is executed with all descendants'
80 def result = objectUnderTest.getDataNode('PERF-DATASPACE', 'PERF-ANCHOR', xpath, INCLUDE_ALL_DESCENDANTS)
82 def readDurationInMillis = stopWatch.getTotalTimeMillis()
83 then: 'read duration is under 500 milliseconds'
84 assert readDurationInMillis < ALLOWED_READ_TIME_AL_NODES_MS
85 and: 'data node is returned with all the descendants populated'
86 assert countDataNodes(result) == TOTAL_NUMBER_OF_NODES
87 where: 'the following xPaths are used'
89 'parent' || PERF_TEST_PARENT
93 def 'Query parent data node with many descendants by cps-path'() {
94 when: 'query is executed with all descendants'
96 def result = objectUnderTest.queryDataNodes('PERF-DATASPACE', 'PERF-ANCHOR', '//perf-parent-1' , INCLUDE_ALL_DESCENDANTS)
98 def readDurationInMillis = stopWatch.getTotalTimeMillis()
99 then: 'read duration is under 500 milliseconds'
100 assert readDurationInMillis < ALLOWED_READ_TIME_AL_NODES_MS
101 and: 'data node is returned with all the descendants populated'
102 assert countDataNodes(result) == TOTAL_NUMBER_OF_NODES
105 def 'Performance of finding multiple xpaths'() {
106 when: 'we query for all grandchildren (except 1 for fun) with the new native method'
107 xpathsToAllGrandChildren.remove(0)
108 readStopWatch.start()
109 def result = objectUnderTest.getDataNodes('PERF-DATASPACE', 'PERF-ANCHOR', xpathsToAllGrandChildren, INCLUDE_ALL_DESCENDANTS)
111 def readDurationInMillis = readStopWatch.getTotalTimeMillis()
112 then: 'the returned number of entities equal to the number of children * number of grandchildren'
113 assert result.size() == xpathsToAllGrandChildren.size()
114 and: 'it took less then 4000ms'
115 assert readDurationInMillis < 4000
118 def 'Query many descendants by cps-path with #scenario'() {
119 when: 'query is executed with all descendants'
121 def result = objectUnderTest.queryDataNodes('PERF-DATASPACE', 'PERF-ANCHOR', '//perf-test-grand-child-1', descendantsOption)
123 def readDurationInMillis = stopWatch.getTotalTimeMillis()
124 then: 'read duration is under 500 milliseconds'
125 assert readDurationInMillis < alowedDuration
126 and: 'data node is returned with all the descendants populated'
127 assert result.size() == NUMBER_OF_CHILDREN
128 where: 'the following options are used'
129 scenario | descendantsOption || alowedDuration
130 'omit descendants ' | OMIT_DESCENDANTS || 150
131 'include descendants (although there are none)' | INCLUDE_ALL_DESCENDANTS || 150
134 def 'Delete 50 grandchildren (that have no descendants)'() {
135 when: 'target nodes are deleted'
137 (1..NUMBER_OF_GRAND_CHILDREN).each {
138 def grandchildPath = "${PERF_TEST_PARENT}/perf-test-child-1/perf-test-grand-child-${it}".toString();
139 objectUnderTest.deleteDataNode('PERF-DATASPACE', 'PERF-ANCHOR', grandchildPath)
142 def deleteDurationInMillis = stopWatch.getTotalTimeMillis()
143 then: 'delete duration is under 1000 milliseconds'
144 assert deleteDurationInMillis < 1000
147 def 'Delete 5 children with grandchildren'() {
148 when: 'child nodes are deleted'
151 def childPath = "${PERF_TEST_PARENT}/perf-test-child-${it}".toString();
152 objectUnderTest.deleteDataNode('PERF-DATASPACE', 'PERF-ANCHOR', childPath)
155 def deleteDurationInMillis = stopWatch.getTotalTimeMillis()
156 then: 'delete duration is under 10000 milliseconds'
157 assert deleteDurationInMillis < 10000
160 def 'Delete 1 large data node with many descendants'() {
161 when: 'parent node is deleted'
163 objectUnderTest.deleteDataNode('PERF-DATASPACE', 'PERF-ANCHOR', PERF_TEST_PARENT)
165 def deleteDurationInMillis = stopWatch.getTotalTimeMillis()
166 then: 'delete duration is under 5000 milliseconds'
167 assert deleteDurationInMillis < 5000
170 def createLineage() {
171 (1..NUMBER_OF_CHILDREN).each {
172 def childName = "perf-test-child-${it}".toString()
173 def child = goForthAndMultiply(PERF_TEST_PARENT, childName)
174 objectUnderTest.addChildDataNode('PERF-DATASPACE', 'PERF-ANCHOR', PERF_TEST_PARENT, child)
178 def goForthAndMultiply(parentXpath, childName) {
179 def grandChildren = []
180 (1..NUMBER_OF_GRAND_CHILDREN).each {
181 def grandChild = new DataNodeBuilder().withXpath("${parentXpath}/${childName}/perf-test-grand-child-${it}").build()
182 xpathsToAllGrandChildren.add(grandChild.xpath)
183 grandChildren.add(grandChild)
185 return new DataNodeBuilder().withXpath("${parentXpath}/${childName}").withChildDataNodes(grandChildren).build()
188 def countDataNodes(dataNodes) {
190 for (DataNode parent : dataNodes) {
191 for (DataNode child : parent.childDataNodes) {
192 nodeCount = nodeCount + (countDataNodes(child))