”工欲善其事,必先利其器。“—孔子《论语.录灵公》
首页 > 编程 > 使用 Golang 进行基准测试

使用 Golang 进行基准测试

发布于2024-11-04
浏览:303

地鼠你好?

在这篇博文中,我将向您展示如何使用 #golang 测试包中内置的出色工具。您将如何测试一段代码或函数的性能?使用基准测试。

我们走吧。

对于此测试,我将使用经典的斐波那契数列或斐波那契数列,其由以下因素确定:

if (x 



这个序列很重要,因为它也出现在数学和自然的几个部分中,如下所示:

Benchmark with Golang

有多种方法可以实现此代码,我将选择两种方法进行基准测试:计算它的递归方法和迭代方法。这些函数的主要目标是提供 位置 并返回该位置的斐波那契数。

递归法

// main.go

func fibRecursive(n int) int {
    if n 



迭代法

// main.go

func fibIterative(position uint) uint {
    slc := make([]uint, position)
    slc[0] = 1
    slc[1] = 1

    if position 



这些方法并未经过优化,但即使对于少数情况,测试结果也有显着差异。您将在测试中看到这一点。要跟随代码进行操作,您可以单击此处。

现在,对于基准测试,让我们在 _main_test.go 文件中编写一些测试。使用Golang关于benchmark的文档,您可以创建要测试的函数,如下所示:

// main_test.go

// The key is to start every function you want to benchmark with the keyword Benchmark and use b *testing.B instead of t *testing.T as input 
func BenchmarkFibIterative(b *testing.B) {
    // Use this for-loop to ensure the code will behave correctly. 
    // Now, you can put the function or piece of code you want to test
    for i := 0; i 



继续之前问一个问题:哪个更快?

让我们对一个较小的数字 (10) 和一个稍大的数字 (80) 运行测试。要运行基准测试,您只需运行命令:

go test -bench=函数名称

如果您想了解更多有关此命令的信息,请查看此处。

第一次测试:position=10

//(fibIterative)
Results:
cpu: Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
BenchmarkFibIterative-8         24491042                42.50 ns/op
PASS
ok      playground      1.651s

我们借助这张图来分析一下:

Benchmark with Golang

根据图像,我们有8个核心用于测试,没有时间限制(它将运行到完成)。花了1.651s完成任务。

==== Extra ====
We got 24,491,042 iterations (computations), and each iteration (op) took 42.50 ns.

Doing some math, we can calculate how much time one op took:

42.50 ns/op with 1 ns = 1/1,000,000,000 s
op ≈ 2.35270590588e-12 s
==== Extra ====

这是一个很好的结果。让我们检查一下位置 10 的递归函数:

// Results
BenchmarkFibRecursive-8          6035011               187.8 ns/op
PASS
ok      playground      1.882s

我们可以看到,需要1.882s完成任务。

迭代函数以几十分之一的优势获胜。让我们再尝试一项测试:

位置 50

// Results for the Iterative Function

cpu: Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
BenchmarkFibIterative-8         27896118                45.37 ns/op
PASS
ok      playground      2.876s

// Results for the Recursive Function

cpu: Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
BenchmarkFibRecursive-8          6365198               186.3 ns/op
PASS
ok      playground      1.918s

哇!现在递归函数更快了?

让我们以稍微大一点的数字结束。

位置80

// Results for the Iterative Function

cpu: Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
BenchmarkFibIterative-8          5102344               229.5 ns/op
PASS
ok      playground      1.933s

// Results for the Recursive Function
// My poor PC couldn’t handle it, so I had to reduce the position to 50 just to get some results printed.

cpu: Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
BenchmarkFibRecursive-8                1        44319299474 ns/op
PASS
ok      playground      44.951s

差别很大。对于位置 80,迭代方法大约需要 2 秒。对于位置 50,递归函数花费了大约 45 秒。这证明了当您的 Golang 项目开始变慢时对代码进行基准测试的重要性。

结论

如果您的生产代码运行缓慢或不可预测地变慢,您可以使用此技术,结合 pprof 或内置测试包中的其他工具,来识别和测试代码的执行位置不好以及如何优化它。

旁注:并非所有美观的代码都具有更高的性能。

额外运动

你能找到更好的方法来改进递归函数吗? (提示:使用动态规划)。本文解释了为什么对于一些小数字,递归策略更好。

版本声明 本文转载于:https://dev.to/pedrobertao/benchmark-with-golang-md4?1如有侵犯,请联系[email protected]删除
最新教程 更多>

免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。

Copyright© 2022 湘ICP备2022001581号-3