Go语言 为什么fasthttp.client在并发使用API时会很慢?

zdwk9cvp  于 2023-01-15  发布在  Go
关注(0)|答案(1)|浏览(201)

这是第一例。

var fastClient fasthttp.Client

fastClient = fasthttp.Client{
    ReadTimeout: 500 * time.Millisecond,
    MaxConnsPerHost: 500,
}

runtime.GOMAXPROCS(1)

for i := 0; i < 500; i++ {
    req := fasthttp.AcquireRequest()
    req.Header.SetMethod("GET")
    req.SetRequestURI(TEST_REST_API)
    res := fasthttp.AcquireResponse()
    
    start := time.Now()
    err := client.Do(req, res)
    ms := time.Since(start).Milliseconds()

    fasthttp.ReleaseRequest(req)
    fasthttp.ReleaseResponse(res)

    fmt.Printf("%v %v", ms, string(res.Header.Peek("x-real-service-time")))
}

这是第二种情况。

var fastClient fasthttp.Client

fastClient = fasthttp.Client{
    ReadTimeout: 500 * time.Millisecond,
    MaxConnsPerHost: 500,
}

runtime.GOMAXPROCS(1)

for i := 0; i < 500; i++ {
    go func(id int, c *fasthttp.Client) {
       req := c.AcquireRequest()
       req.Header.SetMethod("GET")
       req.SetRequestURI(TEST_REST_API)
       res := c.AcquireResponse()

       start := time.Now()
       err := client.Do(req, res)
       ms := time.Since(start).Milliseconds()

       c.ReleaseRequest(req)
       c.ReleaseResponse(res)

       fmt.Printf("%v %v %v", id, ms, string(res.Header.Peek("x-real-service-time")))
    }(i, &fastClient)

    // waiting for end all goroutine
}

X-实时服务时间是记录服务器处理实际请求和响应所花费的时间的报头。
在第一种情况下,MS与X-实时服务时间几乎相同,在第二种情况下,MS比X-实时服务时间慢得太多,并且随着每个ID而变得越来越慢。
你能解释一下这些差异的原因吗?我希望在第二种情况下ms和x-real-service-time之间的差异也很小。我应该如何调整它呢?
编辑:第一例结果。

id: 166 | ms: 115 | x-real-service-time : 105
id: 167 | ms: 103 | x-real-service-time : 97
id: 168 | ms: 89 | x-real-service-time : 73
id: 169 | ms: 92 | x-real-service-time : 76
id: 170 | ms: 79 | x-real-service-time : 73
id: 171 | ms: 81 | x-real-service-time : 73
id: 172 | ms: 84 | x-real-service-time : 76
id: 173 | ms: 84 | x-real-service-time : 78
id: 174 | ms: 81 | x-real-service-time : 73
id: 175 | ms: 82 | x-real-service-time : 76
id: 176 | ms: 82 | x-real-service-time : 75
id: 177 | ms: 80 | x-real-service-time : 74
id: 178 | ms: 79 | x-real-service-time : 71
id: 179 | ms: 75 | x-real-service-time : 69
id: 180 | ms: 87 | x-real-service-time : 78
id: 181 | ms: 81 | x-real-service-time : 72
...

第二种情况的结果。

id: 486 | ms: 516 | x-real-service-time : 72
id: 361 | ms: 620 | x-real-service-time : 100
id: 349 | ms: 620 | x-real-service-time : 96
id: 417 | ms: 621 | x-real-service-time : 100
id: 3 | ms: 631 | x-real-service-time : 76
id: 227 | ms: 634 | x-real-service-time : 90
id: 152 | ms: 639 | x-real-service-time : 77
id: 138 | ms: 640 | x-real-service-time : 78
id: 139 | ms: 640 | x-real-service-time : 84
id: 144 | ms: 640 | x-real-service-time : 79
...
id: 267 | ms: 783 | x-real-service-time : 75
id: 195 | ms: 779 | x-real-service-time : 73
id: 465 | ms: 781 | x-real-service-time : 76
id: 420 | ms: 781 | x-real-service-time : 74
id: 334 | ms: 782 | x-real-service-time : 112
id: 14 | ms: 780 | x-real-service-time : 69
id: 430 | ms: 781 | x-real-service-time : 75
id: 372 | ms: 782 | x-real-service-time : 78
id: 224 | ms: 778 | x-real-service-time : 107
...
id: 481 | ms: 825 | x-real-service-time : 67
id: 105 | ms: 823 | x-real-service-time : 75
id: 467 | ms: 825 | x-real-service-time : 71
id: 67 | ms: 825 | x-real-service-time : 73
id: 460 | ms: 828 | x-real-service-time : 77
id: 142 | ms: 827 | x-real-service-time : 78
id: 58 | ms: 828 | x-real-service-time : 71
id: 311 | ms: 833 | x-real-service-time : 78
id: 414 | ms: 833 | x-real-service-time : 71
id: 289 | ms: 836 | x-real-service-time : 77
...
id: 323 | ms: 1019 | x-real-service-time : 201
id: 211 | ms: 1015 | x-real-service-time : 209
id: 220 | ms: 1015 | x-real-service-time : 203
id: 275 | ms: 1021 | x-real-service-time : 216
id: 477 | ms: 1019 | x-real-service-time : 207
id: 160 | ms: 1017 | x-real-service-time : 197
id: 398 | ms: 1020 | x-real-service-time : 199
id: 253 | ms: 1017 | x-real-service-time : 201
id: 74 | ms: 1019 | x-real-service-time : 204
id: 422 | ms: 1021 | x-real-service-time : 205

id:循环编号(变量i)
ms:客户端在发出请求后收到响应所需的时间(毫秒)
x-实时服务时间:在服务器上实际处理所花费的时间(毫秒)

tf7tbtn2

tf7tbtn21#

你只使用一个CPU(runtime.GOMAXPROCS(1)),
如果你使用goroutine,cpu会等待和挂起,
client.Do花费的时间最多。

不是慢,而是开始的时间太早。
这就是为什么result for second case.id-〉时间

相关问题