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出覆盖率数据了。

相关推荐
xuzhiqiang072415 小时前
Java进阶之路,Java程序员职业发展规划
java·开发语言
时艰.16 小时前
订单系统历史数据归档方案
java
一只叫煤球的猫18 小时前
ThreadForge v1.1.0 发布:让 Java 并发更接近 Go 的开发体验
java·后端·性能优化
014.18 小时前
2025最新jenkins保姆级教程!!!
java·运维·spring boot·spring·jenkins
leifengzhy18 小时前
Jenkins使用pipeline和maven创建项目
运维·jenkins·maven
浣熊88818 小时前
天机学堂虚拟机静态ip无法使用(重启后ip:192.168.150.101无法使用连接Mobaxterm数据库等等,或者无法使用修改之后的Hosts域名去访问nacos,jenkins)
java·微服务·虚拟机·天机学堂·重启之后静态ip用不了
辣椒种子18 小时前
Elasticsearch 基础入门--开发elasticsearch搜索数据篇
大数据·elasticsearch·jenkins
码农阿豪18 小时前
Jenkins Git 克隆失败深度解析:从 “Connection reset by peer“ 到彻底解决
运维·git·jenkins
莫比乌斯之梦18 小时前
使用 Docker 运行 Jenkins:快速搭建高效 CI/CD 环境指南
ci/cd·docker·jenkins
心 -18 小时前
java八股文IOC
java