日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

代码覆盖率测试工具:gcov和lcov的使用

發布時間:2023/12/15 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 代码覆盖率测试工具:gcov和lcov的使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

官網:

  • http://gcc.gnu.org/onlinedocs/gcc/Gcov.html

  • http://gcc.gnu.org/onlinedocs/gcc/Debugging-Options.html

?

參考:

  • https://blog.csdn.net/yanxiangyfg/article/details/80989680

  • https://www.cnblogs.com/bugutian/p/5929530.html

  • https://segmentfault.com/a/1190000011375770

?

?

gcov?is a test coverage program. Use it in concert with GCC to analyze your programs to help create more efficient, faster running code and to discover untested parts of your program. You can use?gcov?as a profiling tool to help discover where your optimization efforts will best affect your code. You can also use?gcov?along with the other profiling tool,?gprof, to assess which parts of your code use the greatest amount of computing time.

?

Profiling tools help you analyze your code’s performance. Using a profiler such as?gcov?or?gprof, you can find out some basic performance statistics, such as:

  • how often each line of code executes

  • what lines of code are actually executed

  • how much computing time each section of code uses

Once you know these things about how your code works when compiled, you can look at each module to see which modules should be optimized.?gcov?helps you determine where to work on optimization.

?

?

gcov是什么:

  • gcov是一個測試代碼覆蓋率的工具。與GCC一起使用來分析程序,以幫助創建更高效、更快的運行代碼,并發現程序的未測試部分。

  • 是一個命令行方式的控制臺程序。需要結合lcov,gcovr等前端圖形工具才能實現統計數據圖形化。

  • 伴隨GCC發布,不需要單獨下載gcov工具。配合GCC共同實現對c/c++文件的語句覆蓋和分支覆蓋測試。

  • 與程序概要分析工具(profiling tool,例如gprof)一起工作,可以估計程序中哪段代碼最耗時。

?

gcov能做什么:

使用象gcov或gprof這樣的分析器,您可以找到一些基本的性能統計數據:

  • 每一行代碼執行的頻率是多少。

  • 實際執行了哪些行代碼,配合測試用例達到滿意的覆蓋率和預期工作。

  • 每段代碼使用了多少計算時間,從而找到熱點優化代碼。

  • gcov創建一個sourcefile.gcov的日志文件,此文件標識源文件sourcefile.c每一行執行的次數,您可以與gprof一起使用這些日志文件來幫助優化程序的性能。gprof提供了您可以使用的時間信息以及從gcov獲得的信息。

?

?

1.?準備代碼test.c

#include <stdio.h>int main(int argc, char** argv) {int i, total;total = 0;for(i = 0; i < 10; i++) {total += i;}if(total != 45) {printf("fail!\n");} else {printf("success!\n");}return 0; }

?

2.?編譯

執行:gcc -fprofile-arcs -ftest-coverage test.c -o test

生成:test.gcno和test,

baoli@ubuntu:~/tools/gcov$ ls

test??test.c??test.gcno

?

-fprofile-arcs -ftest-coverage告訴編譯器生成gcov需要的額外信息,并在目標文件中插入gcov需要的extra profiling information。因此,該命令在生成可執行文件test的同時生成test.gcno文件(gcov note文件)。

?

?

3.?執行test收集信息,產生test.gcda

baoli@ubuntu:~/tools/gcov$ ls

test??test.c??test.gcda??test.gcno

?

?

4.?執行gcov test.c,生成報告test.c.gcov

baoli@ubuntu:~/tools/gcov$ gcov test.c

File 'test.c'

Lines executed:87.50% of 8

Creating 'test.c.gcov'

?

baoli@ubuntu:~/tools/gcov$ ls

test??test.c??test.c.gcov??test.gcda??test.gcno

?

查看test.c.gcov報告信息:

baoli@ubuntu:~/tools/gcov$ cat test.c.gcov-: 0:Source:test.c-: 0:Graph:test.gcno-: 0:Data:test.gcda-: 0:Runs:1-: 0:Programs:1-: 1:#include <stdio.h>-: 2:-: 3:1: 4:int main(int argc, char** argv)-: 5:{-: 6: int i, total;-: 7:1: 8: total = 0;-: 9:11: 10: for(i = 0; i < 10; i++) {10: 11: total += i;-: 12: }-: 13:1: 14: if(total != 45) {#####: 15: printf("fail!\n");-: 16: } else {1: 17: printf("success!\n");-: 18: }-: 19:1: 20: return 0;-: 21:}

其中#####表示未運行的行

每行前面的數字表示行運行的次數

?

5.?lcov

上述生成的.c.gcov文件可視化成都較低,需要借助lcov,genhtml工具直接生成html報告。

?

5.1?安裝lcov

sudo apt-get install lcov

?

5.2?運行lcov,生成相應信息

執行:lcov -d . -o test.info -b . -c

baoli@ubuntu:~/tools/gcov$ lcov -d . -o test.info -b . -c Capturing coverage data from . Found gcov version: 5.4.0 Scanning . for .gcda files ... Found 1 data files in . Processing test.gcda Finished .info-file creationbaoli@ubuntu:~/tools/gcov$ ls test test.c test.gcda test.gcno test.info

?

5.3?生成web可視化信息

執行:genhtml -o result test.info

baoli@ubuntu:~/tools/gcov$ genhtml -o result test.info Reading data file test.info Found 1 entries. Found common filename prefix "/home/baoli/tools" Writing .css and .png files. Generating output. Processing file gcov/test.c Writing directory view page. Overall coverage rate:lines......: 87.5% (7 of 8 lines)functions..: 100.0% (1 of 1 function)baoli@ubuntu:~/tools/gcov$ ls result test test.c test.gcda test.gcno test.info

?

?

5.4?查看web

1)打開index.html

2)詳細信息:

?

上述界面包含:

  • 函數覆蓋率(執行率)

  • 代碼行數覆蓋率(執行率)

  • 語句執行次數

  • 源碼級的詳細信息

?

?

?

?

?

?

?

?

總結

以上是生活随笔為你收集整理的代码覆盖率测试工具:gcov和lcov的使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。