import (
"flag"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"time"
)
var (
Requests int
Connections int
)
func init() {
flag.IntVar(&Connections, "connections", 100, "设置连接数")
flag.IntVar(&Requests, "requests", 100, "每个连接发送请求数")
}
func main() {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Hello, client")
}))
defer ts.Close()
flag.Parse()
fmt.Printf("%d connections and per %d requests\n", Connections, Requests)
result := make(chan time.Duration, Connections)
client := &http.Client{}
for i := 0; i < Connections; i++ {
go func() {
start := time.Now()
for j := 0; j < Requests; j++ {
resp, err := client.Get(ts.URL)
if err != nil {
fmt.Println("err:", err)
continue
}
io.Copy(ioutil.Discard, resp.Body)
resp.Body.Close()
}
result <- time.Since(start)
}()
}
var total int64
for i := 0; i < Connections; i++ {
total += int64(<-result)
}
latency := time.Duration(total / int64(Connections) / int64(Requests))
fmt.Printf("Latency: %s\n", latency)
}
系统OSX 10.11.1;golang 1.5.1;i7 2.2GHz 16G内存
我设置100个connection没有问题,超过200就会有reset by peer了
是我client没有用对吗?还是别的什么问题?求助。
多谢!
--
Yudi(ZhangXinyu)
From:Suzhou, China
Email:smartzxy AT gmail DOT com
QQ: зlббqßοбб