forked from LinShunKang/MyPerf4J
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
LinShunKang
committed
Oct 18, 2018
1 parent
53f0fbe
commit 9b8d633
Showing
36 changed files
with
549 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
MyPerf4J-ASM/src/main/java/cn/myperf4j/asm/ASMRecorderMaintainer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
MyPerf4J-ASM/src/test/java/MyPerf4J/ClassFileUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
25
MyPerf4J-ASM/src/test/java/MyPerf4J/TypeDestUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[][][][][]"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.