Skip to content

Commit

Permalink
add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
LinShunKang committed Oct 18, 2018
1 parent 53f0fbe commit 9b8d633
Show file tree
Hide file tree
Showing 36 changed files with 549 additions and 58 deletions.
6 changes: 6 additions & 0 deletions MyPerf4J-ASM/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@
<artifactId>MyPerf4J-Core</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import cn.myperf4j.asm.aop.ProfilingAspect;
import cn.myperf4j.core.AbstractBootstrap;
import cn.myperf4j.core.AbstractRecorderMaintainer;
import cn.myperf4j.core.recorder.AbstractRecorderMaintainer;
import cn.myperf4j.base.config.ProfilingConfig;
import cn.myperf4j.base.util.Logger;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cn.myperf4j.asm;

import cn.myperf4j.core.AbstractRecorderMaintainer;
import cn.myperf4j.core.Recorders;
import cn.myperf4j.core.recorder.AbstractRecorderMaintainer;
import cn.myperf4j.core.recorder.Recorders;
import cn.myperf4j.base.config.ProfilingParams;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import cn.myperf4j.asm.ASMRecorderMaintainer;
import cn.myperf4j.core.MethodTagMaintainer;
import cn.myperf4j.core.Recorder;
import cn.myperf4j.core.recorder.Recorder;
import cn.myperf4j.base.util.Logger;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import cn.myperf4j.asm.ASMRecorderMaintainer;
import cn.myperf4j.asm.utils.TypeDescUtils;
import cn.myperf4j.base.MethodTag;
import cn.myperf4j.core.AbstractRecorderMaintainer;
import cn.myperf4j.core.recorder.AbstractRecorderMaintainer;
import cn.myperf4j.core.MethodTagMaintainer;
import cn.myperf4j.base.config.ProfilingConfig;
import org.objectweb.asm.MethodVisitor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,4 @@ private static void appendArrDesc(StringBuilder sb, int arrayLevel) {
}
}

public static void main(String[] args) {
System.out.println(getMethodParamsDesc("()V"));
System.out.println(getMethodParamsDesc("(IF)V"));
System.out.println(getMethodParamsDesc("(Ljava/lang/Object;)I"));
System.out.println(getMethodParamsDesc("(ILjava/lang/String;)[I"));
System.out.println(getMethodParamsDesc("(ILjava/lang/Map;)[I"));
System.out.println(getMethodParamsDesc("([I)Ljava/lang/Object;"));
System.out.println(getMethodParamsDesc("([ILjava/lang/Object;[Ljava/lang/Object;[Ljava/lang/String;)Ljava/lang/Object;"));
System.out.println(getMethodParamsDesc("([[ILjava/lang/Object;[[[Ljava/lang/Object;[[[[[Ljava/lang/String;)Ljava/lang/Object;"));
}

}
110 changes: 110 additions & 0 deletions MyPerf4J-ASM/src/test/java/MyPerf4J/ClassFileUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package MyPerf4J;

import cn.myperf4j.base.util.Logger;
import cn.myperf4j.core.recorder.RoughRecorder;

import java.io.*;
import java.net.URL;
import java.net.URLDecoder;
import java.util.Arrays;
import java.util.Enumeration;

