javaagent获得每个方法的执行时间

比如说在对项目的每个方法的执行时间进行监控的需求的时候,不可能对项目中成千上万的方法一个一个的进行处理,当然如果时间要求很宽松的可以 一个一个的进行添加处理。

但是使用字节码插桩的方法,可以比较方便的进行需求的实现。

拦截的时候使用的是 javaagent
编辑、修改 class 字节码文件的时候使用的是 javassist 技术

在引入 javassist 的jar包的时候,尽可能的选择高版本的jar包

<dependency>
    <groupId>javassist</groupId>
    <artifactId>javassist</artifactId>
    <version>3.12.1.GA</version>
</dependency>

下面这个是,插桩需要的代码,将该java文件打包成jar包,

然后在项目启动的时候 添加 -javaagent:yyyy.jar=aaa.bbb.ccc

java -Dfile.encoding=utf-8 -jar xxxx.jar -javaagent:yyyy.jar=aaa.bbb.ccc

xxxx.jar     表示项目的jar包

yyyy.jar     表示插桩的jar包

aaa.bbb.ccc    要监控的类文件夹,或者类文件

具体到类文件的话,会打印出类中所有方法的执行时间

具体到类文件夹的话,会打印出类中所有方法的执行时间+所有类的总执行时间(总时间实在最后才会打印出来)

/**
 * 监控所有的方法类
 *
 * Create by yang_zzu on 2020/6/14 on 20:35
 */
public class PublicAgentMain {

    //javaagent 入口方法

    // 以 arg 为前缀的类才会进行插桩处理 -javaagent:xxx.jar=com.sys.insertPile
    public static void premain(String arg, Instrumentation instrumentation) {

        System.out.println("hello agent!!!!!");

        final String config = arg;

        // 使用 javassist ,在运行时修改 class 字节码,就是 插桩
        final ClassPool pool = new ClassPool();
        pool.appendSystemPath();

        instrumentation.addTransformer(new ClassFileTransformer() {
            @Override
            public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {

                if (className == null || !className.replaceAll("/",".").startsWith(config)) {
                    return null;
                }

                try {
                    className = className.replaceAll("/", ".");
                    CtClass ctClass = pool.get(className);
                    // 获得类中的所有方法
                    for (CtMethod declaredMethod : ctClass.getDeclaredMethods()) {
                        newMethod(declaredMethod);
                    }
                    return ctClass.toBytecode();
                } catch (Exception e) {
                    e.printStackTrace();
                }

                return null;
            }
        });


    }

    //复制原有的方法(类似于使用 agent )
    private static CtMethod newMethod(CtMethod oldMethod) {
        CtMethod copy = null;
        try {
            //1. 将方法进行复制
            copy = CtNewMethod.copy(oldMethod, oldMethod.getDeclaringClass(), null);
            //类似于使用动态代理
            copy.setName(oldMethod.getName() + "$agent");
            //类文件中添加 sayHello$agent 方法
            oldMethod.getDeclaringClass().addMethod(copy);

            //2. 改变原有的方法,将 原有的 sayHello 方法进行重写操作
            if (oldMethod.getReturnType().equals(CtClass.voidType)) {
                oldMethod.setBody(String.format(voidSource, oldMethod.getName()));
            } else {
                oldMethod.setBody(String.format(source, oldMethod.getName()));
            }
        } catch (CannotCompileException | NotFoundException e) {
            e.printStackTrace();
        }
        return copy;

    }

    /**
     * 参数的封装
     * $$ ======》 arg1, arg2, arg3
     * $1 ======》 arg1
     * $2 ======》 arg2
     * $3 ======》 arg3
     * $args ======》 Object[]
     */
    //有返回值得方法
    final static String source = "{ long begin = System.currentTimeMillis();\n" +
            "        Object result;\n" +
            "        try {\n" +
            "            result = ($w)%s$agent($$);\n" + //s% 将参数传递到下一个方法,然后使用 s% 传递的参数进行替换操作, $w 表示的是在进行return的时候会强制的进行类型转换
            "        } finally {\n" +
            "            long end = System.currentTimeMillis();\n" +
            "            System.out.println(end - begin);\n" +
            "        }\n" +
            "        return ($r) result;}";

