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

相关推荐
爱码驱动4 分钟前
Java多线程详解(5)
java·开发语言·多线程
橘子编程9 分钟前
计算机内存与缓存完全指南
java·计算机网络·spring·缓存
杰克尼9 分钟前
springCloud(day09-Elasticsearch02)
java·后端·spring·spring cloud
云烟成雨TD14 分钟前
Spring AI 1.x 系列【24】结构化输出 API
java·人工智能·spring
han_hanker14 分钟前
springboot 不推荐使用@Autowired怎么处理
java·spring boot·后端
最初的↘那颗心16 分钟前
LangChain4j入门:集成SpringBoot与核心概念全解析
java·spring boot·ai·大模型·langchain4j
计算机学姐18 分钟前
基于SpringBoot的高校实验室预约管理系统
java·spring boot·后端·mysql·spring·信息可视化·tomcat
九转成圣18 分钟前
实战记录:用 Java 拼接长图/网格图,我踩了哪些坑?
java·开发语言
lzhdim23 分钟前
SQL 入门 9:SQL 高级子查询:ANY、EXISTS 与多位置应用
java·开发语言·数据库·sql·mysql
杰克尼30 分钟前
springCloud_day08(Elasticsearch)
java·spring·spring cloud