在生产环境中,有没有一种方法可以确定哪些代码行实际运行了?

huangapple go评论106阅读模式
英文:

Is there a way to determine which lines of code actually ran in a production environment?

问题

有没有办法确定在生产环境中实际运行了哪些代码行?类似于代码覆盖率,但不是在测试中,而是在实际代码执行中。

要确定死代码

英文:

Is there a way to determine which lines of code actually ran in a production environment?
Like code coverage, but not in tests. In real code execution.

To determine dead code

答案1

得分: 1

在Go 1.20中,我们可以使用标志"-cover"。

go build -cover

然后只需运行应用程序。

GOCOVERDIR=somedata ./myapp

在该目录中,将收集统计信息。它可以转换为常规文本格式。

go tool covdata textfmt -i=somedata -o coverage.txt

之后,您可以查看结果:

go tool cover -html=coverage.txt
英文:

In go 1.20 we can use flag -cover

go build -cover

And then just run app

GOCOVERDIR=somedata ./myapp 

In the directory the stats will be collected. It can be transformed to regular text format

go tool covdata textfmt -i=somedata -o coverage.txt

After that you can look at the result:

go tool cover -html=coverage.txt

答案2

得分: 0

我不知道有这样的方法。但是,你可以在你认为是无效的部分发出指标,如果在相当长的时间内得到0个数据点,你可以相当有信心地认为代码没有被使用。

英文:

There is no such way that I know of. But, you could emit metrics in the parts that you consider dead and if you get 0 datapoints for a significant period of time, you could be pretty confident that the code isn't being used.

huangapple
  • 本文由 发表于 2023年7月29日 03:08:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76790478.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定