youbbs
youbbs
2138 1 0

fasthttp.ServeFile 在某些情景下速度很慢

这是在 fasthttp 导论组上看到的一个问题,追源发现 go net 库存在的一个小问题。
@mehdipourfar 在使用 fasthttp.ServeFile 做静态文件服务

fasthttp.ServeFile(ctx, image_path)

测试发现使用 fasthttp.ServeFile 时有时候响应很慢:

> go-wrk -d 3 "http://localhost:8080/2"

Running 3s test @ http://localhost:8080/2
  10 goroutine(s) running concurrently
248 requests in 3.029993142s, 11.92MB read
Requests/sec:       81.85
Transfer/sec:       3.93MB
Avg Req Time:       122.177142ms
Fastest Request:    118.257µs
Slowest Request:    216.498781ms <----- 这里
Number of Errors:   0

但用标准库 net/http 没有那么慢

> go-wrk -d 3 "http://localhost:8080/1"

Running 3s test @ http://localhost:8080/1
  10 goroutine(s) running concurrently
31200 requests in 2.923608791s, 1.46GB read
Requests/sec:       10671.74
Transfer/sec:       512.34MB
Avg Req Time:       937.054µs
Fastest Request:    134.841µs
Slowest Request:    7.879186ms  <----- 这里
Number of Errors:   0

fasthttp 的维护者 @erikdubbelboer 究其原因发现问题出在 io.Copy 函数:

The first io.Copy call is fast at around 500µs. All following calls to io.Copy are slow at around 43ms.

并给出测试结果:

$ go run bench.go
501.52µs       <-- fast
211.711399ms   <-- always super slow for some reason
48.073968ms    <-- still slow
43.86743ms
43.951452ms
47.955709ms
43.997812ms
43.916976ms
44.037359ms
48.032407ms

提交 issue 给 go 团队后,疫情期间 go 团队也很给力,很快修复了这个问题。

如果你的应用中使用 sendfile 服务的,多留意下次 go 更新。

参考

0

See Also

Nearby


Discussion (1)

ego008
ego008 2021-04-02 08:28

但 golang 开发团队还在观察,等待足够的数据支撑

0
Login Topics