Wrong memory benchmarking results in Golang

80 views Asked by At

I am using standard Golang benchmarking for my project.

Here are my PC and Golang Specs:

goos: windows
goarch: amd64
cpu: Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz
go version:  go1.21.5
Available Memory: 64GB

After running the benchmark as follows:

go test -bench=MyCodeBench -benchtime=1x -timeout=120m -benchmem -run=^$

Note: I have to set the timeout to 120 minutes because the code takes so long to execute, and if I do not set the timeout flag, I will get the *** Test killed with quit: ran too long error.

Here is one of the benchmark results that I got:

MyCodeBench    1    776903113600 ns/op      402860098688 B/op       90982299 allocs/op

By converting memory allocation from B/op to GB/op, I will end up having 402GB memory allocation!! I was wondering how it could be possible. I don't have that amount of memory on my PC.

PS: I thought maybe the memory allocation is incremental and it reports the whole memory allocation during the benchmark, which makes no sense, I need to know the current memory allocation for my code. Also, while running the code, I checked the memory usage using Windows task manager, it shows that only 23 GB of memory has been used.

Q1. What can I do to get the correct benchmark results?

Q2. In addition, how can I get the memory throughput (MB/s)?

Regarding Q2, as it mentioned here:

// SetBytes records the number of bytes processed in a single operation.
// If this is called, the benchmark will report ns/op and MB/s.
func (b *B) SetBytes(n int64) { b.bytes = n }

by calling the function SetBytes and setting the bytes' number there should be MB/s in the results! however, I couldn't make that work.

0

There are 0 answers