    //没有返回值的方法
    final static String voidSource = "{long begin = System.currentTimeMillis();\n" +
            "        try {\n" +
            "            %s$agent($$);\n" +
            "        } finally {\n" +
            "            long end = System.currentTimeMillis();\n" +
            "            System.out.println(end - begin);\n" +
            "        }}";


}

 监听的类的文件夹

 

pom 文件夹,打包的配置

<!-- 需要打包的类的 -->
<Premain-Class>com.sys.insertPilepublic.PublicAgentMain</Premain-Class>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.sys.yang</groupId>
    <artifactId>mybatisYang</artifactId>
    <version>1.0-SNAPSHOT</version>


    <dependencies>
        <dependency>
            <groupId>javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.12.1.GA</version>
        </dependency>


        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.17</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.1</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.1.0.RELEASE</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-jcl</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.0.RELEASE</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-jcl</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.2</version>
        </dependency>

    </dependencies>


    <build>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <archive>
                        <manifestEntries>
                            <Project-name>${project.name}</Project-name>
                            <Project-version>${project.version}</Project-version>
<!--                            <Premain-Class>com.coderead.agent.AgentMain</Premain-Class>-->
<!--                            <Premain-Class>com.sys.insertPilepublic.AgentMain</Premain-Class>-->
                            <!-- 需要打包的类的 -->
                            <Premain-Class>com.sys.insertPilepublic.PublicAgentMain</Premain-Class>
                            <Can-Redefine-Classes>false</Can-Redefine-Classes>
                            <Can-Retransform-Classes>false</Can-Retransform-Classes>
                        </manifestEntries>
                    </archive>
                    <skip>true</skip>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>utf8</encoding>
                </configuration>
            </plugin>
        </plugins>

        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/test/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>



</project>

UserService.java 文件

/**
 *
 * 字节码插桩
 * 运行的时候需要附着于其他的jar 包,运行
 * java -jar xxx.jar -javaagent:xxxx.jar
 *
 * Create by yang_zzu on 2020/6/14 on 17:35
 */
public class UserService {

    public void sayHello(String s) throws InterruptedException {
        Thread.sleep(50);
        System.out.println("hello world!!! " + s);
    }


    public Integer sayHelloReturn(String s, Integer age) throws InterruptedException {
        Thread.sleep(100);
        System.out.println("hello world!!! " + s + "age = " + age);
        return age;
    }

    public String sayHelloReturnEvery(String name, Integer age, String phone) throws InterruptedException {
        Thread.sleep(200);
        return (name + " 的年龄是 " + age + " 电话是 " + phone);
    }


}

 

AgentTest.java 测试类

/**
 * Create by yang_zzu on 2020/6/14 on 17:58
 */
public class AgentTest {

    public static void main(String[] args) {
        UserService userService = new UserService();

        UserService2 userService2 = new UserService2();
        try {
            userService.sayHello("你好世界@@@@");
            System.out.println("----------------------------------");
            System.out.println("年龄" + userService.sayHelloReturn("萨瓦迪卡", 18));
            System.out.println("----------------------------------");
            System.out.println(userService.sayHelloReturnEvery("小当家", 13, "110110110"));

            System.out.println("+++++++++++++++++++++++++++++++++++");

            userService2.sayHello("你好我的朋友!!!!!");
            System.out.println("----------------------------------");

            System.out.println(userService2.sayHelloReturn("xiaohua", 21));
            System.out.println("----------------------------------");
            System.out.println(userService2.sayHelloReturnEvery("haha", 10, "120110"));
            System.out.println("----------------------------------");


        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

}

项目的结构

本来是不想粘贴测试类的代码,因为粘上代码看着内容比较长,也没有多大的实际意义。

https://blog.csdn.net/yang_zzu/article/details/106750330

这个是另外一个,javaagent 的使用,打印 idea 的所有类文件。这个只是作为学习使用,并没有什么实际性的应用。在破解 idea 的时候应用的就是 javaagent 技术

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值