测试代码覆盖率-GCOV的使用

来源:互联网 发布:php header跳转404 编辑:程序博客网 时间:2024/05/23 16:49

1. gcov是什么?
  • Gcov is GCC Coverage
  • 是一个测试代码覆盖率的工具
  • 是一个命令行方式的控制台程序
  • 伴随GCC发布配合GCC共同实现对C/C++文件的语句覆盖和分支覆盖测试
  • 与程序概要分析工具(profiling tool,例如gprof)一起工作,可以估计程序中哪一段代码最耗时;

注:程序概要分析工具是分析代码性能的工具。

2. gcov能做什么?

gcov可以统计

  • 每一行代码的执行频率
  • 实际上哪些代码确实被执行了
  • 每一段代码(section code)的耗时(执行时间)

因此,gcov可以帮你优化代码,当然这个优化动作还是应该有开发者完成。

 

3. 如何使用gcov

[html] view plain copy print?
  1. #include <iostream>  
  2. using namespace std;  
  3. int main()  
  4. {  
  5.     int i,total;  
  6.     total = 0;  
  7.     for (i=0; i<10; i++)  
  8.         total += i;  
  9.     if (total != 45)  
  10.         cout << "Failure!" << endl;  
  11.     else  
  12.         cout << "Success!" << endl;  
  13.     return 0;  
  14. }  

3.1 使用gcov3个阶段

(1) 编译

             

[html] view plain copy print?
  1. <strong>     </strong>$ g++ -fprofile-arcs -ftest-coverage -o main main.cpp  
[html] view plain copy print?
  1. <span style="font-family: Arial; font-size: 14px;">          $   ls</span>  

[html] view plain copy print?
  1. -rwxrwxr-x. 1 heli heli 20281 Dec  7 11:54 main  
  2. -rw-rw-r--. 1 heli heli   217 Dec  7 10:57 main.cpp  
  3. -rw-rw-r--. 1 heli heli  6632 Dec  7 11:54 <strong>main.gcno</strong>  

-fprofile-arcs  -ftest-coverage告诉编译器生成gcov需要的额外信息,并在目标文件中插入gcov需要的extra profiling information。因此,该命令在生成可执行文件test的同时生成mian.gcno文件(gcov note文件)

(2) 收集信息


执行该程序,生成main.gcda文件(gcov data文件)

 

(3) 报告

[html] view plain copy print?
  1. $ ./main   
  2. Success!  
  3. $ ll  
  4. total 36  
  5. -rwxrwxr-x. 1 heli heli 20281 Dec  7 11:54 main  
  6. -rw-rw-r--. 1 heli heli   217 Dec  7 10:57 main.cpp  
  7. -rw-rw-r--. 1 heli heli   784 Dec  7 11:56 <strong>main.gcda</strong>  
  8. -rw-rw-r--. 1 heli heli  6632 Dec  7 11:54 <strong>main.gcno</strong>  
[html] view plain copy print?
  1. $ gcov main.cpp   
  2. File '/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/ios_base.h'  
  3. Lines executed:0.00% of 2  
  4. /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/ios_base.h:creating 'ios_base.h.gcov'  
  5.   
  6. File '/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/ostream'  
  7. Lines executed:0.00% of 11  
  8. /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/ostream:creating 'ostream.gcov'  
  9.   
  10. File '/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/basic_ios.h'  
  11. Lines executed:0.00% of 10  
  12. /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/basic_ios.h:creating 'basic_ios.h.gcov'  
  13.   
  14. File 'main.cpp'  
  15. Lines executed:88.89% of 9  
  16. main.cpp:creating 'main.cpp.gcov'  
  17.   
  18. File '/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/iostream'  
  19. Lines executed:100.00% of 1  
  20. /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/iostream:creating 'iostream.gcov'  
  21.   
  22. File '/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/char_traits.h'  
  23. Lines executed:0.00% of 2  
  24. /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/char_traits.h:creating 'char_traits.h.gcov'  
  25.   
  26. File '/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/locale_facets.h'  
  27. Lines executed:0.00% of 5  
  28. /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/locale_facets.h:creating 'locale_facets.h.gcov'  
[html] view plain copy print?
  1. $ ll  
  2. total 300  
  3. -rw-rw-r--. 1 heli heli  22753 Dec  7 11:57 basic_ios.h.gcov  
  4. -rw-rw-r--. 1 heli heli  25834 Dec  7 11:57 char_traits.h.gcov  
  5. -rw-rw-r--. 1 heli heli  44130 Dec  7 11:57 ios_base.h.gcov  
  6. -rw-rw-r--. 1 heli heli   4084 Dec  7 11:57 iostream.gcov  
  7. -rw-rw-r--. 1 heli heli 132403 Dec  7 11:57 locale_facets.h.gcov  
  8. -rwxrwxr-x. 1 heli heli  20281 Dec  7 11:54 main  
  9. -rw-rw-r--. 1 heli heli    217 Dec  7 10:57 main.cpp  
  10. -rw-rw-r--. 1 heli heli    618 Dec  7 11:57 <strong>main.cpp.gcov</strong>  
  11. -rw-rw-r--. 1 heli heli    784 Dec  7 11:56 main.gcda  
  12. -rw-rw-r--. 1 heli heli   6632 Dec  7 11:54 main.gcno  
  13. -rw-rw-r--. 1 heli heli  27743 Dec  7 11:57 ostream.gcov  

