package main import ( "fmt" "net/http" "strings" "time" ) var( no =0 ok =0 //记录请求成功失败数 useTime = 0.0 //使用时间 num=100 //并发个数 ) func main() { startTime := time.Now().UnixNano()//记录并发开始时间 goroutinetest(num) endTime := time.Now().UnixNano() useTime = float64(endTime-startTime) / 1e9 //记录所有请求完成时间 fmt.Println("响应成功数:",ok) fmt.Println("相应失败数:",no) fmt.Println("qps :", fmt.Sprintf("%.4f", float64(num)/useTime)) } func goroutinetest(num int) { for i := 0; i < num; i++ { go postdata() } time.Sleep(time.Second * 1) } func postdata(){ resp, err := http.Post("http://www.baidu.com", "application/x-www-form-urlencoded", strings.NewReader("id=1")) if err != nil { panic(err) } //关闭连接 defer resp.Body.Close() if resp.StatusCode != 200 {no += 1}//使用响应吗作为断言 else{ok += 1} }