Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Performance fix for create new value in prism container (+test)
- Loading branch information
Showing
6 changed files
with
218 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
infra/prism/src/test/java/com/evolveum/midpoint/prism/PerfRecorder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* Copyright (c) 2016 Evolveum | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.evolveum.midpoint.prism; | ||
|
||
import org.testng.AssertJUnit; | ||
|
||
/** | ||
* @author semancik | ||
* | ||
*/ | ||
public class PerfRecorder { | ||
|
||
private String name; | ||
private int count = 0; | ||
private Double min = null; | ||
private Double max = null; | ||
private Double sum = 0D; | ||
|
||
public PerfRecorder(String name) { | ||
super(); | ||
this.name = name; | ||
} | ||
|
||
public void record(int index, Double value) { | ||
sum += value; | ||
count ++; | ||
if (min == null || value < min) { | ||
min = value; | ||
} | ||
if (max == null || value > max) { | ||
max = value; | ||
} | ||
} | ||
|
||
public int getCount() { | ||
return count; | ||
} | ||
|
||
public Double getMin() { | ||
return min; | ||
} | ||
|
||
public Double getMax() { | ||
return max; | ||
} | ||
|
||
public Double getSum() { | ||
return sum; | ||
} | ||
|
||
public double getAverage() { | ||
return sum/count; | ||
} | ||
|
||
public void assertAverageBelow(double expected) { | ||
AssertJUnit.assertTrue(name+ ": Expected average below "+expected+" but was "+getAverage(), getAverage() < expected); | ||
} | ||
|
||
public void assertMaxBelow(double expected) { | ||
AssertJUnit.assertTrue(name+ ": Expected maximum below "+expected+" but was "+max, max < expected); | ||
} | ||
|
||
|
||
public String dump() { | ||
return name + ": min / avg / max = "+min+" / "+getAverage()+" / "+max + " (sum="+sum+", count="+count+")"; | ||
} | ||
|
||
} |
116 changes: 116 additions & 0 deletions
116
infra/prism/src/test/java/com/evolveum/midpoint/prism/TestPerformance.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/* | ||
* Copyright (c) 2016 Evolveum | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.evolveum.midpoint.prism; | ||
|
||
import static com.evolveum.midpoint.prism.PrismInternalTestUtil.*; | ||
import static org.testng.AssertJUnit.assertNotNull; | ||
import static org.testng.AssertJUnit.assertTrue; | ||
import static org.testng.AssertJUnit.assertEquals; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
import javax.xml.namespace.QName; | ||
import javax.xml.transform.dom.DOMSource; | ||
import javax.xml.validation.Schema; | ||
import javax.xml.validation.Validator; | ||
|
||
import org.testng.annotations.BeforeSuite; | ||
import org.testng.annotations.Test; | ||
import org.w3c.dom.Document; | ||
import org.xml.sax.SAXException; | ||
|
||
import com.evolveum.midpoint.prism.foo.ActivationType; | ||
import com.evolveum.midpoint.prism.foo.AssignmentType; | ||
import com.evolveum.midpoint.prism.foo.UserType; | ||
import com.evolveum.midpoint.prism.util.PrismAsserts; | ||
import com.evolveum.midpoint.util.DOMUtil; | ||
import com.evolveum.midpoint.util.PrettyPrinter; | ||
import com.evolveum.midpoint.util.exception.SchemaException; | ||
|
||
/** | ||
* @author semancik | ||
* | ||
*/ | ||
public class TestPerformance { | ||
|
||
private static final int ITERATIONS = 10000; | ||
|
||
|
||
@BeforeSuite | ||
public void setupDebug() { | ||
PrettyPrinter.setDefaultNamespacePrefix(DEFAULT_NAMESPACE_PREFIX); | ||
} | ||
|
||
/** | ||
* Construct object with schema. Starts by instantiating a definition and working downwards. | ||
* All the items in the object should have proper definition. | ||
*/ | ||
@Test | ||
public void testPerfContainerNewValue() throws Exception { | ||
final String TEST_NAME = "testPerfContainerNewValue"; | ||
PrismInternalTestUtil.displayTestTitle(TEST_NAME); | ||
|
||
// GIVEN | ||
PrismContext ctx = constructInitializedPrismContext(); | ||
PrismObjectDefinition<UserType> userDefinition = getFooSchema(ctx).findObjectDefinitionByElementName(new QName(NS_FOO,"user")); | ||
PrismObject<UserType> user = userDefinition.instantiate(); | ||
PrismContainer<AssignmentType> assignmentContainer = user.findOrCreateContainer(UserType.F_ASSIGNMENT); | ||
PerfRecorder recorderCreateNewValue = new PerfRecorder("createNewValue"); | ||
PerfRecorder recorderFindOrCreateProperty = new PerfRecorder("findOrCreateProperty"); | ||
PerfRecorder recorderSetRealValue = new PerfRecorder("setRealValue"); | ||
|
||
// WHEN | ||
for (int i=0; i < ITERATIONS; i++) { | ||
long tsStart = System.nanoTime(); | ||
|
||
PrismContainerValue<AssignmentType> newValue = assignmentContainer.createNewValue(); | ||
|
||
long ts1 = System.nanoTime(); | ||
|
||
PrismProperty<String> descriptionProperty = newValue.findOrCreateProperty(AssignmentType.F_DESCRIPTION); | ||
|
||
long ts2 = System.nanoTime(); | ||
|
||
descriptionProperty.setRealValue("ass "+i); | ||
|
||
long tsEnd = System.nanoTime(); | ||
|
||
recorderCreateNewValue.record(i, ((double)(ts1 - tsStart))/1000000); | ||
recorderFindOrCreateProperty.record(i, ((double)(ts2 - ts1))/1000000); | ||
recorderSetRealValue.record(i, ((double)(tsEnd - ts2))/1000000); | ||
|
||
System.out.println("Run "+i+": total "+(((double)(tsEnd - tsStart))/1000000)+"ms"); | ||
} | ||
|
||
// THEN | ||
System.out.println(recorderCreateNewValue.dump()); | ||
System.out.println(recorderFindOrCreateProperty.dump()); | ||
System.out.println(recorderCreateNewValue.dump()); | ||
|
||
// Do not assert maximum here. The maximum values may jump around | ||
// quite wildly (e.g. because of garbage collector runs?) | ||
recorderCreateNewValue.assertAverageBelow(0.05D); | ||
|
||
recorderFindOrCreateProperty.assertAverageBelow(0.1D); | ||
|
||
recorderCreateNewValue.assertAverageBelow(0.05D); | ||
|
||
System.out.println("User:"); | ||
System.out.println(user.debugDump()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters