gcov和lcov对linux c++分析代码覆盖率

来源:互联网 发布:初学java看什么书 编辑:程序博客网 时间:2024/05/19 20:01
gcov和lcov对linux c++分析代码覆盖率
2010年06月24日 星期四 下午 2:21

gcov伴随gcc 发布。gcc编译加入-fprofile-arcs -ftest-coverage 参数生成二进制程序,执行测试用例生成代码覆盖率信息。

fprofile-arcs参数使gcc创建一个程序的流图,之后找到适合图的生成树。

只有不在生成树中的弧被操纵(instrumented):gcc添加了代码来清点这些弧执行的次数。

当这段弧是一个块的唯一出口或入口时,操纵工具代码(instrumentation code)将会添加到块中,

否则创建一个基础块来包含操纵工具代码。gcov主要使用.gcno和.gcda两个文件。

.gcno是由-ftest-coverage产生的,它包含了重建基本块图和相应的块的源码的行号的信息。

.gcda是由加了-fprofile-arcs编译参数的编译后的文件运行所产生的,它包含了弧跳变的次数和其他的概要信息。

Gcov执行函数覆盖、语句覆盖和分支覆盖。

Lcov则是上的gcov 结果展现的一个前端,可从 http://ltp.sourceforge.net/coverage/lcov.php 下载。

可以将覆盖率信息转换成html 展现。

安装lcov:su - root;make install

Makefile 在编译和link环节都加入 -fprofile-arcs -ftest-coverage 选项

GCC = g++ -fprofile-arcs -ftest-coverage

.SUFFIXES: .o .cpp

iquery: $(LIBS) TestQuery.o

$(GCC) $(LDPATH) -g -o $@ TestQuery.o -lsearch -lupdate -lbuild -lstore -lanalysis -lconfig -ldocument

-lmxml -lonline -lutility -ldictionary -lpublic -lpthread -lrt

.cpp.o:

$(GCC) -c -g $(INCLUDE) -DLINUX -o $@ $<

执行完iquery命令行。

[search@b2b_search_211 core]$./iquery -f ~/eragon_yb/conf/se.conf -s "offer_gb?q=mp3"

对于apache module的代码覆盖率分析,必须是启动apache httpd进程,执行查询最后退出apache httpd进程

才能收集到信息。

[search@b2b_search_211 core]$ ll

总用量 36120

drwxrwxr-x 4 search search 4096 7月 8 19:23 cpp

-rwxrwxr-x 1 search search 8742605 7月 8 20:06 ibuild

-rwxrwxr-x 1 search search 13490318 7月 8 20:06 idelete

-rwxrwxr-x 1 search search 13711848 7月 8 20:06 iquery

-rw-rw-r-- 1 search search 3115 7月 8 20:04 Makefile

drwxrwxr-x 3 search search 4096 7月 8 19:23 test

-rw-rw-r-- 1 search search 893 6月 12 18:18 TestAnalysis.cpp

-rw-rw-r-- 1 search search 10551 6月 12 18:18 TestBuild.cpp

-rw-rw-r-- 1 search search 15080 7月 8 20:06 TestBuild.gcno

-rw-rw-r-- 1 search search 115808 7月 8 20:06 TestBuild.o

-rw-rw-r-- 1 search search 1143 6月 12 18:18 TestConfig.cpp

-rw-rw-r-- 1 search search 5366 6月 12 18:18 TestDelete.cpp

-rw-rw-r-- 1 search search 11204 7月 8 20:06 TestDelete.gcno

-rw-rw-r-- 1 search search 252064 7月 8 20:06 TestDelete.o

生成: TestQuery.gcda、 TestQuery.gcno

[search@b2b_search_211 core]$ gcov TestQuery.cpp

File `TestQuery.cpp’

Lines executed:22.32% of 336

TestQuery.cpp:creating `TestQuery.cpp.gcov’

[search@b2b_search_211 core]$ ll

总用量 36620

