Some strange bug in Go over time

Hi all. I needed to somehow calculate the server performance. And we set the task to calculate the minimum, maximum, and average time for processing a request.

In short, I did and did and could not understand why there were microseconds at idle, and some strange numbers on the test.

I started researching the reason. And I found her. And now I’m asking myself: either the skis don’t work or there’s a go bug?

count := 100000
wg := sync.WaitGroup{}
wg.Add(count)

dur2 := atomic.Int64{}

at := time.Now()

for i := 0; i < count; i++ {
    go func() {
        defer wg.Done()

        at2 := time.Now()
        for k := 0; k < 1000000; k++ {
        }

        dur2.Add(int64(time.Since(at2)))
    }()
}

wg.Wait()

durOne := time.Since(at) / time.Duration(count)
durGo := time.Duration(dur2.Load() / int64(count))

log.Println("Duration parallel: one ", durOne.String(), " goroutine ", durGo.String())

result:

2024/10/30 20:34:35 Duration parallel: one 35.319µs goroutine 1.096925ms

go version go1.23.1 windows/amd64
goos: windows
goarch: amd64
cpu: 12th Gen Intel(R) Core(TM) i7-1255U

The bug appears on both Windows and Linux

Write in the comments if you have encountered such a bug and what you think about it

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *