1.背景併發
在性能測試場景中,須要進行評估服務的QPS和服務併發數.徹底模擬程序併發數非go 語言channel和gorouting 莫屬以下腳本內容
package main import ( "fmt" "net/http" "time" ) func requestServer(ch chan <- string){ resp, err := http.Get("http://192.168.1.12:88") if err != nil { ch <- fmt.Sprint(err) return } ch <- fmt.Sprint(resp.Status) defer resp.Body.Close() } func main() { ch := make(chan string) count := 5000 //開始計時 begin := time.Now() fmt.Println("開始時間:", begin) for i := 0; i<= count; i++ { go requestServer(ch) } for y := 0; y<=count; y++ { fmt.Println(<-ch) } end := time.Now() fmt.Println("結束時間:", end, time.Since(begin)) }