Java应用程序的测试覆盖率之设计与实现(二)-- jacoco agent

说在前面的话

要想获得测试覆盖率报告,第一步要做的是,采集覆盖率数据,并输入到tcp。

而本文便是介绍一种java应用程序部署下的推荐方式。

作为一种通用方案,首先不想对应用程序有所侵入,其次运维和管理方便。

正好,jacoco agent就是类似于pinpoint agent一样,都使用了javaagent技术。

在灰度部署、压力测试等都许多场景下,都适用于javaagent技术。

一、jacoco agent

java agent技术,对jar应用程序没有入侵。

其作用是采集覆盖率数据,建议采用tcpserver的方式往外部输出。

安装简单,启动脚本增加以下参数:

bash 复制代码
basepath=/opt
# 本机IP
LOCAL_IP=192.168.10.128
# 服务名称
service=

# java包名是com.tech.*
java -javaagent:${basepath}/jacocoagent.jar=includes=com.tech.*,output=tcpserver,address=${LOCAL_IP},port=9322,classdumpdir=${basepath}/${service}/classdumpdir/classes/  -jar xxx.jar

二、参数说明

1、includes

默认是* (all classes) , 填写的是项目的class文件所在包package,多个之间使用冒号隔开。

2、output

  • file: At VM termination execution data is written to the file specified in the destfile attribute.
  • tcpserver: The agent listens for incoming connections on the TCP port specified by the address and port attribute. Execution data is written to this TCP connection.
  • tcpclient: At startup the agent connects to the TCP port specified by the address and port attribute. Execution data is written to this TCP connection.
  • none: Do not produce any output.

填写的是tcpserver,支持远程读取覆盖率数据。而file是保存于本地目录。

3、host

tcp server地址

4、port

tcp server端口

5、classdumpdir

class文件所在目录

下图为本地启动的测试结果。

6、append

是否追加,默认是true,设置为false时,每次重新生成。

三、总结

bash 复制代码
> ss -anlp |grep 9122
tcp  LISTEN  0  1  [::ffff:192.168.80.148]:9122  [::]:*  users:(("java",pid=18901,fd=10))

接下来,我们就可以远程连接tcpserver,dump出覆盖率数据了。

相关推荐
zuowei28894 分钟前
spring实例化对象的几种方式(使用XML配置文件)
xml·java·spring
C、空白格5 分钟前
Java集成Vosk实现离线语音识别
java·开发语言·语音识别
阿巴斯甜12 分钟前
preProcessHandler: (AppException) -> Boolean用法:
java
Lyyaoo.24 分钟前
【JAVA基础面经】native方法
java·开发语言
牛十二26 分钟前
nacos2.4连接出错源码分析
java·linux·开发语言
阿巴斯甜33 分钟前
userList.stream().sorted((u1, u2) -> u2.getAge() - u1.getAge()); 怎么判断是升序还是降序?
java
小松加哲34 分钟前
AspectJ编译期织入实战
java·开发语言
贺小涛36 分钟前
python和golang进程、线程、协程区别
java·python·golang
Seven971 小时前
Tomcat的架构设计和启动过程详解
java
Mr-Wanter1 小时前
踩坑记录:IDEA 启动服务连续三次 OOM 内存溢出完整解决
java·ide·intellij-idea·oom