-rw-rw-r-- 1 search search 7024 7月 8 20:08 allocator.h.gcov

drwxrwxr-x 4 search search 4096 7月 8 19:23 cpp

-rw-rw-r-- 1 search search 12827 7月 8 20:08 GlobalDef.h.gcov

-rwxrwxr-x 1 search search 8742605 7月 8 20:06 ibuild

-rwxrwxr-x 1 search search 13490318 7月 8 20:06 idelete

-rw-rw-r-- 1 search search 44797 7月 8 20:08 ios_base.h.gcov

-rw-rw-r-- 1 search search 4638 7月 8 20:08 iostream.gcov

-rwxrwxr-x 1 search search 13711848 7月 8 20:06 iquery

-rw-rw-r-- 1 search search 128499 7月 8 20:08 locale_facets.tcc.gcov

-rw-rw-r-- 1 search search 3115 7月 8 20:04 Makefile

-rw-rw-r-- 1 search search 12684 7月 8 20:08 MemCache.h.gcov

-rw-rw-r-- 1 search search 10158 7月 8 20:08 MemPool.h.gcov

-rw-rw-r-- 1 search search 6524 7月 8 20:08 new_allocator.h.gcov

-rw-rw-r-- 1 search search 5742 7月 8 20:08 new.gcov

-rw-rw-r-- 1 search search 1844 7月 8 20:08 QueryCache.h.gcov

-rw-rw-r-- 1 search search 44015 7月 8 20:08 stl_algobase.h.gcov

-rw-rw-r-- 1 search search 8328 7月 8 20:08 stl_construct.h.gcov

-rw-rw-r-- 1 search search 44016 7月 8 20:08 stl_function.h.gcov

-rw-rw-r-- 1 search search 31113 7月 8 20:08 stl_multiset.h.gcov

-rw-rw-r-- 1 search search 62978 7月 8 20:08 stl_tree.h.gcov

-rw-rw-r-- 1 search search 10365 7月 8 20:08 Svector.h.gcov

[search@b2b_search_211 core]$ cat TestQuery.cpp.gcov

-: 47:static int nAverageDocSize = 1024;

function _ZN9QueryStatC1EPKcxi called 0 returned 0% blocks executed 0%

#####: 53: QueryStat(const char* szQuery, n64_t d, n32_t docs){

#####: 54: query = szQuery;

#####: 55: dual = d;

#####: 56: docnum = docs;

-: 57: }

-: 58:};

-: 59:struct CmpQueryStat{

function _ZN12CmpQueryStatclERK9QueryStatS2_ called 0 returned 0% blocks executed 0%

#####: 60: bool operator()(const QueryStat& a, const QueryStat& b){

#####: 61: return a.dual < b.dual;

-: 62: };

-: 63:};

1: 534:}

带 #####表示未执行的行

[search@b2b_search_211 core]$

[search@b2b_search_211 core]$ ll *gcov*

收集覆盖率数据生成app.info文件

[search@b2b_search_211 core]$ lcov --directory . --capture --output-file app.info

Capturing coverage data from .

Found gcov version: 3.4.6

Scanning . for .gcda files ...

Found 1 data files in .

Processing ./TestQuery.gcda

Finished .info-file creation

转换成html格式

[search@b2b_search_211 core]$ genhtml -o results app.info

Reading data file app.info

Found 18 entries.

Found common filename prefix "/home/search/isearch_yb/src"

Writing .css and .png files.

Generating output.

Processing file cpp/core/basis/GlobalDef.h

Processing file cpp/core/search/QueryCache.h

...

Writing directory view page.

Overall coverage rate: 117 of 514 lines (22.8%)

将results目录tar cvf 打包sz到windows,打开目录夹包含:

总体报告:

单个cpp文件的覆盖率:

还可以看到具体的行执行情况

另外再运行一组更丰富的查询日志,测试结果截然不同。


类别:自动化测试,覆盖测试,测试工程||添加到搜藏|分享到i贴吧|浏览(794)|评论 
原创粉丝点击