|
| 1 | +package org.javarosa.benchmarks.utils; |
| 2 | + |
| 3 | +import static org.javarosa.test.utils.ResourcePathHelper.r; |
| 4 | +import static org.javarosa.core.reference.ReferenceManagerTestUtils.setUpSimpleReferenceManager; |
| 5 | + |
| 6 | +import java.io.File; |
| 7 | +import java.io.IOException; |
| 8 | +import java.lang.annotation.Annotation; |
| 9 | +import java.lang.reflect.InvocationTargetException; |
| 10 | +import java.lang.reflect.Method; |
| 11 | +import java.net.URI; |
| 12 | +import java.net.URISyntaxException; |
| 13 | +import java.nio.file.FileSystem; |
| 14 | +import java.nio.file.FileSystemNotFoundException; |
| 15 | +import java.nio.file.FileSystems; |
| 16 | +import java.nio.file.Files; |
| 17 | +import java.nio.file.Path; |
| 18 | +import java.util.HashMap; |
| 19 | +import java.util.stream.Stream; |
| 20 | +import org.javarosa.core.model.QuestionDef; |
| 21 | +import org.javarosa.core.model.data.IAnswerData; |
| 22 | +import org.javarosa.core.model.data.LongData; |
| 23 | +import org.javarosa.core.model.data.StringData; |
| 24 | +import org.openjdk.jmh.annotations.Benchmark; |
| 25 | +import org.openjdk.jmh.annotations.Setup; |
| 26 | +import org.openjdk.jmh.annotations.State; |
| 27 | +import org.openjdk.jmh.infra.Blackhole; |
| 28 | + |
| 29 | +public class BenchmarkUtils { |
| 30 | + private static Path CACHE_PATH; |
| 31 | + private static Path WORKING_DIR; |
| 32 | + public static Path prepareAssets(String... filenames) { |
| 33 | + try { |
| 34 | + Path assetsDir = Files.createTempDirectory("javarosa_benchmarks_"); |
| 35 | + for (String filename : filenames) { |
| 36 | + String realPath = BenchmarkUtils.class |
| 37 | + .getResource(filename.startsWith("/") ? filename : "/" + filename) |
| 38 | + .toURI().toString(); |
| 39 | + Files.copy( |
| 40 | + realPath.contains("!") ? getPathInJar(realPath) : r(filename), |
| 41 | + assetsDir.resolve(filename) |
| 42 | + ); |
| 43 | + } |
| 44 | + return assetsDir; |
| 45 | + } catch (IOException | URISyntaxException e) { |
| 46 | + throw new RuntimeException(e); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + private static Path getPathInJar(String realPath) { |
| 51 | + Path sourcePath; |
| 52 | + try { |
| 53 | + String[] parts = realPath.split("!"); |
| 54 | + String jarPart = parts[0]; |
| 55 | + String filePart = parts[1]; |
| 56 | + sourcePath = getFileSystem(URI.create(jarPart)).getPath(filePart); |
| 57 | + } catch (IOException e) { |
| 58 | + throw new RuntimeException(e); |
| 59 | + } |
| 60 | + return sourcePath; |
| 61 | + } |
| 62 | + |
| 63 | + private static FileSystem getFileSystem(URI jarUri) throws IOException { |
| 64 | + FileSystem fileSystem; |
| 65 | + try { |
| 66 | + fileSystem = FileSystems.getFileSystem(jarUri); |
| 67 | + } catch (FileSystemNotFoundException e) { |
| 68 | + fileSystem = FileSystems.newFileSystem(jarUri, new HashMap<String, String>()); |
| 69 | + } |
| 70 | + return fileSystem; |
| 71 | + } |
| 72 | + |
| 73 | + private static Blackhole getBlackhole() { |
| 74 | + return new Blackhole("Today's password is swordfish. I understand instantiating Blackholes directly is dangerous."); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * This method will run all methods annotated with @Benchmark declared in the provided class. |
| 79 | + * <p> |
| 80 | + * This method uses reflection to provide all the required params. |
| 81 | + */ |
| 82 | + @SuppressWarnings("unchecked") |
| 83 | + public static void dryRun(Class clazz) { |
| 84 | + Stream<Method> methodsWithAnnotation = getMethodsWithAnnotation(clazz, Benchmark.class); |
| 85 | + methodsWithAnnotation.forEach(method -> { |
| 86 | + try { |
| 87 | + Object[] paramValues = new Object[method.getParameterCount()]; |
| 88 | + int i = 0; |
| 89 | + for (Class paramType : method.getParameterTypes()) |
| 90 | + if (paramType.equals(Blackhole.class)) |
| 91 | + paramValues[i++] = getBlackhole(); |
| 92 | + else { |
| 93 | + Object stateInstance = paramType.getConstructor().newInstance(); |
| 94 | + if (hasAnnotation(paramType, State.class)) { |
| 95 | + getMethodsWithAnnotation(paramType, Setup.class) |
| 96 | + .findFirst() |
| 97 | + .orElseThrow(RuntimeException::new) |
| 98 | + .invoke(stateInstance); |
| 99 | + } |
| 100 | + paramValues[i++] = stateInstance; |
| 101 | + } |
| 102 | + Object instance = clazz.newInstance(); |
| 103 | + method.invoke(instance, paramValues); |
| 104 | + } catch (InstantiationException | NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { |
| 105 | + throw new RuntimeException(e); |
| 106 | + } |
| 107 | + }); |
| 108 | + |
| 109 | + } |
| 110 | + |
| 111 | + private static boolean hasAnnotation(Class<?> paramType, Class<? extends Annotation> annotationType) { |
| 112 | + return Stream.of(paramType.getDeclaredAnnotations()).anyMatch(a -> a.annotationType().equals(annotationType)); |
| 113 | + } |
| 114 | + |
| 115 | + private static Stream<Method> getMethodsWithAnnotation(Class<?> clazz, Class<? extends Annotation> annotationClass) { |
| 116 | + return Stream.of(clazz.getDeclaredMethods()) |
| 117 | + .filter(paramTypeMethod -> paramTypeMethod.isAnnotationPresent(annotationClass)); |
| 118 | + } |
| 119 | + |
| 120 | + public static IAnswerData getStubAnswer(QuestionDef question) { |
| 121 | + switch (question.getLabelInnerText()) { |
| 122 | + case "State": |
| 123 | + return new StringData("7b0ded95031647702b8bed17dce7698a"); // Abia |
| 124 | + case "LGA": |
| 125 | + return new StringData("6fa741c46485b9c618f14b79edf50e88"); // Aba North |
| 126 | + case "Ward": |
| 127 | + return new StringData("90fa443787485709a5b11c5f7925fb71"); // Ariaria |
| 128 | + case "Comments": |
| 129 | + return new StringData("No Comment"); |
| 130 | + case "What population do you want to search for?": |
| 131 | + return new LongData(699967); |
| 132 | + default: |
| 133 | + return new StringData(""); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + public static Path getNigeriaWardsXMLWithInternal2ndryInstance(){ |
| 138 | + Path assetsPath = prepareAssets("nigeria_wards_internal_2ndry_instance.xml"); |
| 139 | + Path filePath = assetsPath.resolve("nigeria_wards_internal_2ndry_instance.xml"); |
| 140 | + return filePath; |
| 141 | + } |
| 142 | + |
| 143 | + public static Path getMinifiedNigeriaWardsXMLWithInternal2ndryInstance(){ |
| 144 | + Path assetsPath = prepareAssets("nigeria_wards_internal_2ndry_instance_minified.xml"); |
| 145 | + Path filePath = assetsPath.resolve("nigeria_wards_internal_2ndry_instance_minified.xml"); |
| 146 | + return filePath; |
| 147 | + } |
| 148 | + |
| 149 | + public static Path getNigeriaWardsXMLWithExternal2ndryInstance(){ |
| 150 | + Path assetsPath = prepareAssets("nigeria_wards_external_2ndry_instance.xml", "lgas.xml", "wards.xml"); |
| 151 | + setUpSimpleReferenceManager("file", assetsPath); |
| 152 | + Path filePath = assetsPath.resolve("nigeria_wards_external_2ndry_instance.xml"); |
| 153 | + return filePath; |
| 154 | + } |
| 155 | + |
| 156 | + public static Path getWardsExternalInstance(){ |
| 157 | + Path assetsPath = prepareAssets( "wards.xml"); |
| 158 | + setUpSimpleReferenceManager("file", assetsPath); |
| 159 | + Path filePath = assetsPath.resolve("wards.xml"); |
| 160 | + return filePath; |
| 161 | + } |
| 162 | + |
| 163 | + public static Path getLGAsExternalInstance(){ |
| 164 | + Path assetsPath = prepareAssets( "lgas.xml"); |
| 165 | + setUpSimpleReferenceManager("file", assetsPath); |
| 166 | + Path filePath = assetsPath.resolve("lgas.xml"); |
| 167 | + return filePath; |
| 168 | + } |
| 169 | + |
| 170 | + public static Path getCachePath() throws IOException { |
| 171 | + if(CACHE_PATH == null){ |
| 172 | + File cacheDir = new File(getWorkingDir() + File.separator + "_cache"); |
| 173 | + cacheDir.mkdir(); |
| 174 | + CACHE_PATH = cacheDir.toPath(); |
| 175 | + } |
| 176 | + return CACHE_PATH; |
| 177 | + } |
| 178 | + |
| 179 | + public static Path getWorkingDir() throws IOException { |
| 180 | + if(WORKING_DIR == null){ |
| 181 | + String tempDir = System.getProperty("java.io.tmpdir"); |
| 182 | + File file = new File(tempDir + File.separator + "javarosa_benchmarks"); |
| 183 | + if(!file.exists()){ |
| 184 | + file.mkdir(); |
| 185 | + } |
| 186 | + WORKING_DIR = file.toPath(); |
| 187 | + } |
| 188 | + return WORKING_DIR; |
| 189 | + } |
| 190 | + |
| 191 | +} |
0 commit comments