当前位置:网站首页>[go language] code coverage test (Gcov)

[go language] code coverage test (Gcov)

2022-07-19 01:51:00 tissar

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

 Insert picture description here

add.go.gcov

 Insert picture description here

sqrt.go.gcov

 Insert picture description here
Recursive algorithm is really not suitable for Fibonacci numbers ~

原网站

版权声明
本文为[tissar]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/200/202207170005512296.html