7.18_23期业务层接口执行效率

2023-07-23  本文已影响0人  萧修

本文将之前学习的内容整理,写一个万次执行耗时案例

导入的依赖坐标有
德鲁伊操作数据库,myql、mybatis

<dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.1.16</version>
    </dependency>

    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.5.13</version>
    </dependency>
    
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.32</version>
    </dependency>

Spring系列
基础spring-context
Spring测试spring-test
Spring的jdbcspring-jdbc
mybatis的Springmybatis-spring
切面aspectjweaver

<dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.2.10.RELEASE</version>
    </dependency>
    
    
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>5.2.10.RELEASE</version>
    </dependency>
    
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>5.2.10.RELEASE</version>
    </dependency>
    
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>1.3.0</version>
    </dependency>
    
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.9.4</version>
      <scope>compile</scope>
    </dependency>

单元测试

<dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.13.2</version>
      <scope>test</scope>
    </dependency>

执行万次耗时,需要使用到切面类型的环绕,执行方法前后获取系统时间,取差值。在环绕中有一个比较关键的类
ProceedingJoinPoint

pjp能获取到此方法运行的签名信息,运行的类以及方法

比如打印

万次耗时:interface com.itheima.service.BookServicefindByid-----2736ms

    public void runSpeed(ProceedingJoinPoint pjp) throws Throwable {

        Long startTime = System.currentTimeMillis();
        for (int i = 0; i < 10000; i++) {
            pjp.proceed();
        }
        Long endTime = System.currentTimeMillis();

        Signature signature = pjp.getSignature();
        Class className = signature.getDeclaringType();
        System.out.println("getDeclaringType:" + className);

        String methodName =  signature.getName();
        System.out.println("getName:" + methodName);

        System.out.println("万次耗时:" + className + methodName + "-----"+(endTime - startTime) + "ms");
    }
上一篇 下一篇

猜你喜欢

热点阅读