2013-12-12 97 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,並採取看看你的系統調整大量的開放式套接字