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
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.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
31 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
32 import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS
34 class CpsDataPersistenceServicePerfTest extends CpsPersistencePerfSpecBase {
37 CpsDataPersistenceService objectUnderTest
40 DataspaceRepository dataspaceRepository
43 AnchorRepository anchorRepository
46 FragmentRepository fragmentRepository
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
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'
56 createLineage(objectUnderTest, NUMBER_OF_CHILDREN, NUMBER_OF_GRAND_CHILDREN, false)
58 def setupDurationInMillis = stopWatch.getTotalTimeMillis()
59 and: 'setup duration is under 10 seconds'
60 recordAndAssertPerformance('Setup', 10000, setupDurationInMillis)
63 def 'Get data node with many descendants by xpath #scenario'() {
64 when: 'get parent is executed with all descendants'
66 def result = objectUnderTest.getDataNodes(PERF_DATASPACE, PERF_ANCHOR, xpath, INCLUDE_ALL_DESCENDANTS)
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 || 3500
79 def 'Query parent data node with many descendants by cps-path'() {
80 when: 'query is executed with all descendants'
82 def result = objectUnderTest.queryDataNodes(PERF_DATASPACE, PERF_ANCHOR, '//perf-parent-1' , INCLUDE_ALL_DESCENDANTS)
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
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)
95 def result = objectUnderTest.getDataNodesForMultipleXpaths(PERF_DATASPACE, PERF_ANCHOR, xpathsToAllGrandChildren, INCLUDE_ALL_DESCENDANTS)
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 3000ms'
101 recordAndAssertPerformance('Find multiple xpaths', 3000, readDurationInMillis)
104 def 'Query many descendants by cps-path with #scenario'() {
105 when: 'query is executed with all descendants'
107 def result = objectUnderTest.queryDataNodes(PERF_DATASPACE, PERF_ANCHOR, '//perf-test-grand-child-1', descendantsOption)
109 def readDurationInMillis = stopWatch.getTotalTimeMillis()
110 then: 'read duration is under #allowedDuration milliseconds'
111 assert readDurationInMillis < allowedDuration
112 recordAndAssertPerformance("Query many descendants by cpspath (${scenario})", allowedDuration, readDurationInMillis)
113 and: 'data node is returned with all the descendants populated'
114 assert result.size() == NUMBER_OF_CHILDREN
115 where: 'the following options are used'
116 scenario | descendantsOption || allowedDuration
117 'omit descendants ' | OMIT_DESCENDANTS || 150
118 'include descendants (although there are none)' | INCLUDE_ALL_DESCENDANTS || 150
121 def 'Update data nodes with descendants'() {
122 given: 'a list of xpaths to data nodes with descendants (xpath for each child)'
123 def xpaths = (1..20).collect {
124 "${PERF_TEST_PARENT}/perf-test-child-${it}".toString()
126 and: 'the correct number of data nodes are fetched'
127 def dataNodes = objectUnderTest.getDataNodesForMultipleXpaths(PERF_DATASPACE, PERF_ANCHOR, xpaths, INCLUDE_ALL_DESCENDANTS)
128 assert dataNodes.size() == 20
129 assert countDataNodes(dataNodes) == 20 + 20 * 50
130 when: 'the fragment entities are updated by the data nodes'
132 objectUnderTest.updateDataNodesAndDescendants(PERF_DATASPACE, PERF_ANCHOR, dataNodes)
134 def updateDurationInMillis = stopWatch.getTotalTimeMillis()
135 then: 'update duration is under 600 milliseconds'
136 recordAndAssertPerformance('Update data nodes with descendants', 600, updateDurationInMillis)
139 def 'Update data nodes without descendants'() {
140 given: 'a list of xpaths to data nodes without descendants (xpath for each grandchild)'
142 for (int childIndex = 21; childIndex <= 40; childIndex++) {
143 xpaths.addAll((1..50).collect {
144 "${PERF_TEST_PARENT}/perf-test-child-${childIndex}/perf-test-grand-child-${it}".toString()
147 and: 'the correct number of data nodes are fetched'
148 def dataNodes = objectUnderTest.getDataNodesForMultipleXpaths(PERF_DATASPACE, PERF_ANCHOR, xpaths, OMIT_DESCENDANTS)
149 assert dataNodes.size() == 20 * 50
150 assert countDataNodes(dataNodes) == 20 * 50
151 when: 'the fragment entities are updated by the data nodes'
153 objectUnderTest.updateDataNodesAndDescendants(PERF_DATASPACE, PERF_ANCHOR, dataNodes)
155 def updateDurationInMillis = stopWatch.getTotalTimeMillis()
156 then: 'update duration is under 1400 milliseconds'
157 recordAndAssertPerformance('Update data nodes without descendants', 1400, updateDurationInMillis)