Reputation: 6030
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
Upvotes: 4
Views: 1193
Reputation: 49187
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.
Upvotes: 5