Jacoco动态监测Web项目的覆盖率

 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() {
      }
  }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值