当前位置:网站首页>[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 ~
边栏推荐
- 基于机器学习技术的无线小区负载均衡自优化
- C language operator priority
- 【文献阅读】TENET: A Framework for Modeling Tensor Dataflow Based on Relation-centric Notation
- The interviewer asked: how to check if redis suddenly slows down?
- CheckPoint and DataNode
- NFT differentiation trend has shown, how to capture value?
- 14:07:08 ckati failed with: signal: killed
- ACE下载地址
- Xcode11添加引导页(升级后Launch Images Source选项不见了)
- Using XOR to exchange two variables is inefficient
猜你喜欢

袋鼠云数栈基于CBO在Spark SQL优化上的探索

基于开源流批一体数据同步引擎ChunJun数据还原—DDL解析模块的实战分享

Why is opensea the absolute monopolist of NFT trading market?

Namenode and secondarynamenode

MapReduce环境准备

争夺存量用户关键战,助力企业构建完美标签体系丨01期直播回顾

CheckPoint and DataNode

Solve the problem that Scala cannot initialize the class of native

Xcode11添加引导页(升级后Launch Images Source选项不见了)

VSCode中安装Go:tools failed to install.
随机推荐
使用异或交换两个变量是低效的
Punch in 10 interview questions every day - JVM article
Redis 突然变慢了?
走好数据中台最后一公里,为什么说数据服务API是数据中台的标配?
0章 性能平台GodEye源码分析-课程介绍
【文献阅读】Small-Footprint Keyword Spotting with Multi-Scale Temporal Convolution
phthon3 安装 MySQLdb 报错问题解决 Reason: image not found
Detailed explanation of errno
wkwebview白屏
Database programming (MySQL) of node, adding, deleting, modifying and querying
VSCode中安装Go:tools failed to install.
The platform of digital collection NFT is good
The use of libtomcrypt password Library
Red sun range 2
CheckPoint and DataNode
Today's code farmer girl did exercises on breathing lights, controlled components and high-level components
MapReduce环境准备
nodejs-uuid
5G专网在智慧医疗中的应用
C language operator priority