生成:

[html] view plain copy print?
  1. <strong>     main.cpp.gcov</strong>  
文件,该文件记录了每行代码被执行的次数。

 

main.cpp.gcov文件内容如下:

[html] view plain copy print?
  1. -:    0:Source:main.cpp  
  2.         -:    0:Graph:main.gcno  
  3.         -:    0:Data:main.gcda  
  4.         -:    0:Runs:1  
  5.         -:    0:Programs:1  
  6.         -:    1:#include <iostream>  
  7.         -:    2:using namespace std;  
  8.         1:    3:int main()  
  9.         -:    4:{  
  10.         -:    5:    int i,total;  
  11.         1:    6:    total = 0;  
  12.         -:    7:  
  13.        11:    8:    for (i=0; i<10; i++)  
  14.        10:    9:        total += i;  
  15.         -:   10:  
  16.         1:   11:    if (total != 45)  
  17.     #####:   12:        cout << "Failure!" << endl;  
  18.         -:   13:    else  
  19.         1:   14:        cout << "Success!" << endl;  
  20.         1:   15:    return 0;  
  21.         3:   16:}  

3.2 gcov的选项

 

gcov的选项不多,也好理解,此处选3个典型的选项并结合例子加以说明。

 

(1) -a--all-blocks

 

.gcov文件中输出每个基本快(basic block)的执行次数。如果没有-a选项,则输出'main'函数这个block的执行次数,如上所示。使用该选项可以

    Write individual execution counts for every basic block.  Normally gcov outputs execution counts only for the main blocks of a line.  With this option you can determine if blocks within a single line are not being executed.

 

# gcov -a main.cpp

[html] view plain copy print?
  1. $ gcov -a main.cpp  
  2. File '/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/ios_base.h'  
  3. Lines executed:0.00% of 2  
  4. /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/ios_base.h:creating 'ios_base.h.gcov'  
  5.   
  6. File '/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/ostream'  
  7. Lines executed:0.00% of 11  
  8. /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/ostream:creating 'ostream.gcov'  
  9.   
  10. File '/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/basic_ios.h'  
  11. Lines executed:0.00% of 10  
  12. /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/basic_ios.h:creating 'basic_ios.h.gcov'  
  13.   
  14. File 'main.cpp'  
  15. Lines executed:88.89% of 9  
  16. main.cpp:creating 'main.cpp.gcov'  
  17.   
  18. File '/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/iostream'  
  19. Lines executed:100.00% of 1  
  20. /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/iostream:creating 'iostream.gcov'  
  21.   
  22. File '/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/char_traits.h'  
  23. Lines executed:0.00% of 2  
  24. /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/char_traits.h:creating 'char_traits.h.gcov'  
  25.   
  26. File '/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/locale_facets.h'  
  27. Lines executed:0.00% of 5  
  28. /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/locale_facets.h:creating 'locale_facets.h.gcov'  

main.cpp.gcov文件内容。

[html] view plain copy print?
  1. -:    0:Source:main.cpp  
  2.         -:    0:Graph:main.gcno  
  3.         -:    0:Data:main.gcda  
  4.         -:    0:Runs:1  
  5.         -:    0:Programs:1  
  6.         -:    1:#include <iostream>  
  7.         -:    2:using namespace std;  
  8.         1:    3:int main()  
  9.         -:    4:{  
  10.         -:    5:    int i,total;  
  11.         1:    6:    total = 0;  
  12.         -:    7:  
  13.        11:    8:    for (i=0; i<10; i++)  
  14.         1:    8-block  0  
  15.        10:    8-block  1  
  16.        11:    8-block  2  
  17.        10:    9:        total += i;  
  18.         -:   10:  
  19.         1:   11:    if (total != 45)  
  20.         1:   11-block  0  
  21.     #####:   12:        cout << "Failure!" << endl;  
  22.     
     
    $:   12-block  0  
  23.         -:   13:    else  
  24.         1:   14:        cout << "Success!" << endl;  
  25.         1:   14-block  0  
  26.         1:   15:    return 0;  
  27.         1:   15-block  0  
  28.         3:   16:}  
  29.         1:   16-block  0  
  30.         1:   16-block  1  
  31.         1:   16-block  2  
  32.         1:   16-block  3  

 (2) -b--branch-probabilities 

.gcov文件中输出每个分支的执行频率,并有分支统计信息。

# gcov -b main.cpp

(3) -c--branch-counts

.gcov文件中输出每个分支的执行次数。

# gcov -c main.cpp

1 0