/**
* Created by LinShunkang on 2018/10/17
*/
public class ClassFileUtils {

public static void main(String[] args) throws IOException {
System.out.println(Arrays.toString(getClassFileContent(RoughRecorder.class.getName())));
}

public static byte[] getClassFileContent(String fullClassName) throws IOException {
int idx = fullClassName.lastIndexOf(".");
String simpleClassName = fullClassName.substring(idx + 1);
File targetClassFile = getClasses(fullClassName.substring(0, idx), true, simpleClassName + ".class");
if (targetClassFile == null) {
return null;
}

return toByteArray(new FileInputStream(targetClassFile));
}

private static File getClasses(String basePackage, boolean recursive, String targetClassName) {
String packageName = basePackage;
if (packageName.endsWith(".")) {
packageName = packageName.substring(0, packageName.lastIndexOf('.'));
}

String package2Path = packageName.replace('.', '/');
try {
Enumeration<URL> dirs = Thread.currentThread().getContextClassLoader().getResources(package2Path);
while (dirs.hasMoreElements()) {
URL url = dirs.nextElement();
String protocol = url.getProtocol();
if ("file".equals(protocol)) {
Logger.debug("ClassScanner scanning file type class....");
String filePath = URLDecoder.decode(url.getFile(), "UTF-8");
File file = scanByFile(packageName, filePath, recursive, targetClassName);
if (file != null) {
return file;
}
}
}
} catch (IOException e) {
Logger.error("ClassScanner.getClasses(" + basePackage + ", " + recursive + ")", e);
}
return null;
}

private static File scanByFile(String packageName,
String packagePath,
final boolean recursive,
String targetClassName) {
File dir = new File(packagePath);
if (!dir.exists() || !dir.isDirectory()) {
return null;
}

File[] dirFiles = dir.listFiles(new FileFilter() {
// 自定义文件过滤规则
public boolean accept(File file) {
if (file.isDirectory()) {
return recursive;
}
return filterClassName(file.getName());
}
});

if (dirFiles == null) {
return null;
}

for (File file : dirFiles) {
if (file.isDirectory()) {
scanByFile(packageName + "." + file.getName(), file.getAbsolutePath(), recursive, targetClassName);
continue;
}

if (file.getName().equals(targetClassName)) {
return file;
}
}
return null;
}

private static boolean filterClassName(String className) {
return className.endsWith(".class");
}

private static byte[] toByteArray(InputStream in) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[1024 * 4];
int n;
while ((n = in.read(buffer)) != -1) {
out.write(buffer, 0, n);
}
return out.toByteArray();
}


}
17 changes: 17 additions & 0 deletions MyPerf4J-ASM/src/test/java/MyPerf4J/ClassToTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package MyPerf4J;

import cn.myperf4j.base.util.ThreadUtils;

import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;

/**
* Created by LinShunkang on 2018/10/18
*/
public class ClassToTest {

public String getStr() {
ThreadUtils.sleepQuietly(ThreadLocalRandom.current().nextInt(10), TimeUnit.MILLISECONDS);
return "111";
}
}
71 changes: 71 additions & 0 deletions MyPerf4J-ASM/src/test/java/MyPerf4J/PreMainTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package MyPerf4J;

import cn.myperf4j.asm.ASMBootstrap;
import cn.myperf4j.asm.aop.ProfilingTransformer;
import cn.myperf4j.base.constant.PropertyKeys;
import cn.myperf4j.base.constant.PropertyValues;
import cn.myperf4j.base.util.ThreadUtils;
import cn.myperf4j.base.util.file.AutoRollingFileWriter;
import cn.myperf4j.base.util.file.MinutelyRollingFileWriter;
import org.junit.Test;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.concurrent.TimeUnit;

