Closed Cloverk1t closed 11 months ago
@cloverkits, I need clarification on your goals and objectives. I will try to provide facts. Typically, goroutine gets used where concurrent/parallelism is required to optimize overall processing time and efficiency. I think the above goroutine example wouldn't provide valuable information on time measurement. Have you tried the Go default HTTP client with your goroutine example?
@jeevatkm yep! Default HTTP client maybe have something wrong in goroutine. In your words "concurrent/parallelism is required to optimize overall processing time" which is my case. I need reads multiple objects in parallel from one server multi interfaces.
example code
package main
import (
"io"
"log"
"net/http"
"sync"
"time"
)
func main() {
wg := sync.WaitGroup{}
for i := 0; i < 50; i++ {
wg.Add(1)
go func() {
defer wg.Done()
startAt := time.Now()
resp, err := http.Get("https://www.baidu.com")
if err != nil {
log.Println(err)
}
defer resp.Body.Close()
log.Println("latency:", time.Since(startAt))
_, err = io.ReadAll(resp.Body)
if err != nil {
log.Println(err)
}
}()
}
wg.Wait()
}
result
2023/09/25 10:12:23 latency: 69.907033ms
2023/09/25 10:12:23 latency: 69.614386ms
2023/09/25 10:12:23 latency: 70.939928ms
2023/09/25 10:12:23 latency: 70.62763ms
2023/09/25 10:12:23 latency: 71.418996ms
2023/09/25 10:12:23 latency: 72.425473ms
2023/09/25 10:12:23 latency: 78.597691ms
2023/09/25 10:12:23 latency: 81.947911ms
2023/09/25 10:12:23 latency: 82.902086ms
2023/09/25 10:12:23 latency: 84.898575ms
2023/09/25 10:12:23 latency: 84.801553ms
2023/09/25 10:12:23 latency: 84.496353ms
2023/09/25 10:12:23 latency: 85.732495ms
2023/09/25 10:12:23 latency: 85.642949ms
2023/09/25 10:12:23 latency: 85.952062ms
2023/09/25 10:12:23 latency: 86.141165ms
2023/09/25 10:12:23 latency: 87.003269ms
2023/09/25 10:12:23 latency: 87.436695ms
2023/09/25 10:12:23 latency: 91.543479ms
2023/09/25 10:12:23 latency: 91.84506ms
2023/09/25 10:12:23 latency: 92.977854ms
2023/09/25 10:12:23 latency: 93.429503ms
2023/09/25 10:12:23 latency: 94.71919ms
2023/09/25 10:12:23 latency: 95.130987ms
2023/09/25 10:12:23 latency: 97.990181ms
2023/09/25 10:12:23 latency: 98.086186ms
2023/09/25 10:12:23 latency: 98.242818ms
2023/09/25 10:12:23 latency: 98.315098ms
2023/09/25 10:12:23 latency: 98.920861ms
2023/09/25 10:12:23 latency: 99.129425ms
2023/09/25 10:12:23 latency: 100.425497ms
2023/09/25 10:12:23 latency: 101.000571ms
2023/09/25 10:12:23 latency: 101.15033ms
2023/09/25 10:12:23 latency: 100.735866ms
2023/09/25 10:12:23 latency: 101.528584ms
2023/09/25 10:12:23 latency: 101.650535ms
2023/09/25 10:12:23 latency: 101.478554ms
2023/09/25 10:12:23 latency: 104.253945ms
2023/09/25 10:12:23 latency: 104.981183ms
2023/09/25 10:12:23 latency: 105.334097ms
2023/09/25 10:12:23 latency: 105.663269ms
2023/09/25 10:12:23 latency: 106.390947ms
2023/09/25 10:12:23 latency: 106.30004ms
2023/09/25 10:12:23 latency: 107.450104ms
2023/09/25 10:12:23 latency: 110.235126ms
2023/09/25 10:12:23 latency: 110.161177ms
2023/09/25 10:12:23 latency: 110.386901ms
2023/09/25 10:12:23 latency: 110.360874ms
2023/09/25 10:12:23 latency: 110.623886ms
2023/09/25 10:12:23 latency: 111.314326ms
I used resty to create an http client through which to test a website. Through 50 cycles, observe the change of the delay after the client obtains data. Request latency values are normal when goroutine is not used. However, if you use goroutine, you will find that the request latency value increases.
goroutine is not used
test code
result
goroutine is used
result