performance - 无法克服localhost HTTP Golang服务器上的4000 RS阈值

标签 performance http go

我尝试在本地计算机上测量Go默认HTTP服务器实现的带宽。服务器仅接受任何HTTP请求,使用sync.atomic递增计数器并发送200 OK响应。此外,服务器每秒收集一次请求量,将其打印并将计数器重置为零:

type hand struct {
    cnt int32
}

func (h *hand) ServeHTTP(rsp http.ResponseWriter, req *http.Request) {
    atomic.AddInt32(&h.cnt, 1)
    rsp.WriteHeader(200)
    if req.Body != nil {
        req.Body.Close()
    }
}

func main() {
    h := new(hand)
    s := &http.Server{
        Addr:    ":8080",
        Handler: h,
    }
    ticker := time.NewTicker(1 * time.Second)
    go func() {
        for tick := range ticker.C {
            val := atomic.SwapInt32(&h.cnt, 0)
            fmt.Printf("(%v) %d RPS\n", tick, val)
        }
    }()
    log.Fatal(s.ListenAndServe())
}

目标客户端正在尝试同时发送100000 GET请求:

const total = 100000

func main() {
    var r int32
    var rsp int32
    r = total
    rsp = r
    for r > 0 {
        go func() {
            p, err := http.Get("http://localhost:8080")
            atomic.AddInt32(&rsp, -1)
            if err != nil {
                fmt.Printf("error: %s\n", err)
                return
            }
            if p.StatusCode != 200 {
                fmt.Printf("status %d\n", p.StatusCode)
            }
        }()
        r--

    }
    for {
        x := atomic.LoadInt32(&rsp)
        fmt.Printf("sent : %d\n", total-x)
        if x == 0 {
            return
        }
        time.Sleep(1 * time.Second)
    }
}

我正在使用具有5.3.2-gentoo内核的Linux机器。我将ulimitssoft(hardnofile都)更改为100000。当我运行此测试时,所有其他用户应用程序均已停止。

我不希望得到准确的结果,但是只需要知道此阈值的水平即可,例如X000X0000X00000

但是服务器每秒不能处理超过4000请求,它看起来太低了:
# removed timestamps
0 RPS
0 RPS
0 RPS
3953 RPS
3302 RPS
387 RPS
37 RPS
1712 RPS

如何提高HTTP服务器的带宽?还是我的测试方法或本地配置有问题?

最佳答案

问题出在测试方法上:

  • 在同一台计算机上运行客户端和服务器是不正确的,目标服务器应位于专用主机上,目标和客户端之间的网络应足够快
  • 不能选择用于网络测试的自定义脚本:在简单情况下,可以使用wrk,对于更复杂的场景,可以使用Jmetr或其他框架

  • 当我使用wrk在专用主机上测试此服务器时,它显示285900.73 RPS。

    关于performance - 无法克服localhost HTTP Golang服务器上的4000 RS阈值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59333721/

    相关文章:

    php - 使用单个查询更新多个记录以提高性能

    java - 为什么使用 for 循环的倒数总和比流快 400 倍?

    java - 如何通过 HTTP 使用 Java 到 PHP 5 将文件上传到服务器

    arrays - json:无法将数组解码为结构类型的 Go 值

    go - beego中处理表单提交的正确方法是什么?

    java - 通过 MPI 发送 java 对象

    php - 为什么总是调用 symfony2 安全选民?

    performance - HTTP 获取请求 |等待时间长

    asp.net - 如何在asp.net MVC应用程序中为不同的 Controller 指定不同的端口?

    go模块没有正确下载动态库的cgo软链接(soft link)