那么问题来了,我们怎么知道这个test case有没有覆盖到所有的代码呢?
C代码覆盖率测试,需要用到gcc的配套工具gcov , 还有一个可视化公建lcov。
首先需要打开Postgresql的一个编译选项。
--enable-coverage build with coverage testing instrumentation
这个编译项对应gcc的两个参数-fprofile-arcs -ftest-coverage
# enable code coverage if --enable-coverageif test "$enable_coverage" = yes; thenif test "$GCC" = yes; thenCFLAGS="$CFLAGS -fprofile-arcs -ftest-coverage"elseas_fn_error $? "--enable-coverage is supported only when using GCC" "$LINENO" 5fifi
这两个参数的含义如下:
man gcc
Add code so that program flow arcs are instrumented. During execution the program records how many times each branch and call is executed how many times it taken or returnsWhen the compiled
program exits it saves this data to a file called auxnamegcda for each source fileThe data may be used profiledirected optimizations (-fbranchprobabilities), test coverage analysis
ftestcoverage).Eachobject file’s auxname generated from the name of the output file,136)">if explicitly specified it not the final executable otherwise it the basename of the source file.
In both cases any suffix removed (eg foo input file dir/foo dir output file specified aso diro).
coverage
This option used to compile link code instrumented coverage analysis a synonym coverage (when compiling)lgcov linkingSee the
documentation those options more details.
· Compile the source files arcs plus optimization code generation optionsFor test coverage analysisuse the additional coverage optionYoudo need to profile every
source file in a programLink your files lgcov the latter implies the formerRun the program on a representative workload to generate the arc profile information may be repeated any number of times can run concurrent instances of your program provided that
the file system supports locking the data files will be correctly updatedAlso "fork" calls are detected correctly handled double counting will happendirected optimizations compile the source files again the same optimization code generation options plus gcov to produce human readable information the gcno gcda filesRefer to the gcov documentation further informationWitharcs each function of your program GCC creates a program flow graphthen finds a spanning tree the graphOnly arcs that are on the spanning tree have to be instrumented:
the compiler adds code to count the number of times that these arcs are executed an arc the only exit only entrance to a block the instrumentation code can be added to the block;
otherwise a new basic block must be created to hold the instrumentation code.
Produce a notes file that the gcov codecoverage utility can to show program coverage source files note file called auxname to the arcs option above a
description of auxname instructions on how to generate test coverage dataCoverage data will match the source files more closely you optimize.
首先需要安装依赖gcov和lcov
gcov在gcc包中已经包含了,locv是ltp的一个gcov扩展插件,用来产生HTML报告.
lcov用法参考:
# yum install -y http://downloads.sourceforge.net/ltp/lcov-1.11-1.noarch.rpm
编译Postgresql
./configure --prefix=/optpgsql9.4.4--withpgport=1921perl python tcl openssl pam ldap libxml libxslt threadsafety debug dtrace gmake world && gmake installworld
postgres@digoal-> ll
total 1.3M
rwr 1 postgres postgres 22KJun1003:29 gistbuildbuffersc
-------1.6KSep71442gcda
15K38gcno
70Ko
37K gistbuild2.2K20K92K43K gist3.1K29K16K gistget1.3K13K74K101K39K gistproc31K79K9.1K gistscan8486.7K60K24K gistsplit1.5K68K21K gistutil84K7.1K gistvacuum7847.3K56K14K gistxlog1.2K12K50K538Makefile
357 objfilestxt
README
postgres@digoal pwd
soft_bakpostgresql-9.44srcbackendaccessgist