jacoco統計新增代碼覆蓋率踩過的坑


緣起:

比較正在開發的branch和master代碼的區別,然后統計新增代碼覆蓋率。

步驟:

a,代碼覆蓋率是通過分析exec文件來比較的,exec文件是通過ant從服務器中dump下來的,調用jacoco也是通過ant的build.xml文件來實現的,所以,branch需要從ant文件傳給jacoco;

b,傳給jacoco之后,通過jgit統計變更的代碼;

c,在jacoco代碼統計的入口處,將不需要統計的行數和類過濾掉;

踩過的坑:

1,莫名其妙的不能不能clone多個branch,最后莫名其妙的好了,至今沒搞明白為什么。

command.setBranchesToClone(Arrays.asList("refs/heads/master","refs/heads/"+branch));

2,運行ant的xml文件的時候出現類檢查錯誤,這個是因為加載類的時候會對類進行類檢查,不通過的時候就會報這個錯。

Type top (current frame, locals[11]) is not assignable to integer 。

這個是jvm虛擬機的機制,可以通過設置jvm的參數來去掉檢查。

對於java7而言,需要添加-XX:-UseSplitVerifier(已實踐)。 
對於java8而言,需要添加-noverify

ant如何設置jvm參數?下面是官方文檔

The Ant wrapper script for Unix will source (read and evaluate) the file ~/.antrc before it does anything. On Windows, the Ant wrapper batch-file invokes %HOME%\antrc_pre.bat at the start and %HOME%\antrc_post.bat at the end. You can use these files, for example, to set/unset environment variables that should only be visible during the execution of Ant. See the next section for examples.

我用的mac電腦,通過添加~/.antrc文件,文件中添加ANT_OPTS="-noverify",設置jvm參數,ant運行.xml文件的時候,會自動加載這個文件設置jvm的參數。

3,如何把branch和repository傳進去,通過給group添加兩個參數將它們傳進去,不過這個要讓ant認識這兩個屬性attribute,根據ant的官方文檔需要更改jacoco的reportTask類支持新屬性。
public void setRepository(final String repository) {
this.repository = repository;
}
public void setBranch(final String branch) {
this.branch = branch;
}
分析新增代碼覆蓋率的思路:
1,將分支和master的代碼拉下來,獲取到新增加和修改的類addOrModifiedClasses集合,分析代碼的時候只分析addOrModifiedClasses,將不是addOrModifiedClasses集合中的代碼過濾調。分兩個層次來過濾
    a,過濾掉不在addOrModifiedClasses里面的類。
    b,過濾掉沒有更改的行,只分析更改的行數。
2,分析新增代碼覆蓋率的實現方式:
    a,用jgit開源軟件,獲取到兩個分支的代碼的區別DiffEntity。
    b,Analyzer中的analyzeAll類文件的分析的入口處過濾掉,紅色的部分添加的過濾類文件的代碼。
public int analyzeAll(final File file) throws IOException {
int count = 0;
if (file.isDirectory()) {
for (final File f : file.listFiles()) {
count += analyzeAll(f);
}
} else {
String filePath=file.getPath();
if(filePath.endsWith(CLASS_SUFFIX)) {
String className = filePath.substring(CLASS_PREFIX.length(), (file.getPath().length() - CLASS_SUFFIX.length()));
//只分析修改或者添加的class
if (isNewClass(className)) {
final InputStream in = new FileInputStream(file);
try {
System.out.println("正在分析的className:" + className);
count += analyzeAll(in, file.getPath());
} finally {
in.close();
}
}
}else {

}
}
return count;
}
     c,ClassAnalyzer過濾沒有修改的method,獲取到修改和新增加的的method,統計修改了和新增加的的method,忽略未修改的method。紅色部分為新增加的代碼。
return new MethodAnalyzer(stringPool.get(name), stringPool.get(desc),
stringPool.get(signature), probes) {
@Override
public void visitEnd() {
super.visitEnd();
final IMethodCoverage methodCoverage = getCoverage();
if (methodCoverage.getInstructionCounter().getTotalCount() > 0) {
// Only consider methods that actually contain code
final int firstLine = methodCoverage.getFirstLine();
final int lastLine = methodCoverage.getLastLine();
for (int i = firstLine; i <= lastLine; i++) {
//對於更改的類:只分析更改的方法,對於沒有更改的方法不進行分析diffLines.size()>0&&diffLines.contains(i)
//對於增加的類:分析所有的diffLines.size()==0
if((diffLines.size()>0&&diffLines.contains(i))||diffLines.size()==0){
coverage.addMethod(methodCoverage,diffLines);
break;
}
}

}
}
};
    d,SourceNodeImpl過濾新增加和修改的行,統計這部分代碼的覆蓋率,忽略未修改和未增加的行。紅色部分是新增加的部分
public void increment(final ISourceNode child,List<Integer> diffLines) {
instructionCounter = instructionCounter.increment(child
.getInstructionCounter());
branchCounter = branchCounter.increment(child.getBranchCounter());
complexityCounter = complexityCounter.increment(child
.getComplexityCounter());
methodCounter = methodCounter.increment(child.getMethodCounter());
classCounter = classCounter.increment(child.getClassCounter());
final int firstLine = child.getFirstLine();
if (firstLine != UNKNOWN_LINE) {
final int lastLine = child.getLastLine();
ensureCapacity(firstLine, lastLine);
for (int i = firstLine; i <= lastLine; i++) {
//只分析增加和修改的line的的覆蓋率
//對於更改的類的方法:diffLines.contains(i)如果包含修改的行的話,就分析這行
//對於新增加類的方法diffLines.size()==0分析所有的
if(diffLines.contains(i)||diffLines.size()==0){
final ILine line = child.getLine(i);
incrementLine(line.getInstructionCounter(),
line.getBranchCounter(), i);
}
}
}
}

研究 jacoco 的入口:
ReportGenerator
資料:
jgit使用指南:https://github.com/centic9/jgit-cookbook;
ant文檔:http://ant.apache.org/manual/running.html;
jgit相關博客:https://blog.csdn.net/chen136428/article/details/8904416?utm_source=copy;
解決類校驗的錯誤的相關博客:https://www.cnblogs.com/zhangcybb/p/4897417.html
jgit document:  http://download.eclipse.org/jgit/site/5.1.1.201809181055-r/apidocs/index.html
有贊的jacoco簡介: https://testerhome.com/articles/16981
 
問題:
改了class碼之后不會對程序造成什么異常嗎?但是改了字節碼,真正的程序的運行不是用的字節碼嗎?
為什么不是侵入式的?因為他沒有改源代碼,源代碼雖然沒有改。
如何更改字節碼的?通過ClassFileTransformer類的transform防范。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM