当前位置:网站首页>[go language] code coverage test (Gcov)
[go language] code coverage test (Gcov)
2022-07-19 01:51:00 【tissar】
【Go Language 】 Code coverage testing (gcov)
GCOV
gcov yes gcc Code coverage testing tools provided , Its test results reflect 2 Indicators .
- How often each line of code is executed
- Actually executed a few lines of code
For code with high execution frequency , Need to focus on Optimization ; Code that has not been executed , It is necessary to check whether BUG, Or useless code .
Example project
Altogether 3 Code files :
- calc/main.go The main program
- simplemath/add.go Provide Add() function
- simplemath/sqrt.go Provide Sqrt() function
[email protected]:~/Workplace/golang/helloprj$ tree
.
├── bin
├── pkg
│ ├── gccgo_linux_amd64
│ └── linux_amd64
└── src
├── calc
│ └── main.go
└── simplemath
├── add.go
└── sqrt.go
7 directories, 4 files
[email protected]:~/Workplace/golang/helloprj$
simplemath/add.go
package simplemath
func Add(a int64, b int64) int64 {
return a + b
}
simplemath/sqrt.go
package simplemath
import "math"
func Sqrt(i int) int {
v := math.Sqrt(float64(i))
return int(v)
}
calc/main.go
package main
import "fmt"
import "simplemath"
// use for Cycle the Fibonacci number
func fibonacci(n int64) (int64) {
var res int64 = 0
var tmp int64 = 1
var idx int64 = 0
for ; idx < n; idx++ {
res = simplemath.Add(res, tmp)
res, tmp = tmp, res
}
return res
}
// Calculate Fibonacci number with recursive method
func fibonacci_r(n int64) (int64) {
if n < 1 {
return 0
} else if n < 3 {
return 1
}
lhs := fibonacci_r(n - 1)
rhs := fibonacci_r(n - 2)
ret := simplemath.Add(lhs, rhs)
return ret
}
// Calculate Fibonacci number , And print it out
func main() {
num := fibonacci(30)
fmt.Println("Result:", num)
res := fibonacci_r(30)
fmt.Println("Result:", res)
}
Compile code ( Generate gcno file )
Makefile
all: simplemath calc
simplemath:
gccgo -c -o simplemath.o $(FLAGS) src/simplemath/add.go src/simplemath/sqrt.go
ar r pkg/gccgo_linux_amd64/libsimplemath.a simplemath.o
rm simplemath.o
calc:
gccgo -o bin/calc $(FLAGS) -L pkg/gccgo_linux_amd64 src/calc/main.go -lsimplemath
clean:
rm pkg/gccgo_linux_amd64/libsimplemath.a
rm bin/calc
Use "–coverage" Compile parameters
[[email protected] goprj]$ FLAGS="--coverage" make
gccgo -c -o simplemath.o --coverage src/simplemath/add.go src/simplemath/sqrt.go
ar r pkg/gccgo_linux_amd64/libsimplemath.a simplemath.o
ar: creating pkg/gccgo_linux_amd64/libsimplemath.a
rm simplemath.o
gccgo -o bin/calc --coverage -L pkg/gccgo_linux_amd64 src/calc/main.go -lsimplemath
After compiling , Generate... In the current directory gcno file
[[email protected] goprj]$ ls -lh *.gcno
-rw-rw-r-- 1 root root 5.7K Jan 1 00:00 main.gcno
-rw-rw-r-- 1 root root 792 Jan 1 00:00 simplemath.gcno
Run the program ( Generate gcda file )
After operation , Generate... In the current directory gcda file
[[email protected] goprj]$ bin/calc
Result: 832040
Result: 832040
[[email protected] goprj]$ ls -lh *.gcda
-rw-rw-r-- 1 root root 640 Jan 1 00:00 main.gcda
-rw-rw-r-- 1 root root 120 Jan 1 00:00 simplemath.gcda
gcov( Generate gcov file )
[[email protected] goprj]$ gcov *.gcda
main.gcno:version 'B02*', prefer 'A93*'
main.gcda:version 'B02*', prefer version 'A93*'
simplemath.gcno:version 'B02*', prefer 'A93*'
simplemath.gcda:version 'B02*', prefer version 'A93*'
File 'src/calc/main.go'
Lines executed:95.65% of 23
Creating 'main.go.gcov'
File 'src/simplemath/sqrt.go'
Lines executed:0.00% of 3
Creating 'sqrt.go.gcov'
File 'src/simplemath/add.go'
Lines executed:100.00% of 2
Creating 'add.go.gcov'
[[email protected] goprj]$ ls -lh *.gcov
-rw-rw-r-- 1 root root 340 Jan 1 00:00 add.go.gcov
-rw-rw-r-- 1 root root 1.8K Jan 1 00:00 main.go.gcov
-rw-rw-r-- 1 root root 346 Jan 1 00:00 sqrt.go.gcov
lcov( Generate info file )
[[email protected] goprj]$ lcov -c -d ./ -o calc.coverage
Finished .info-file creation
[[email protected] goprj]$ ls -lh calc.coverage
-rw-rw-r-- 1 root root 1.4K Jan 1 00:00 calc.coverage
genhtml( Visualize into html)
[[email protected] goprj]$ genhtml -o calc.coverage.html calc.coverage
Reading data file calc.coverage
Found 3 entries.
Writing .css and .png files.
Generating output.
Processing file calc/main.go
Processing file simplemath/sqrt.go
Processing file simplemath/add.go
Writing directory view page.
Overall coverage rate:
lines......: 85.7% (24 of 28 lines)
functions..: 50.0% (5 of 10 functions)
Result analysis
main.go.gcov
add.go.gcov
sqrt.go.gcov
Recursive algorithm is really not suitable for Fibonacci numbers ~
边栏推荐
- 未成年人数字安全保护的问题与对策
- 开源项目丨 Taier 1.1 版本正式发布,新增功能一览为快
- NameNode 和 SecondaryNameNode
- 【文献阅读】Small-Footprint Keyword Spotting with Multi-Scale Temporal Convolution
- 2章 性能平台GodEye源码分析-数据模块
- Show Me the Code之MXNet网络模型(三)
- rotoc-gen-go: unable to determine Go import path for **.proto
- Dhfs read / write process
- 通感一体去蜂窝超大规模MIMO与高频段无线接入技术
- Database programming (MySQL) of node, adding, deleting, modifying and querying
猜你喜欢

