2013-12-12 99 views
0

我有一个非常简单的围棋服务器:Golang服务器超时

package main 

import(
    "net/http" 
    "fmt" 
    "log" 
) 

func test(w http.ResponseWriter, r *http.Request){ 
    fmt.Println("No bid") 
    http.Error(w, "NoBid", 204) 
} 

func main() { 
    http.HandleFunc("/test/bid", test) 
    http.ListenAndServe(":8080", nil) 
    log.Println("Done serving") 
} 

我然后运行Apache的基准测试工具:

ab -c 50 -n 50000 -p post.txt http://127.0.0.1:8080/test/bid 

服务器运行和响应大约15000请求,然后超时。我想知道为什么会发生这种情况,如果有什么我可以做的。

+0

尝试运行可执行文件以下列方式:'nohup的./ [名]' – ymg

+1

你试图关闭Request.Body? – Volker

+0

你有什么错误(如果有的话?)。你的操作系统上有可能会打开套接字。我还建议使用wrk(https://github.com/wg/wrk)或weighttp(https://github.com/lighttpd/weighttp)而不是Apache Bench。 – elithrar

回答

3

如果你在Linux中运行,也许打开太多的文件,所以它不能创建连接,Yo你需要改变系统配置来支持更多的连接。

例如,

编辑/etc/security/limits.conf添加

* soft nofile 100000 
* soft nofile 100000 

要打开多个文件。

编辑/etc/sysctl.conf

# use more port 
net.ipv4.ip_local_port_range = 1024 65000 
# keep alive timeout 
net.ipv4.tcp_keepalive_time = 300 
# allow reuse 
net.ipv4.tcp_tw_reuse = 1 
# quick recovery 
net.ipv4.tcp_tw_recycle = 1 
2

我尝试没有成功复制在我的Linux笔记本电脑AMD64您的问题 - 它甚至与

ab -c 200 -n 500000 -p post.txt http://127.0.0.1:8080/test/bid 

工作得很好共有约28000插座开放虽然可以碰撞你的系统上的限制。

一个更真实的世界测试也许是把保活上马克塞斯在400个插座

ab -k -c 200 -n 500000 -p post.txt http://127.0.0.1:8080/test/bid 

这样做的结果是

Server Software:   
Server Hostname:  127.0.0.1 
Server Port:   8080 

Document Path:   /test/bid 
Document Length:  6 bytes 

Concurrency Level:  200 
Time taken for tests: 33.807 seconds 
Complete requests:  500000 
Failed requests:  0 
Write errors:   0 
Keep-Alive requests: 500000 
Total transferred:  77000000 bytes 
Total body sent:  221500000 
HTML transferred:  3000000 bytes 
Requests per second: 14790.04 [#/sec] (mean) 
Time per request:  13.523 [ms] (mean) 
Time per request:  0.068 [ms] (mean, across all concurrent requests) 
Transfer rate:   2224.28 [Kbytes/sec] received 
         6398.43 kb/s sent 
         8622.71 kb/s total 

Connection Times (ms) 
       min mean[+/-sd] median max 
Connect:  0 0 0.1  0  11 
Processing:  0 14 5.2  13  42 
Waiting:  0 14 5.2  13  42 
Total:   0 14 5.2  13  42 

Percentage of the requests served within a certain time (ms) 
    50%  13 
    66%  16 
    75%  17 
    80%  18 
    90%  20 
    95%  21 
    98%  24 
    99%  27 
100%  42 (longest request) 

我建议你尝试ab-k,并采取看看你的系统调整大量的开放式套接字