如何在Go中编写基准测试脚本以衡量运营/秒 [英] How do i write a benchmark script in Go to measure ops/sec

查看:61
本文介绍了如何在Go中编写基准测试脚本以衡量运营/秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过编写一个简单的Redis克隆来练习Golang.

I am practicing my Golang by writing a simple Redis clone.

我该如何编写一个基准脚本,以C并发级别每秒建立X个连接,以处理服务器协议并测量每秒操作数?

How do i write a benchmark script that would establish X connections per second at C concurrency level to deal with my server's protocol and measure how many ops/sec?

我可以简单地编写一个可以实际执行此操作的脚本:

I can simply write a script that would actually do this:

for i := range(1000) {
    // Open connection
    // Perform command
    // Close connection
}

但是我想知道每秒分发每个并发级别的连接数的概念.

But i want to know the concept behind distributing the number of connections per concurrency level per second.

推荐答案

最好由内置的 testing.Benchmark 系统.例如,像这样的测试用例:

This is best handled by the built-in testing.Benchmark system. For example, a test case like:

func BenchmarkHello(b *testing.B) {
    for i := 0; i < b.N; i++ {
        fmt.Sprintf("hello")
    }
}

将自动计算此功能的时序计算.有关设置的更多详细信息,请参见软件包文档,但是如果您之前编写过Go测试用例,应该会非常熟悉.

will automatically work out timing calculations for this function. See the package documentation for more details on setting it up, but it should be very familiar if you've written Go test cases before.

对于有关并发基准测试的问题,请参见RunParallel帮助器,该帮助器专门用于此目的.

To your question about concurrency benchmarking, see the RunParallel helper, which is specifically to assist in this.

这篇关于如何在Go中编写基准测试脚本以衡量运营/秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