51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 12033|回复: 0

[原创] gcov和lcov对linux c++分析代码覆盖率

[复制链接]

该用户从未签到

发表于 2008-7-9 16:10:30 | 显示全部楼层 |阅读模式
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文件的覆盖率:


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


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

[ 本帖最后由 liangjz 于 2008-7-9 16:15 编辑 ]
回复

使用道具 举报

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

GMT+8, 2024-4-18 12:42 , Processed in 0.075247 second(s), 28 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

快速回复 返回顶部 返回列表