英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论