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.springframework.util.StopWatch
25 import org.onap.cps.spi.CpsDataPersistenceService
26 import org.onap.cps.spi.repository.AnchorRepository
27 import org.onap.cps.spi.repository.DataspaceRepository
28 import org.onap.cps.spi.repository.FragmentRepository
29 import org.springframework.beans.factory.annotation.Autowired
30 import org.springframework.test.context.jdbc.Sql
32 import java.util.concurrent.TimeUnit
34 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
35 import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS
37 class CpsDataPersistenceServicePerfTest extends CpsPersistencePerfSpecBase {
40 CpsDataPersistenceService objectUnderTest
43 DataspaceRepository dataspaceRepository
46 AnchorRepository anchorRepository
49 FragmentRepository fragmentRepository
51 static def NUMBER_OF_CHILDREN = 200
52 static def NUMBER_OF_GRAND_CHILDREN = 50
53 static def TOTAL_NUMBER_OF_NODES = 1 + NUMBER_OF_CHILDREN + (NUMBER_OF_CHILDREN * NUMBER_OF_GRAND_CHILDREN) // Parent + Children + Grand-children
54 static def ALLOWED_SETUP_TIME_MS = TimeUnit.SECONDS.toMillis(10)
55 static def ALLOWED_READ_TIME_AL_NODES_MS = 500
57 def stopWatch = new StopWatch()
58 def readStopWatch = new StopWatch()
60 @Sql([CLEAR_DATA, PERF_TEST_DATA])
61 def 'Create a node with many descendants (please note, subsequent tests depend on this running first).'() {
62 given: 'a node with a large number of descendants is created'
64 createLineage(objectUnderTest, NUMBER_OF_CHILDREN, NUMBER_OF_GRAND_CHILDREN, false)
66 def setupDurationInMillis = stopWatch.getTotalTimeMillis()
67 and: 'setup duration is under #ALLOWED_SETUP_TIME_MS milliseconds'
68 assert setupDurationInMillis < ALLOWED_SETUP_TIME_MS
71 def 'Get data node with many descendants by xpath #scenario'() {
72 when: 'get parent is executed with all descendants'
74 def result = objectUnderTest.getDataNode(PERF_DATASPACE, PERF_ANCHOR, xpath, INCLUDE_ALL_DESCENDANTS)
76 def readDurationInMillis = stopWatch.getTotalTimeMillis()
77 then: 'read duration is under 500 milliseconds'
78 assert readDurationInMillis < ALLOWED_READ_TIME_AL_NODES_MS
79 and: 'data node is returned with all the descendants populated'
80 assert countDataNodes(result) == TOTAL_NUMBER_OF_NODES
81 where: 'the following xPaths are used'
83 'parent' || PERF_TEST_PARENT
87 def 'Query parent data node with many descendants by cps-path'() {
88 when: 'query is executed with all descendants'
90 def result = objectUnderTest.queryDataNodes(PERF_DATASPACE, PERF_ANCHOR, '//perf-parent-1' , INCLUDE_ALL_DESCENDANTS)
92 def readDurationInMillis = stopWatch.getTotalTimeMillis()
93 then: 'read duration is under 500 milliseconds'
94 assert readDurationInMillis < ALLOWED_READ_TIME_AL_NODES_MS
95 and: 'data node is returned with all the descendants populated'
96 assert countDataNodes(result) == TOTAL_NUMBER_OF_NODES
99 def 'Performance of finding multiple xpaths'() {
100 when: 'we query for all grandchildren (except 1 for fun) with the new native method'
101 xpathsToAllGrandChildren.remove(0)
102 readStopWatch.start()
103 def result = objectUnderTest.getDataNodes(PERF_DATASPACE, PERF_ANCHOR, xpathsToAllGrandChildren, INCLUDE_ALL_DESCENDANTS)
105 def readDurationInMillis = readStopWatch.getTotalTimeMillis()
106 then: 'the returned number of entities equal to the number of children * number of grandchildren'
107 assert result.size() == xpathsToAllGrandChildren.size()
108 and: 'it took less then 4000ms'
109 assert readDurationInMillis < 4000
112 def 'Query many descendants by cps-path with #scenario'() {
113 when: 'query is executed with all descendants'
115 def result = objectUnderTest.queryDataNodes(PERF_DATASPACE, PERF_ANCHOR, '//perf-test-grand-child-1', descendantsOption)
117 def readDurationInMillis = stopWatch.getTotalTimeMillis()
118 then: 'read duration is under 500 milliseconds'
119 assert readDurationInMillis < alowedDuration
120 and: 'data node is returned with all the descendants populated'
121 assert result.size() == NUMBER_OF_CHILDREN
122 where: 'the following options are used'
123 scenario | descendantsOption || alowedDuration
124 'omit descendants ' | OMIT_DESCENDANTS || 150
125 'include descendants (although there are none)' | INCLUDE_ALL_DESCENDANTS || 150