【发布时间】:2017-02-14 16:29:30
【问题描述】:
我正在使用 fasthttp 包构建一个 Rest API。我有一个用来衡量性能的测试路线:
package main
import (
"github.com/valyala/fasthttp"
"runtime"
)
func main() {
runtime.GOMAXPROCS(8)
m := func(ctx *fasthttp.RequestCtx) {
switch string(ctx.Path()) {
case "/test":
test(ctx)
default:
ctx.Error("not found", fasthttp.StatusNotFound)
}
}
fasthttp.ListenAndServe(":80", m)
}
func test(ctx *fasthttp.RequestCtx) {
println("HERE")
}
如果我向该路由发送请求,则需要 10 多秒才能到达测试函数中的 println("HERE")。
我在 Node.js 中构建了一个类似的端点,这个完全相同的功能和路由需要 126 毫秒。
为什么在 Go 中调用这条路由指向的函数需要这么长时间?
【问题讨论】:
-
我刚刚使用您的确切代码进行了测试,几乎不需要时间(不到 1 秒)即可到达
println("HERE")。您的测试设置有多准确? -
尝试我的示例并发布(添加评论)代码(2)输出