Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Benchmarks for Parsing and caching auto generated XForms #454

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ sourceSets {
test.java.srcDirs = ['src/test/java']
test.resources.srcDirs = ['src/test/resources']
}

compileJava.options.encoding = "UTF-8"
compileTestJava.options.encoding = "UTF-8"

Expand Down
57 changes: 52 additions & 5 deletions src/jmh/java/org/javarosa/benchmarks/BenchmarkUtils.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package org.javarosa.benchmarks;

import static org.javarosa.test.utils.ResourcePathHelper.r;
import static org.javarosa.core.reference.ReferenceManagerTestUtils.setUpSimpleReferenceManager;
import static org.javarosa.test.utils.ResourcePathHelper.r;

import java.io.File;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
Expand All @@ -16,17 +17,24 @@
import java.nio.file.Path;
import java.util.HashMap;
import java.util.stream.Stream;

import org.javarosa.benchmarks.utils.XFormFileGenerator;
import org.javarosa.core.model.CoreModelModule;
import org.javarosa.core.model.QuestionDef;
import org.javarosa.core.model.data.IAnswerData;
import org.javarosa.core.model.data.LongData;
import org.javarosa.core.model.data.StringData;
import org.javarosa.core.services.PrototypeManager;
import org.javarosa.core.util.JavaRosaCoreModule;
import org.javarosa.model.xform.XFormsModule;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.infra.Blackhole;

