Jacoco动态监测Web项目的覆盖率
我自己曾经尝试用python的urllib自己dump数据,结果tomcat的后台报错。
java.io.IOException: Invalid execution data file.
at org.jacoco.agent.rt.internal_932a715.core.data.ExecutionDataReader.read(ExecutionDataReader.java:84)
at org.jacoco.agent.rt.internal_932a715.output.TcpConnection.run(TcpConnection.java:59)
at org.jacoco.agent.rt.internal_932a715.output.TcpServerOutput$1.run(TcpServerOutput.java:63)
at java.lang.Thread.run(Thread.java:619)
|
好奇为啥不行。翻阅Jacoco官网上的文档,找到了它的一个demo,修改参数连接到我的tomcat下dump数据,好使。
具体的配置和代码链接贴到这里,希望大家有用。希望后面有时间能扒拉一下源代码看看,本地化成自己的。
配置
下载jacoco的jar包,放到某个地方,然后在catalina.sh中配置如下:
JAVA_OPTS="-server -Xms2000m -Xmx2000m -Xmn800m -XX:PermSize=64m
-XX:MaxPermSize=256m -XX:SurvivorRatio=4 -XX:+UseConcMarkSweepGC
-XX:MaxTenuringThreshold=15 -Dfile.encoding=utf8 -Duser.language=zh
-javaagent:/home/auser/myproject/apache-tomcat-6.0.37/lib/
jacocoagent.jar=includes=com.xxx.*,output=tcpserver,port=8494,
address=10.10.10.10"
当然,针对jaococ,真正有用的是-javaagent=yourpath/jacocoagent.jar=[opt]=[val],[opt]=[val]这段:
-javaagent:/home/iteqa/cms/apache-tomcat-6.0.37/lib/
jacocoagent.jar=includes=com.baidu.*,output=tcpserver,port=8494,
address=10.10.10.10"
dump数据
http://www.eclemma.org/jacoco/trunk/doc/examples/java/ExecutionDataClient.java
测试ok,能够dump出数据
生成报告
http://www.eclemma.org/jacoco/trunk/doc/examples/java/ReportGenerator.java
测试报错,can't find class loader ....
找个下班时间继续研究研究
maven依赖
该程序使用maven管理时的依赖
<dependency>
<groupId>org.jacoco</groupId>
<artifactId>org.jacoco.core</artifactId>
<version>0.7.1.201405082137</version>
</dependency>
<dependency>
<groupId>org.jacoco</groupId>
<artifactId>org.jacoco.report</artifactId>
<version>0.7.1.201405082137</version>
</dependency>
ExecutionDataClient
用于生成xxx.exec数据的程序
public class ExecutionDataClient {
private static final String DESTFILE = "jacoco-client.exec";
private static final String ADDRESS = "10.xx.xx.xx";
private static final int PORT = 8494;
/**
* Starts the execution data request.
*
* @param args
* @throws IOException
*/
public static void main(final String[] args) throws IOException {
final FileOutputStream localFile = new FileOutputStream(DESTFILE);
final ExecutionDataWriter localWriter = new ExecutionDataWriter(
localFile);
// Open a socket to the coverage agent:
final Socket socket = new Socket(InetAddress.getByName(ADDRESS), PORT);
final RemoteControlWriter writer = new RemoteControlWriter(
socket.getOutputStream());
final RemoteControlReader reader = new RemoteControlReader(
socket.getInputStream());
reader.setSessionInfoVisitor(localWriter);
reader.setExecutionDataVisitor(localWriter);
// Send a dump command and read the response:
writer.visitDumpCommand(true, false);
reader.read();
socket.close();
localFile.close();
}
private ExecutionDataClient() {
}
}
|