工程中用的测试用例代码
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"time"
)
type Response struct {
Count int `json:"count"`
}
func main() {
ipDesp:=make(map[string]string, 14)
ipDesp["这里是ip"]="华东杭州"
ipDesp["这里是ip"]="华东杭州"
ipDesp["这里是ip"]="华东上海"
ipDesp["这里是ip"]="华东上海"
client := &http.Client{}
for{
t := time.Now()
fmt.Println("----------------------------------------------------begin")
fmt.Println(time.Now().Format(time.RFC850), t.Unix())
fmt.Println("----------------------------------------------------")
var count int
var count_hd, counthb, counthn int
for k,v:=range ipDesp{
url := fmt.Sprintf("http://%s:8141/yourpath",k)
request, _ := http.NewRequest("GET", url, nil)
//fmt.Println(url)
response, _ := client.Do(request)
if response.StatusCode == 200 {
body, _ := ioutil.ReadAll(response.Body)
//fmt.Println(string(body))
var r = Response{}
json.Unmarshal(body,&r)
out:=fmt.Sprintf("%8s\t%2d\t%s\t",v,r.Count,k)
fmt.Println(out)
count+=r.Count
if strings.Contains(v,"华南"){
counthn+=r.Count
}else if strings.Contains(v,"华东"){
count_hd+=r.Count
}else if strings.Contains(v,"华北"){
counthb+=r.Count
}
}
}
fmt.Println("同时在线:",count, "华东:",count_hd,"华南:",counthn,"华北:",counthb)
fmt.Println("----------------------------------------------------end")
time.Sleep(5*time.Second)
}
}
如果增加超时
client := &http.Client{
Timeout: time.Duration(1 * time.Second),
}