golang http client 长连接vs短连接基准测试

package main

import (
	"io/ioutil"
	"net/http"
	"strings"
	"testing"
)


func KeepAlive(client *http.Client) {
	req, err := http.NewRequest("GET", "http://localhost:8080", strings.NewReader("string(str)"))
	if err != nil {
		panic(err)
	}
	res, err := client.Do(req)
	if err != nil {
		panic(err)
	}
	defer res.Body.Close()
	ioutil.ReadAll(res.Body)
}

func BenchmarkKeepAlive(b *testing.B) {
	var client = http.DefaultClient
	for i := 0; i < b.N; i++ {
		KeepAlive(client)
	}

}


func BenchmarkDisableKeepAlive(b *testing.B) {
	client := &http.Client{
		Transport:     &http.Transport{
			DisableKeepAlives: true,
		},
	}
	for i := 0; i < b.N; i++ {
		KeepAlive(client)
	}

}

 

 

 

  

posted on 2021-05-07 14:32  biwentao  阅读(713)  评论(0编辑  收藏  举报

导航