2 * ============LICENSE_START=======================================================
3 * Copyright (C) 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.impl
23 import org.onap.cps.spi.model.DataNode
24 import org.onap.cps.spi.model.DataNodeBuilder
25 import org.springframework.util.StopWatch
27 class CpsPersistencePerfSpecBase extends CpsPersistenceSpecBase {
29 static final String PERF_TEST_DATA = '/data/perf-test.sql'
30 static final String PERF_DATASPACE = 'PERF-DATASPACE'
31 static final String PERF_ANCHOR = 'PERF-ANCHOR'
32 static final String PERF_TEST_PARENT = '/perf-parent-1'
34 static def xpathsToAllGrandChildren = []
36 static def PERFORMANCE_RECORD = []
38 def stopWatch = new StopWatch()
41 println('#############################################################################')
42 println('## P E R F O R M A N C E T E S T R E S U L T S ##')
43 println('#############################################################################')
44 PERFORMANCE_RECORD.sort().each { println(it) }
45 PERFORMANCE_RECORD.clear()
48 def createLineage(cpsDataPersistenceService, numberOfChildren, numberOfGrandChildren, createLists) {
49 xpathsToAllGrandChildren = []
50 (1..numberOfChildren).each {
52 def xpathFormat = "${PERF_TEST_PARENT}/perf-test-list-${it}[@key='%d']"
53 def listElements = goForthAndMultiply(xpathFormat, numberOfGrandChildren)
54 cpsDataPersistenceService.addListElements(PERF_DATASPACE, PERF_ANCHOR, PERF_TEST_PARENT, listElements)
56 def xpathFormat = "${PERF_TEST_PARENT}/perf-test-child-${it}/perf-test-grand-child-%d"
57 def grandChildren = goForthAndMultiply(xpathFormat, numberOfGrandChildren)
58 def child = new DataNodeBuilder()
59 .withXpath("${PERF_TEST_PARENT}/perf-test-child-${it}")
60 .withChildDataNodes(grandChildren)
62 cpsDataPersistenceService.addChildDataNode(PERF_DATASPACE, PERF_ANCHOR, PERF_TEST_PARENT, child)
67 def goForthAndMultiply(xpathFormat, numberOfGrandChildren) {
68 def grandChildren = []
69 (1..numberOfGrandChildren).each {
70 def xpath = String.format(xpathFormat as String, it)
71 def grandChild = new DataNodeBuilder().withXpath(xpath).build()
72 xpathsToAllGrandChildren.add(grandChild.xpath)
73 grandChildren.add(grandChild)
78 def countDataNodes(dataNodes) {
80 for (DataNode parent : dataNodes) {
81 for (DataNode child : parent.childDataNodes) {
82 nodeCount = nodeCount + (countDataNodes(child))
88 def recordAndAssertPerformance(String shortTitle, thresholdInMs, recordedTimeInMs) {
89 def pass = recordedTimeInMs <= thresholdInMs
90 if (shortTitle.length()>40) {
91 shortTitle = shortTitle.substring(0,40)
93 def record = String.format('%2d.%-40s limit%,7d took %,7d ms ', PERFORMANCE_RECORD.size()+1, shortTitle, thresholdInMs, recordedTimeInMs)
94 record += pass?'PASS':'FAIL'
95 PERFORMANCE_RECORD.add(record)
96 assert recordedTimeInMs <= thresholdInMs