|
| 1 | +package com.codename1.tools.translator; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.AfterEach; |
| 4 | +import org.junit.jupiter.api.BeforeEach; |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | +import org.objectweb.asm.ClassWriter; |
| 7 | +import org.objectweb.asm.MethodVisitor; |
| 8 | +import org.objectweb.asm.Opcodes; |
| 9 | + |
| 10 | +import java.io.File; |
| 11 | +import java.io.FileOutputStream; |
| 12 | +import java.nio.file.Files; |
| 13 | +import java.nio.file.Path; |
| 14 | + |
| 15 | +public class StressTest { |
| 16 | + |
| 17 | + private Path tempDir; |
| 18 | + private String originalOptimizer; |
| 19 | + |
| 20 | + @BeforeEach |
| 21 | + public void setup() throws Exception { |
| 22 | + Parser.cleanup(); |
| 23 | + tempDir = Files.createTempDirectory("stress-test"); |
| 24 | + originalOptimizer = System.getProperty("optimizer"); |
| 25 | + System.setProperty("optimizer", "on"); |
| 26 | + // Force re-initialization of optimizerOn flag in BytecodeMethod if needed, |
| 27 | + // but it's static final initialized at load time. |
| 28 | + // Actually BytecodeMethod.optimizerOn is static but not final, but it is initialized in static block. |
| 29 | + // We might need to reflectively set it if the class is already loaded. |
| 30 | + // However, tests run in separate forks or BytecodeMethod might be reloaded? |
| 31 | + // Let's assume we might need to toggle it via reflection if it's already loaded. |
| 32 | + |
| 33 | + try { |
| 34 | + java.lang.reflect.Field f = BytecodeMethod.class.getDeclaredField("optimizerOn"); |
| 35 | + f.setAccessible(true); |
| 36 | + f.set(null, true); |
| 37 | + } catch (Exception e) { |
| 38 | + e.printStackTrace(); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + @AfterEach |
| 43 | + public void teardown() throws Exception { |
| 44 | + Parser.cleanup(); |
| 45 | + deleteDir(tempDir.toFile()); |
| 46 | + if (originalOptimizer != null) { |
| 47 | + System.setProperty("optimizer", originalOptimizer); |
| 48 | + } else { |
| 49 | + System.clearProperty("optimizer"); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + private void deleteDir(File file) { |
| 54 | + if (file.isDirectory()) { |
| 55 | + for (File f : file.listFiles()) { |
| 56 | + deleteDir(f); |
| 57 | + } |
| 58 | + } |
| 59 | + file.delete(); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void testChainOfMethods() throws Exception { |
| 64 | + // Creates a long chain of dependencies: Class0.main -> Class0.m0 -> Class0.m1 ... -> Class0.mN -> Class1.m0 ... |
| 65 | + int numClasses = 50; |
| 66 | + int methodsPerClass = 50; |
| 67 | + |
| 68 | + System.out.println("Generating " + numClasses + " classes with " + methodsPerClass + " methods each..."); |
| 69 | + for (int i = 0; i < numClasses; i++) { |
| 70 | + createClass(i, numClasses, methodsPerClass); |
| 71 | + } |
| 72 | + |
| 73 | + System.out.println("Parsing classes..."); |
| 74 | + File[] files = tempDir.toFile().listFiles((d, n) -> n.endsWith(".class")); |
| 75 | + for (File f : files) { |
| 76 | + Parser.parse(f); |
| 77 | + } |
| 78 | + |
| 79 | + System.out.println("Running optimizer..."); |
| 80 | + long start = System.currentTimeMillis(); |
| 81 | + File outputDir = Files.createTempDirectory("stress-output").toFile(); |
| 82 | + try { |
| 83 | + // Mock ByteCodeTranslator.output to C or something valid |
| 84 | + ByteCodeTranslator.output = ByteCodeTranslator.OutputType.OUTPUT_TYPE_IOS; |
| 85 | + |
| 86 | + Parser.writeOutput(outputDir); |
| 87 | + } finally { |
| 88 | + deleteDir(outputDir); |
| 89 | + } |
| 90 | + long end = System.currentTimeMillis(); |
| 91 | + System.out.println("Optimization and write took: " + (end - start) + "ms for " + (numClasses * methodsPerClass) + " methods."); |
| 92 | + } |
| 93 | + |
| 94 | + private void createClass(int index, int totalClasses, int methodsPerClass) throws Exception { |
| 95 | + String className = "Class" + index; |
| 96 | + ClassWriter cw = new ClassWriter(0); |
| 97 | + cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, className, null, "java/lang/Object", null); |
| 98 | + |
| 99 | + // Add constructor |
| 100 | + MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null); |
| 101 | + mv.visitCode(); |
| 102 | + mv.visitVarInsn(Opcodes.ALOAD, 0); |
| 103 | + mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false); |
| 104 | + mv.visitInsn(Opcodes.RETURN); |
| 105 | + mv.visitMaxs(1, 1); |
| 106 | + mv.visitEnd(); |
| 107 | + |
| 108 | + // Add main method if index 0 |
| 109 | + if (index == 0) { |
| 110 | + mv = cw.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "main", "([Ljava/lang/String;)V", null, null); |
| 111 | + mv.visitCode(); |
| 112 | + // Call method 0 of class 0 |
| 113 | + mv.visitMethodInsn(Opcodes.INVOKESTATIC, className, "method0", "()V", false); |
| 114 | + mv.visitInsn(Opcodes.RETURN); |
| 115 | + mv.visitMaxs(1, 1); |
| 116 | + mv.visitEnd(); |
| 117 | + } |
| 118 | + |
| 119 | + // Add methods |
| 120 | + for (int m = 0; m < methodsPerClass; m++) { |
| 121 | + mv = cw.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "method" + m, "()V", null, null); |
| 122 | + mv.visitCode(); |
| 123 | + |
| 124 | + // Call next method or method in next class to create dependency chain |
| 125 | + if (m < methodsPerClass - 1) { |
| 126 | + mv.visitMethodInsn(Opcodes.INVOKESTATIC, className, "method" + (m + 1), "()V", false); |
| 127 | + } else if (index < totalClasses - 1) { |
| 128 | + mv.visitMethodInsn(Opcodes.INVOKESTATIC, "Class" + (index + 1), "method0", "()V", false); |
| 129 | + } |
| 130 | + |
| 131 | + mv.visitInsn(Opcodes.RETURN); |
| 132 | + mv.visitMaxs(1, 0); |
| 133 | + mv.visitEnd(); |
| 134 | + } |
| 135 | + |
| 136 | + cw.visitEnd(); |
| 137 | + |
| 138 | + FileOutputStream fos = new FileOutputStream(new File(tempDir.toFile(), className + ".class")); |
| 139 | + fos.write(cw.toByteArray()); |
| 140 | + fos.close(); |
| 141 | + } |
| 142 | +} |
0 commit comments