Skip to content

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
skublik committed Nov 10, 2020
2 parents aeeb0f4 + bebf1ac commit c4a9782
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 71 deletions.
Expand Up @@ -9,17 +9,15 @@
import static com.evolveum.midpoint.prism.util.PrismTestUtil.getPrismContext;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.evolveum.midpoint.prism.impl.match.MatchingRuleRegistryFactory;
import com.evolveum.midpoint.prism.match.MatchingRuleRegistry;

import org.javasimon.Split;
import org.javasimon.Stopwatch;
import org.jetbrains.annotations.NotNull;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeSuite;
import org.xml.sax.SAXException;

Expand All @@ -29,13 +27,14 @@
import com.evolveum.midpoint.schema.constants.MidPointConstants;
import com.evolveum.midpoint.schema.internals.InternalsConfig;
import com.evolveum.midpoint.tools.testng.AbstractUnitTest;
import com.evolveum.midpoint.tools.testng.PerformanceTestMixin;
import com.evolveum.midpoint.util.CheckedProducer;
import com.evolveum.midpoint.util.PrettyPrinter;
import com.evolveum.midpoint.util.exception.CommonException;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.xml.ns._public.common.common_3.UserType;

public class AbstractSchemaPerformanceTest extends AbstractUnitTest {
public class AbstractSchemaPerformanceTest extends AbstractUnitTest implements PerformanceTestMixin {

protected static final String LABEL = "new-mapxnode";

Expand All @@ -60,53 +59,53 @@ public void setup() throws SchemaException, SAXException, IOException {
assert !InternalsConfig.isConsistencyChecks();
}

protected void measure(String label, CheckedProducer<?> producer) throws CommonException, IOException {
measure(label, producer, DEFAULT_EXECUTION, DEFAULT_REPEATS);
@BeforeClass
@Override
public void initTestMonitor() {
PerformanceTestMixin.super.initTestMonitor();
}

protected void measure(String label, String note, CheckedProducer<?> producer) throws CommonException, IOException {
measure(label, note, producer, DEFAULT_EXECUTION, DEFAULT_REPEATS);
}

protected void measure(String label, CheckedProducer<?> producer, long executionTime, int repeats) throws CommonException, IOException {
List<Double> times = new ArrayList<>();
protected void measure(String label, String note, CheckedProducer<?> producer, long executionTime, int repeats) throws CommonException, IOException {
Stopwatch watch = stopwatch(label, note);
for (int i = 0; i < repeats; i++) {
double micros = measureSingle(label, producer, executionTime);
times.add(micros);
}
double micros = measureSingle(label, producer, executionTime, watch);

PrintWriter resultsWriter = new PrintWriter(new FileWriter(RESULTS_FILE, true));
double min = times.stream().min(Double::compareTo).orElse(0.0);
double max = times.stream().max(Double::compareTo).orElse(0.0);
double sum = times.stream().mapToDouble(Double::doubleValue).sum();
double avg = sum / repeats;
double avg2 = (sum - min - max) / (repeats - 2);
resultsWriter.print(runId + ";" + new Date() + ";" + LABEL + ";" + label + ";" + executionTime + ";" + repeats + ";" + avg2 + ";" + avg + ";" + min + ";" + max);
for (Double time : times) {
resultsWriter.print(";" + time);
}
resultsWriter.println();
resultsWriter.close();

System.out.println(label + ": Average without the best and the worst = " + avg2);
}

protected double measureSingle(String label, CheckedProducer<?> producer, long executionTime) throws CommonException {
protected double measureSingle(String label, CheckedProducer<?> producer, long executionTime, Stopwatch watch) throws CommonException {
long until = System.currentTimeMillis() + executionTime;
int iteration = 0;
while (System.currentTimeMillis() < until) {
if (producer.get() == null) {
Object result = null;
iteration++;
try (Split split = watch.start()) {
result = producer.get();
}
if (result == null) {
// just to make sure the result is used somehow (and not optimized away)
throw new IllegalStateException("null result from the producer");
}
iteration++;
}
double micros = ((double) executionTime) * 1000 / (double) iteration;
double micros = ((double) executionTime) * 1000 / iteration;
String message = label + ": " + iteration + " iterations in " + executionTime + " milliseconds (" + micros + " us per iteration)";
System.out.println(message);
logger.info(message);

return micros;
}

@NotNull
public PrismObject<UserType> getJack() throws SchemaException, IOException {
return getPrismContext().parserFor(USER_JACK_FILE).parse();
}

@AfterClass
@Override
public void dumpReport() {
PerformanceTestMixin.super.dumpReport();
}
}
Expand Up @@ -12,50 +12,63 @@
import com.evolveum.midpoint.prism.polystring.PolyString;
import com.evolveum.midpoint.prism.schema.SchemaRegistry;
import com.evolveum.midpoint.prism.xnode.RootXNode;
import com.evolveum.midpoint.tools.testng.PerformanceTestMixin;
import com.evolveum.midpoint.xml.ns._public.common.common_3.UserType;
import com.evolveum.prism.xml.ns._public.types_3.PolyStringType;
import org.testng.annotations.Test;

import static com.evolveum.midpoint.prism.util.PrismTestUtil.getPrismContext;

public class TestBasicPerformance extends AbstractSchemaPerformanceTest {
public class TestBasicPerformance extends AbstractSchemaPerformanceTest implements PerformanceTestMixin {

@Test
public void test010Dummy() throws Exception {
measure("currentTimeMillis", () -> System.currentTimeMillis());
measure("long numbers addition", () -> System.currentTimeMillis() + 2000L);
measure("currentTimeMillis", "Dummy test invoking currentTimeMillis",
() -> System.currentTimeMillis());
measure("long numbers addition", "Dummy test invoking currentTimeMillis",
() -> System.currentTimeMillis() + 2000L);
}

@Test
public void test100FindNameProperty() throws Exception {
PrismObject<UserType> jack = getJack();
measure("findProperty(name)", () -> jack.findProperty(UserType.F_NAME));
measure("userJack.",
"Search of property 'name' on user-jack",
() -> jack.findProperty(UserType.F_NAME));
}

@Test
public void test110FindNameItem() throws Exception {
PrismObject<UserType> jack = getJack();
measure("findItem(name)", () -> jack.findItem(UserType.F_NAME));
measure("userJack.findItem",
"Search of item 'name on user-jack",
() -> jack.findItem(UserType.F_NAME));
}

@Test
public void test120FindExtensionProperty() throws Exception {
PrismObject<UserType> jack = getJack();
ItemPath bar23 = ItemPath.create(UserType.F_EXTENSION, "bar23");
measure("findProperty(extension/bar23)", () -> jack.findProperty(bar23));
measure("userJack.findProperty.extension",
"Search of extension/bar23 property on user/jack",
() -> jack.findProperty(bar23));
}

@Test
public void test130FindExtensionItem() throws Exception {
PrismObject<UserType> jack = getJack();
ItemPath bar23 = ItemPath.create(UserType.F_EXTENSION, "bar23");
measure("findItem(extension/bar23)", () -> jack.findItem(bar23));
measure("userJack.findItem.extension",
"Search of extension/bar23 item on user/jack",
() -> jack.findItem(bar23));
}

@Test
public void test200SetName() throws Exception {
PrismObject<UserType> jack = getJack();
measure("setName(name)", () -> {
measure("userJack.asObjectable.setName",
"Setting name of user jack via Objectable",
() -> {
jack.asObjectable().setName(PolyStringType.fromOrig("jack_" + Math.random()));
return true;
});
Expand All @@ -64,7 +77,9 @@ public void test200SetName() throws Exception {
@Test
public void test210SetNameViaProperty() throws Exception {
PrismObject<UserType> jack = getJack();
measure("findProperty(name).setRealValue", () -> {
measure("findProperty(name).setRealValue",
"Setting name of user jack via property.setRealValue",
() -> {
jack.findProperty(UserType.F_NAME).setRealValue(PolyString.fromOrig("jack_" + Math.random()));
return true;
});
Expand All @@ -74,17 +89,20 @@ public void test210SetNameViaProperty() throws Exception {
public void test215SetNameViaPropertyUsingExistingValue() throws Exception {
PrismObject<UserType> jack = getJack();
PolyString realValue = PolyString.fromOrig("jack_" + Math.random());
measure("findProperty(name).setRealValue(existing)", () -> {
jack.findProperty(UserType.F_NAME).setRealValue(realValue);
return true;
measure("findProperty(name).setRealValue.static",
"Setting constant name of user jack via property.setRealValue",
() -> {
jack.findProperty(UserType.F_NAME).setRealValue(realValue); return true;
});
}

@Test
public void test220SetExtensionItemString() throws Exception {
PrismObject<UserType> jack = getJack();
ItemPath bar23 = ItemPath.create(UserType.F_EXTENSION, "bar23");
measure("findProperty(extension/bar23).setRealValue", () -> {
measure("findProperty(extension/bar23).setRealValue",
"",
() -> {
jack.findProperty(bar23).setRealValue("jack_" + Math.random());
return 1;
});
Expand All @@ -94,7 +112,9 @@ public void test220SetExtensionItemString() throws Exception {
public void test230SetExtensionItemPolyString() throws Exception {
PrismObject<UserType> jack = getJack();
ItemPath bar23 = ItemPath.create(UserType.F_EXTENSION, "bar23");
measure("findProperty(extension/bar23).setRealValue(polystring)", () -> {
measure("findProperty(extension/bar23).setRealValue(polystring)",
"",
() -> {
jack.findProperty(bar23).setRealValue(PolyString.fromOrig("jack_" + Math.random()));
return 1;
});
Expand All @@ -103,75 +123,103 @@ public void test230SetExtensionItemPolyString() throws Exception {
@Test
public void test300Clone() throws Exception {
PrismObject<UserType> jack = getJack();
measure("jack.clone", () -> jack.clone());
measure("jack.clone",
"Clone of user jack",
() -> jack.clone());
}

@Test
public void test310ParseXml() throws Exception {
PrismObject<UserType> jack = getJack();
String string = getPrismContext().xmlSerializer().serialize(jack);
measure("parse XML (" + string.length() + " chars)", () -> getPrismContext().parserFor(string).xml().parse());
measure("parse XML to XNode (" + string.length() + " chars)", () -> getPrismContext().parserFor(string).xml().parseToXNode());
measure("parse.xml.prism",
"parse XML (" + string.length() + " chars)",
() -> getPrismContext().parserFor(string).xml().parse());
measure("parse.xml.xnode",
"parse XML to XNode (" + string.length() + " chars)",
() -> getPrismContext().parserFor(string).xml().parseToXNode());
}

@Test
public void test320ParseJson() throws Exception {
PrismObject<UserType> jack = getJack();
String string = getPrismContext().jsonSerializer().serialize(jack);
measure("parse JSON (" + string.length() + " chars)", () -> getPrismContext().parserFor(string).json().parse());
measure("parse JSON to XNode (" + string.length() + " chars)", () -> getPrismContext().parserFor(string).json().parseToXNode());
measure("parse.json.prism",
"parse JSON (" + string.length() + " chars)",
() -> getPrismContext().parserFor(string).json().parse());
measure("parse.json.xnode",
"parse JSON to XNode (" + string.length() + " chars)",
() -> getPrismContext().parserFor(string).json().parseToXNode());
}

@Test
public void test330ParseYaml() throws Exception {
PrismObject<UserType> jack = getJack();
String string = getPrismContext().yamlSerializer().serialize(jack);
measure("parse YAML (" + string.length() + " chars)", () -> getPrismContext().parserFor(string).yaml().parse());
measure("parse YAML to XNode (" + string.length() + " chars)", () -> getPrismContext().parserFor(string).yaml().parseToXNode());
measure("parse.yaml.prism",
"parse YAML (" + string.length() + " chars)",
() -> getPrismContext().parserFor(string).yaml().parse());
measure("parse.yaml.json",
"parse YAML to XNode (" + string.length() + " chars)",
() -> getPrismContext().parserFor(string).yaml().parseToXNode());
}

@Test
public void test340ParseXNode() throws Exception {
PrismObject<UserType> jack = getJack();
RootXNode xnode = getPrismContext().xnodeSerializer().serialize(jack);
measure("parse XNode", () -> getPrismContext().parserFor(xnode).parse());
measure("parse.xnode.prism",
"parse XNode",
() -> getPrismContext().parserFor(xnode).parse());
}

@Test
public void test350SerializeToXml() throws Exception {
PrismObject<UserType> jack = getJack();
measure("serialize to XML", () -> getPrismContext().xmlSerializer().serialize(jack));
measure("serialize.xml",
"serialize to XML",
() -> getPrismContext().xmlSerializer().serialize(jack));
}

@Test
public void test360SerializeToJson() throws Exception {
PrismObject<UserType> jack = getJack();
measure("serialize to JSON", () -> getPrismContext().jsonSerializer().serialize(jack));
measure("serialize.json",
"serialize to JSON",
() -> getPrismContext().jsonSerializer().serialize(jack));
}

@Test
public void test370SerializeToYaml() throws Exception {
PrismObject<UserType> jack = getJack();
measure("serialize to YAML", () -> getPrismContext().yamlSerializer().serialize(jack));
measure("serialize.yaml",
"serialize to YAML",
() -> getPrismContext().yamlSerializer().serialize(jack));
}

@Test
public void test400FindNameDefinition() throws Exception {
SchemaRegistry schemaRegistry = getPrismContext().getSchemaRegistry();
PrismObjectDefinition<UserType> userDefinition = schemaRegistry.findObjectDefinitionByCompileTimeClass(UserType.class);
measure("userDefinition.findItemDefinition(UserType.F_NAME)", () -> userDefinition.findItemDefinition(UserType.F_NAME));
measure("userDefinition.findItemDefinition(UserType.F_NAME)",
"Lookup name definition in user definition",
() -> userDefinition.findItemDefinition(UserType.F_NAME));
}

@Test
public void test410FindAdminGuiConfigurationDefinition() throws Exception {
SchemaRegistry schemaRegistry = getPrismContext().getSchemaRegistry();
PrismObjectDefinition<UserType> userDefinition = schemaRegistry.findObjectDefinitionByCompileTimeClass(UserType.class);
measure("userDefinition.findItemDefinition(UserType.F_ADMIN_GUI_CONFIGURATION)", () -> userDefinition.findItemDefinition(UserType.F_ADMIN_GUI_CONFIGURATION));
measure("userDefinition.findItemDefinition(UserType.F_ADMIN_GUI_CONFIGURATION)",
"Lookup admin gui configuration definition in user definition",
() -> userDefinition.findItemDefinition(UserType.F_ADMIN_GUI_CONFIGURATION));
}

@Test
public void test420FindUserDefinition() throws Exception {
SchemaRegistry schemaRegistry = getPrismContext().getSchemaRegistry();
measure("schemaRegistry.findObjectDefinitionByCompileTimeClass(UserType.class)", () -> schemaRegistry.findObjectDefinitionByCompileTimeClass(UserType.class));
measure("schemaRegistry.findObjectDefinitionByCompileTimeClass(UserType.class)",
"Lookup User definition by UserType class",
() -> schemaRegistry.findObjectDefinitionByCompileTimeClass(UserType.class));
}
}

0 comments on commit c4a9782

Please sign in to comment.