ChunJun支持异构数据源DDL转换与自动执行 丨DTMO 02期回顾(内含课程回放+课件)

Fair Attribute Classification through Latent Space De-biasing

What are the NFT digital collection platforms? Which platforms are worth collecting?

手把手带你从零开始完整开发经典游戏【俄罗斯方块】,全部逻辑只用不到200行代码。

【MySQL】windows安装MySQL 5.7

Classification and use of express Middleware

Scala environment construction

mysql innodb 事务相关记录

Countless times of stepping on the pit to install awvs

【文献阅读】Counting Integer Points in Parametric Polytopes Using Barvinok‘s Rational Functions
随机推荐
golang编译的常见错误
JVM 判断对象已死,实践验证GC回收
remote sensing 投稿流程
Boost线程池
【翻译】Transformers in Computer Vision
Cocos Creator 3.0 基础——常见操作
The use of libtomcrypt password Library
Punch in 10 interview questions every day - JVM article
【文献阅读】VAQF: Fully Automatic Software-Hardware Co-Design Framework for Low-Bit Vision Transformer
Solve the problem that Scala cannot initialize the class of native
One vs One Mitigation of Intersectional Bias
4章 性能平台GodEye源码分析-监控模块
cookie、LocalStorage、sessionStorage三者区别以及使用方式
通感一体化融合的研究及其挑战
The interviewer asked: how to check if redis suddenly slows down?
记录一次海外图片加载不出来的排查
网络安全新架构:零信任安全
Byte two side: what is pseudo sharing? How to avoid it?
MapReduce
MXNet网络模型(四)GAN神经网络