工程中用的测试用例代码

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),
	}
Logo

新一代开源开发者平台 GitCode,通过集成代码托管服务、代码仓库以及可信赖的开源组件库,让开发者可以在云端进行代码托管和开发。旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