What should I do with benchmark in Go

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

What should I do with benchmark in Go

问题

我理解什么是基准测试,如何编写和执行Go中的基准测试。
但我真的不太明白我应该如何使用它。

就像测试一样,我应该创建测试用例来测试我创建的每个函数,以便确认函数是否按照我预期的方式工作。如果我在其他地方有进一步的更改,我可以运行单元测试来确认一切是否正常工作。

那基准测试呢?例如,有许多排序算法,如冒泡排序和归并排序。但在应用程序代码库中只会有一个带有签名sort(arr []int)的实现。那么我应该为这些算法创建基准测试函数,然后在找到最佳适配时复制并粘贴实现吗?

那么,在Go中我应该如何使用基准测试?欢迎提供从日常使用到性能优化的任何想法。

英文:

I understand what benchmark is, how to write, and how to execute benchmark in Go.
But I'm not really understanding what should I do with it.

Like test, I should create test case to test every function I created, so that I can confirm if the function works as I expected. And if I have further change in other place, I can run unit tests to confirm if everything works well.

What about benchmark? E.g. there are many sort algorithm, like bubble sort and merge sort. But there will only be 1 implementation in the app code base with signature sort(arr []int). So I should create benchmarks functions for those algorithms then copy & paste the implementation whenever I found the best fit?

So, what should I do with benchmark in Go? Welcome every idea from daily to performance optimization.

答案1

得分: 1

基准测试也是一种测试。难怪它们被放在测试包中。正如你所说,一般的单元测试(testing.T)可以检查某个重构是否破坏了函数的逻辑。但在高负载系统中,除了正确工作的逻辑之外,还有另一个标准,那就是处理某些逻辑所需的时间。基准测试可以让你检查你所做的重构是否使函数的运行速度变慢。

英文:

Benchmarks are tests too. No wonder they placed in testing package. As you correctly said general unit tests (testing.T) allow to check if some refactoring hasn't break logic of function. But in highload systems there is another criteria besides to correctly working logic. It is time of processing of some logic. Benchmarks allow you to check that refactoring that you made
had not made your function work slower.

huangapple
  • 本文由 发表于 2022年11月11日 12:35:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/74398059.html
匿名

发表评论

匿名网友

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

确定