dspv70887 2015-02-09 09:41
浏览 44
已采纳

为什么这个“ hello world” golang http示例在osx上变慢

I was benchmarking my little web application and noticed some significant slowdown when I was running apache bench against it after a couple of thousand requests.

ab -n 20000 http://localhost:8080

The first few thousand requests are fast, then it gets slower and slower. Dramatically slower. While the first thousand take maybe a second, requests 18000-20000 take up to 10 seconds.

So I was trying to find the slow parts and at some point didn't have anything to exclude anymore until I ended up with benchmarking a "hello world" style http server example. To my surprise, the same slowdown occurred.

package main

import "net/http"

func main() {
    mux := http.NewServeMux()

    mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        w.WriteHeader(200)
    })
    http.ListenAndServe(":8080", mux)
}

The same little example runs consistently fast on a linux box. It's not really a big thing because I don't plan to run the app on osx in production ;) But I'm curious what's causing the terrible slowdown on osx anyways

  • 写回答

1条回答 默认 最新

  • duanchen6423 2015-02-09 11:40
    关注

    Phrasing my comments as an answer of sorts:

    I'm not sure what's the specific difference between Linux and OSX, but your server code is perfectly fine and not leaking anything. (I don't have an OSX machine to dig deeper at the moment).

    My suspicion is that the client you're using (ab) doesn't reuse connections, and due to configuration and behavior differences between the two OSes, you're flooding the network stack with about-to-be closed connections or something along these lines.

    Simply use KeepAlive in your ab script - ab -k -c 100 -n 20000 http://localhost:8080 - and as noted in the comments this fixed it. It's also worth trying other tools like JMeter, siege, etc - or just writing a simple Go based client for this. Go has connection reuse in the http library by default.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图2.0 版本点聚合中Marker的位置无法实时更新,如何解决呢?
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题