How do i write a benchmark script in Go to measure ops/sec

一曲冷凌霜 提交于 2019-12-10 10:48:02

问题


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

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.


回答1:


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")
    }
}

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.

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



来源:https://stackoverflow.com/questions/30202611/how-do-i-write-a-benchmark-script-in-go-to-measure-ops-sec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!