package httpclent import ( "context" "fmt" "git.shuncheng.lu/bigthing/gocommon/pkg/boot" "git.shuncheng.lu/bigthing/gocommon/pkg/conf" "git.shuncheng.lu/bigthing/gocommon/pkg/logger" "net/http" "net/url" "testing" "time" ) func initLogger() { var log = func(ctx context.Context, format string, v ...interface{}) { fmt.Printf(format+"\n", v...) } logger.Infoc = log logger.Errorc = log logger.Warnc = log } /** ➜ httpclent git:(master) ✗ go test -v -run=none -bench=BenchmarkLoadDefaultOp -benchmem . goos: darwin goarch: amd64 pkg: git.shuncheng.lu/bigthing/gocommon/pkg/net/httpclent BenchmarkLoadDefaultOp-12 52315970 23.3 ns/op 0 B/op 0 allocs/op PASS ok git.shuncheng.lu/bigthing/gocommon/pkg/net/httpclent 1.257s */ func BenchmarkLoadDefaultOp(b *testing.B) { for i := 0; i < b.N; i++ { op := LoadDefaultOp() FreeOp(op) } } /** ➜ httpclent git:(master) ✗ go test -v -run=none -bench=BenchmarkNewOp -benchmem . goos: darwin goarch: amd64 pkg: git.shuncheng.lu/bigthing/gocommon/pkg/net/httpclent BenchmarkNewOp-12 20625889 62.5 ns/op 112 B/op 1 allocs/op PASS ok git.shuncheng.lu/bigthing/gocommon/pkg/net/httpclent 1.368s */ func BenchmarkNewOp(b *testing.B) { for i := 0; i < b.N; i++ { NewOp() } } func NewOp(option ...Option) *Options { op := new(Options) for _, elem := range option { if elem != nil { elem(op) } } return op } func Test_handlerRequestUrl(t *testing.T) { serverUrl := _TestGetServerUrl(t) resp, err := http.Get(serverUrl.String()) if err != nil { return } t.Logf("%s\n", serverUrl) t.Logf("%#v\n", resp) } func _TestGetServerUrl(t *testing.T) *url.URL { op := LoadDefaultOp(func(options *Options) { options.ServerHostUrl = func(ctx context.Context, serverName string) (s string, e error) { return "127.0.0.1:8080", nil } options.HandlerRequestUrl = SetUrlRequestParams(url.Values{"time": []string{"1"}}) options.Scheme = defaultScheme }) uri, err := handlerRequestUrl(context.Background(), "go-template", "/get", op) if err != nil { t.Fatal(err) } return uri } func TestHttpRequestAndDecode(t *testing.T) { initLogger() response := make(map[string]interface{}, 0) err := HttpRequestAndDecode(context.Background(), "go-template", "/go-template/info", RequestParams{"id": 11}, &response, func(options *Options) { options.ServerHostUrl = func(ctx context.Context, serverName string) (s string, e error) { return "127.0.0.1:8080", nil } options.HandlerRequestHeader = func(ctx context.Context, header http.Header) { header.Add("Content-Type", "application/json") } }) if err != nil { t.Fatal(err) } } /* go test -run=none -bench=Benchmark_addThirdLog -benchmem ./pkg/net/httpclent -o ./bin/Benchmark_addThirdLog goos: darwin goarch: amd64 pkg: git.shuncheng.lu/bigthing/gocommon/pkg/net/httpclent Benchmark_addThirdLog-12 30000 37131 ns/op 1602 B/op 18 allocs/op PASS ok git.shuncheng.lu/bigthing/gocommon/pkg/net/httpclent 1.445s */ func Benchmark_addThirdLog(b *testing.B) { boot.Init() for i := 0; i < b.N; i++ { func() { beginTime := time.Now() if conf.GetString("log", "third_log_switch") == "off" { return } logger.ThirdLog("xxx", 200, time.Now().Sub(beginTime).Milliseconds(), 0, logger.GetTraceId(context.Background())) }() } }