public class BenchmarkUtils {
private static Path CACHE_PATH;
private static Path WORKING_DIR;
public static Path prepareAssets(String... filenames) {
try {
Path assetsDir = Files.createTempDirectory("javarosa_benchmarks_");
Expand Down Expand Up @@ -146,30 +154,69 @@ public static Path getMinifiedNigeriaWardsXMLWithInternal2ndryInstance(){

public static Path getNigeriaWardsXMLWithExternal2ndryInstance(){
Path assetsPath = prepareAssets("nigeria_wards_external_2ndry_instance.xml", "lgas.xml", "wards.xml");
setUpSimpleReferenceManager(assetsPath, "file");
setUpSimpleReferenceManager(assetsPath,"file");
Path filePath = assetsPath.resolve("nigeria_wards_external_2ndry_instance.xml");
return filePath;
}

public static Path getWardsExternalInstance(){
Path assetsPath = prepareAssets( "wards.xml");
setUpSimpleReferenceManager(assetsPath, "file");
setUpSimpleReferenceManager( assetsPath,"file");
Path filePath = assetsPath.resolve("wards.xml");
return filePath;
}

public static Path getLGAsExternalInstance(){
Path assetsPath = prepareAssets( "lgas.xml");
setUpSimpleReferenceManager(assetsPath, "file");
setUpSimpleReferenceManager( assetsPath,"file");
Path filePath = assetsPath.resolve("lgas.xml");
return filePath;
}


public static File generateXFormFile(int noOfQuestions, int noOfQuestionGroups, int noOfInternalSecondaryInstances, int noOfExternalSecondaryInstances, int noOf2ndryInstanceElements) throws IOException {
XFormFileGenerator xFormFileGenerator = new XFormFileGenerator();
String title = String.format("xform_%s_%sISI%sE_%sESI%sE", noOfQuestions,
noOfInternalSecondaryInstances, noOf2ndryInstanceElements,
noOfExternalSecondaryInstances, noOf2ndryInstanceElements
);
File existingFile = getWorkingDir().resolve(title + ".xml").toFile();
File xFormXmlFile;
if(existingFile.exists()){
xFormXmlFile = existingFile;
}else{
xFormXmlFile = xFormFileGenerator.generateXFormFile(title, noOfQuestions, noOfQuestionGroups, noOfInternalSecondaryInstances, noOfExternalSecondaryInstances, noOf2ndryInstanceElements, getWorkingDir());
}
return xFormXmlFile;
}

public static void registerCacheProtoTypes() {
PrototypeManager.registerPrototypes(JavaRosaCoreModule.classNames);
PrototypeManager.registerPrototypes(CoreModelModule.classNames);
new XFormsModule().registerModule();
}



public static Path getCachePath() throws IOException {
if(CACHE_PATH == null){
CACHE_PATH = Files.createTempDirectory("javarosa_benchmarks_cache");
File cacheDir = new File(getWorkingDir() + File.separator + "_cache");
cacheDir.mkdir();
CACHE_PATH = cacheDir.toPath();
}
return CACHE_PATH;
}

public static Path getWorkingDir() throws IOException {
if(WORKING_DIR == null){
String tempDir = System.getProperty("java.io.tmpdir");
File file = new File(tempDir + File.separator + "javarosa_benchmarks");
if(!file.exists()){
file.mkdir();
}
WORKING_DIR = file.toPath();
}
return WORKING_DIR;
}

}
62 changes: 62 additions & 0 deletions src/jmh/java/org/javarosa/benchmarks/Cache2FormDefBenchmark.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package org.javarosa.benchmarks;

import org.javarosa.benchmarks.utils.FormDefCache;
import org.javarosa.core.model.FormDef;
import org.javarosa.xform.parse.FormParserHelper;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.infra.Blackhole;

import java.io.File;
import java.io.IOException;

import static org.javarosa.core.reference.ReferenceManagerTestUtils.setUpSimpleReferenceManager;
import static org.javarosa.benchmarks.BenchmarkUtils.dryRun;
import static org.javarosa.benchmarks.BenchmarkUtils.getCachePath;
import static org.javarosa.benchmarks.BenchmarkUtils.getWorkingDir;
import static org.javarosa.benchmarks.BenchmarkUtils.registerCacheProtoTypes;

public class Cache2FormDefBenchmark {

public static void main(String[] args) {
dryRun(Cache2FormDefBenchmark.class);
}

@State(Scope.Thread)
public static class FormTypesState {
File xFormXmlFile ;
FormDef formDef ;
String CACHE_PATH;
@Param({"10", "200", "500"})
public int noOfQuestions;
@Param({"1", "10"})
public int noOfInternalSecondaryInstances;
@Param({"50", "500", "5000"})
public int noOf2ndryInstanceElements;
@Param({"0"})
public int noOfQuestionGroups;
@Param({"0"})
public int noOfExternalSecondaryInstances;
@Setup(Level.Trial)
public void initialize() throws IOException {
CACHE_PATH = getCachePath().toString();
xFormXmlFile = BenchmarkUtils.generateXFormFile(noOfQuestions, noOfQuestionGroups, noOfInternalSecondaryInstances, noOfExternalSecondaryInstances, noOf2ndryInstanceElements);
setUpSimpleReferenceManager(getWorkingDir(),"file");
String formPath = xFormXmlFile.getPath();
formDef = FormParserHelper.parse(xFormXmlFile.toPath());
registerCacheProtoTypes();
FormDefCache.writeCache(formDef, formPath, CACHE_PATH);
}
}

@Benchmark
public void runBenchmark(FormTypesState state, Blackhole bh) throws IOException {
FormDef formDef = FormDefCache.readCache(state.xFormXmlFile, state.CACHE_PATH);
bh.consume(formDef);
}

}
61 changes: 61 additions & 0 deletions src/jmh/java/org/javarosa/benchmarks/FormDef2CacheBenchmark.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package org.javarosa.benchmarks;

import org.javarosa.benchmarks.utils.FormDefCache;
import org.javarosa.core.model.FormDef;
import org.javarosa.xform.parse.FormParserHelper;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;

import java.io.File;
import java.io.IOException;

import static org.javarosa.core.reference.ReferenceManagerTestUtils.setUpSimpleReferenceManager;
import static org.javarosa.benchmarks.BenchmarkUtils.dryRun;
import static org.javarosa.benchmarks.BenchmarkUtils.getCachePath;
import static org.javarosa.benchmarks.BenchmarkUtils.getWorkingDir;
import static org.javarosa.benchmarks.BenchmarkUtils.registerCacheProtoTypes;

public class FormDef2CacheBenchmark {

public static void main(String[] args) {
dryRun(FormDef2CacheBenchmark.class);
}

@State(Scope.Thread)
public static class FormTypesState {
String formPath ;
FormDef formDef ;
String CACHE_PATH;
@Param({"10", "200", "500"})
public int noOfQuestions;
@Param({"1", "10"})
public int noOfInternalSecondaryInstances;
@Param({"50", "500", "5000"})
public int noOf2ndryInstanceElements;
@Param({"0"})
public int noOfQuestionGroups = 1;
@Param({"0"})
public int noOfExternalSecondaryInstances;
@Setup(Level.Trial)
public void initialize() throws IOException {
CACHE_PATH = getCachePath().toString();
File xFormXmlFile = BenchmarkUtils.generateXFormFile(noOfQuestions, noOfQuestionGroups, noOfInternalSecondaryInstances, noOfExternalSecondaryInstances, noOf2ndryInstanceElements);
setUpSimpleReferenceManager(getWorkingDir(), "file");
registerCacheProtoTypes();
formPath = xFormXmlFile.getPath();
formDef = FormParserHelper.parse(xFormXmlFile.toPath());
registerCacheProtoTypes();
}
}


@Benchmark
public void runBenchmark(FormTypesState state) throws IOException {
FormDefCache.writeCache(state.formDef,state.formPath, state.CACHE_PATH);
}

}
54 changes: 54 additions & 0 deletions src/jmh/java/org/javarosa/benchmarks/XForm2FormDefBenchmark.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.javarosa.benchmarks;

import org.javarosa.core.model.FormDef;
import org.javarosa.xform.parse.FormParserHelper;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.infra.Blackhole;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;

import static org.javarosa.benchmarks.BenchmarkUtils.dryRun;
import static org.javarosa.benchmarks.BenchmarkUtils.getWorkingDir;
import static org.javarosa.core.reference.ReferenceManagerTestUtils.setUpSimpleReferenceManager;

public class XForm2FormDefBenchmark {

public static void main(String[] args) {
dryRun(XForm2FormDefBenchmark.class);
}

@State(Scope.Thread)
public static class FormTypesState {
Path xFormXmlPath ;
@Param({"10", "200", "500"})
public int noOfQuestions;
@Param({"1", "10"})
public int noOfInternalSecondaryInstances;
@Param({"50", "500", "5000"})
public int noOf2ndryInstanceElements;
@Param({"0"})
public int noOfQuestionGroups;
@Param({"0"})
public int noOfExternalSecondaryInstances;
@Setup(Level.Trial)
public void initialize() throws IOException {
File xFormXmlFile = BenchmarkUtils.generateXFormFile(noOfQuestions, noOfQuestionGroups, noOfInternalSecondaryInstances, noOfExternalSecondaryInstances, noOf2ndryInstanceElements);
setUpSimpleReferenceManager(getWorkingDir(),"file");
xFormXmlPath = xFormXmlFile.toPath();
}
}

@Benchmark
public void runBenchmark(FormTypesState state, Blackhole bh) throws IOException {
FormDef formDef = FormParserHelper.parse(state.xFormXmlPath);
bh.consume(formDef);
}

}
Loading