Coverage for deamon

I’m trying to make coverage for a daemon. when building I’m using -cover .
is there any way to dump ( like gcov_flush in GCC) or terminate gracefully in order to get coverage?

Coverage profiling support for integration tests - The Go Programming Language

Sorry, the link I provided seems not helping, maybe GoLang not support this feature currently. We can open a proposal to discuss this issue with GoLang community if you want.

Yeah, that can be good option to discuss with the community.
also I found GitHub - qiniu/goc: A Comprehensive Coverage Testing System for The Go Programming Language
didn’t mange to get it to work yet, but do you think it might?

1 Like

According to the document [1], I think that maybe the tool that you need.

[1] Goc can collect code coverages at runtime for your long-run golang applications.

I just tried the goc tool, it seems works fine. If you got stuck, feel free to talk to me.

I also managed to get it to work. Thanks.

But it’s causing me new issue, because It forces me to build with goc. and I cant put the goc on my compilation machine. is there a way to modify the output binaries the use the goc? or some other ideas?

To the best of my knowledge, it’s advisable to have goc installed on the compilation machine.

The mechanism behind goc operates as follows: the compiler (goc) inserts coverage-related statements into your code before feeding them into the compiler.

  1. It copies the project into /tmp/goc-build-${hash}/. This folder will be deleted after compilation, but you can use goc build --debug to retain it for further investigation.
  2. It parses the source code into an AST (abstract syntax tree).
  3. Coverage counting statements are added at the beginning of every Basic Block.
  4. The source code of the dedicated web server (http_cover_apis_auto_generated.go) is copied and imported into your project, responsible for collecting coverage data.
  5. Finally, it compiles the modified project.

This process is similar to how coverage data is collected when using go test -cover. You can read more about it in this blog post.

You can observe the modified source code under /tmp/goc-build-${hash}. The modified source code will resemble the following (notice the counter variable, e.g., GoCover_0_373131613736336566316238.Count):

func main() { GoCover_0_373131613736336566316238.Count[2]++;
    total := 256
    scheduler := gojob.New().Start()
    for i := 0; i < total; i++ { GoCover_0_373131613736336566316238.Count[4]++;
        scheduler.Submit(New(i, rand.Intn(10)))
    GoCover_0_373131613736336566316238.Count[3]++; scheduler.Wait()

Regarding your question, is there a way to modify the output binaries to use goc?

The answer is yes, but it would be tricky and ungraceful, especially if you need to send the modified project to the compilation machine.