/**
* Created by LinShunkang on 2018/10/17
*/
public class PreMainTest {

@Test
public void test() {
test(PropertyValues.METRICS_PROCESS_TYPE_STDOUT);
// test(PropertyValues.METRICS_PROCESS_TYPE_INFLUX_DB);
}

private void test(int metricsProcessorType) {
prepare(metricsProcessorType);
if (ASMBootstrap.getInstance().initial()) {
MyClassLoader loader = new MyClassLoader();
Class<?> aClass = loader.findClass("MyPerf4J.ClassToTest");
try {
Object obj = aClass.newInstance();
Method method = aClass.getMethod("getStr");
for (int i = 0; i < 100; ++i) {
method.invoke(obj);
}
} catch (Exception e) {
e.printStackTrace();
}
ThreadUtils.sleepQuietly(3, TimeUnit.SECONDS);
}
}

private void prepare(int metricsProcessorType) {
String propertiesFile = "/tmp/MyPerf4J.properties";
System.setProperty(PropertyKeys.PRO_FILE_NAME, propertiesFile);
AutoRollingFileWriter writer = new MinutelyRollingFileWriter(propertiesFile);
writer.write("AppName=MyPerf4JTest\n");
writer.write("MetricsProcessorType=" + metricsProcessorType + "\n");
writer.write("IncludePackages=MyPerf4J\n");
writer.write("MillTimeSlice=1000\n");
writer.closeFile(true);
}

public class MyClassLoader extends ClassLoader {
@Override
protected Class<?> findClass(String name) {
ProfilingTransformer transformer = new ProfilingTransformer();
Class<?> targetClass = ClassToTest.class;
try {
byte[] transformBytes = transformer.transform(PreMainTest.class.getClassLoader(), targetClass.getName(), targetClass, null, ClassFileUtils.getClassFileContent(targetClass.getName()));
return defineClass(name, transformBytes, 0, transformBytes.length);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}

}
25 changes: 25 additions & 0 deletions MyPerf4J-ASM/src/test/java/MyPerf4J/TypeDestUtilsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package MyPerf4J;

import org.junit.Assert;
import org.junit.Test;

import static cn.myperf4j.asm.utils.TypeDescUtils.getMethodParamsDesc;

/**
* Created by LinShunkang on 2018/10/19
*/
public class TypeDestUtilsTest {

@Test
public void test() {
Assert.assertEquals(getMethodParamsDesc("()V"),"");
Assert.assertEquals(getMethodParamsDesc("(IF)V"),"int, float");
Assert.assertEquals(getMethodParamsDesc("(Ljava/lang/Object;)I"),"Object");
Assert.assertEquals(getMethodParamsDesc("(ILjava/lang/String;)[I"),"int, String");
Assert.assertEquals(getMethodParamsDesc("(ILjava/lang/Map;)[I"),"int, Map");
Assert.assertEquals(getMethodParamsDesc("([I)Ljava/lang/Object;"),"int[]");
Assert.assertEquals(getMethodParamsDesc("([ILjava/lang/Object;[Ljava/lang/Object;[Ljava/lang/String;)Ljava/lang/Object;"),"int[], Object, Object[], String[]");
Assert.assertEquals(getMethodParamsDesc("([[ILjava/lang/Object;[[[Ljava/lang/Object;[[[[[Ljava/lang/String;)Ljava/lang/Object;"),"int[][], Object, Object[][][], String[][][][][]");
}

}
4 changes: 4 additions & 0 deletions MyPerf4J-ASM/src/test/java/MyPerf4J/test2/Test2.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package MyPerf4J.test2;

import cn.myperf4j.asm.ASMRecorderMaintainer;
import cn.myperf4j.asm.aop.ProfilingClassAdapter;
import cn.myperf4j.base.metric.processor.stdout.StdoutMethodMetricsProcessor;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter;
Expand All @@ -16,6 +18,8 @@
public class Test2 {

public static void main(String[] args) throws IOException {
ASMRecorderMaintainer instance = ASMRecorderMaintainer.getInstance();
instance.initial(new StdoutMethodMetricsProcessor(), true, 1);
test2();
// runNewFoo2();
}
Expand Down
8 changes: 8 additions & 0 deletions MyPerf4J-Base/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@
<maven.compiler.target>1.7</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* Created by LinShunkang on 2018/8/22
*/
public class DateFormatUtils {

private static final ThreadLocal<DateFormat> DEFAULT_DATE_FORMAT = new ThreadLocal<DateFormat>() {
@Override
protected DateFormat initialValue() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,4 @@ protected DecimalFormat initialValue() {
public static String getFormatStr(double num) {
return decimalFormat.get().format(num);
}

public static void main(String[] args) {
System.out.println(getFormatStr(10011.22222D));
System.out.println(getFormatStr(10011.22D));
System.out.println(getFormatStr(1.2222D));
System.out.println(getFormatStr(1.2D));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cn.myperf4j.base.util;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
Expand All @@ -19,4 +20,11 @@ public Thread newThread(Runnable r) {
};
}

public static void sleepQuietly(long time, TimeUnit timeUnit) {
try {
timeUnit.sleep(time);
} catch (Exception e) {
//empty
}
}
}
Loading

0 comments on commit 9b8d633

Please sign in to comment.