package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"sync"
"time"
)
func main(){
f, err := os.OpenFile("out.log", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
log.Fatal(err)
}
// 完成后延迟关闭
defer f.Close()
//设置日志输出到 f
log.SetOutput(f)
var wg sync.WaitGroup //协程同步并发控制
var total int =1000
var loopnum int=10
wg.Add(total * loopnum)
for num:=1; num <= total; num++{
id := num
go func() {
for i := 1; i <= loopnum; i++ {
testget(id)
//testpost()
//sl := rand.Intn(100)
var du time.Duration = time.Duration(id)
time.Sleep(time.Microsecond * 10 * du)
wg.Done()
}
}()
}
wg.Wait()
}
func testget(id int){
start:=time.Now()
resp, err := http.Get("http://10.12.205.134:8180/pt/idproducer/create?appid=1")
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
resCode := resp.StatusCode //状态码
if err != nil {
fmt.Println("请求失败")
return
}
secs:=time.Since(start).Seconds()
log.Printf("id:%d, 状态:%d, 时间:%.3fs\nBody:%s",id, resCode, secs, string(body))
}
func testpost() {
start:=time.Now()
client := &http.Client{}
//map序列化为json
data := make(map[string]interface{})
data["appid"] = 24
data["appname"] = "测试5"
data["initid"] = 50
data["setp"] = 50
bytesData, _ := json.Marshal(data)
req, _ := http.NewRequest("POST","http://1.1.1.1:8180/idp/cre",bytes.NewReader(bytesData))
resp, err := client.Do(req)
if err != nil {
fmt.Println("请求失败")
return
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
resCode := resp.StatusCode
secs:=time.Since(start).Seconds()
log.Printf("状态:%d, 时间: %.3fs\nBody:%s", resCode, secs, string(body))
}