Exemplo n.º 1
0
func benchmarkHttpImpl(N uint64) {
	client := &http.Client{
		Transport: &http.Transport{
			Proxy:               http.ProxyFromEnvironment,
			MaxIdleConnsPerHost: 4 * runtime.GOMAXPROCS(0),
		},
	}
	driver.Parallel(N, 4, func() {
		t0 := time.Now()
		res, err := client.Get(server.Addr)
		if err != nil {
			log.Printf("Get: %v", err)
			return
		}
		defer res.Body.Close()
		all, err := ioutil.ReadAll(res.Body)
		if err != nil {
			log.Printf("ReadAll: %v", err)
			return
		}
		body := string(all)
		if body != "Hello world.\n" {
			log.Fatalf("Got body: " + body)
		}
		driver.LatencyNote(t0)
	})
}
Exemplo n.º 2
0
func benchmarkN(N uint64) {
	const (
		clientsPerConn = 4
		maxInflight    = 16
	)
	procs := runtime.GOMAXPROCS(0)
	send := int64(N)
	var wg sync.WaitGroup
	wg.Add(procs)
	for p := 0; p < procs; p++ {
		client, err := rpc.Dial("tcp", rpcServerAddr)
		if err != nil {
			log.Fatal("error dialing:", err)
		}
		var clientwg sync.WaitGroup
		clientwg.Add(clientsPerConn)
		go func() {
			clientwg.Wait()
			client.Close()
			wg.Done()
		}()
		for c := 0; c < clientsPerConn; c++ {
			resc := make(chan *rpc.Call, maxInflight+1)
			gate := make(chan struct{}, maxInflight)
			go func() {
				for atomic.AddInt64(&send, -1) >= 0 {
					gate <- struct{}{}
					req := &FindReq{"foo", 3}
					res := &FindRes{Start: time.Now()}
					client.Go("Server.Find", req, res, resc)
				}
				close(gate)
			}()
			go func() {
				defer clientwg.Done()
				for _ = range gate {
					call := <-resc
					if call.Error != nil {
						log.Fatalf("rpc failed: %v", call.Error)
					}
					res := call.Reply.(*FindRes)
					if len(res.Matches) != 3 {
						log.Fatalf("incorrect reply: %v", res)
					}
					driver.LatencyNote(res.Start)
				}
			}()
		}
	}
	wg.Wait()
}