http_client_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package httpclent
  2. import (
  3. "context"
  4. "fmt"
  5. "git.shuncheng.lu/bigthing/gocommon/pkg/boot"
  6. "git.shuncheng.lu/bigthing/gocommon/pkg/conf"
  7. "git.shuncheng.lu/bigthing/gocommon/pkg/logger"
  8. "net/http"
  9. "net/url"
  10. "testing"
  11. "time"
  12. )
  13. func initLogger() {
  14. var log = func(ctx context.Context, format string, v ...interface{}) {
  15. fmt.Printf(format+"\n", v...)
  16. }
  17. logger.Infoc = log
  18. logger.Errorc = log
  19. logger.Warnc = log
  20. }
  21. /**
  22. ➜ httpclent git:(master) ✗ go test -v -run=none -bench=BenchmarkLoadDefaultOp -benchmem .
  23. goos: darwin
  24. goarch: amd64
  25. pkg: git.shuncheng.lu/bigthing/gocommon/pkg/net/httpclent
  26. BenchmarkLoadDefaultOp-12 52315970 23.3 ns/op 0 B/op 0 allocs/op
  27. PASS
  28. ok git.shuncheng.lu/bigthing/gocommon/pkg/net/httpclent 1.257s
  29. */
  30. func BenchmarkLoadDefaultOp(b *testing.B) {
  31. for i := 0; i < b.N; i++ {
  32. op := LoadDefaultOp()
  33. FreeOp(op)
  34. }
  35. }
  36. /**
  37. ➜ httpclent git:(master) ✗ go test -v -run=none -bench=BenchmarkNewOp -benchmem .
  38. goos: darwin
  39. goarch: amd64
  40. pkg: git.shuncheng.lu/bigthing/gocommon/pkg/net/httpclent
  41. BenchmarkNewOp-12 20625889 62.5 ns/op 112 B/op 1 allocs/op
  42. PASS
  43. ok git.shuncheng.lu/bigthing/gocommon/pkg/net/httpclent 1.368s
  44. */
  45. func BenchmarkNewOp(b *testing.B) {
  46. for i := 0; i < b.N; i++ {
  47. NewOp()
  48. }
  49. }
  50. func NewOp(option ...Option) *Options {
  51. op := new(Options)
  52. for _, elem := range option {
  53. if elem != nil {
  54. elem(op)
  55. }
  56. }
  57. return op
  58. }
  59. func Test_handlerRequestUrl(t *testing.T) {
  60. serverUrl := _TestGetServerUrl(t)
  61. resp, err := http.Get(serverUrl.String())
  62. if err != nil {
  63. return
  64. }
  65. t.Logf("%s\n", serverUrl)
  66. t.Logf("%#v\n", resp)
  67. }
  68. func _TestGetServerUrl(t *testing.T) *url.URL {
  69. op := LoadDefaultOp(func(options *Options) {
  70. options.ServerHostUrl = func(ctx context.Context, serverName string) (s string, e error) {
  71. return "127.0.0.1:8080", nil
  72. }
  73. options.HandlerRequestUrl = SetUrlRequestParams(url.Values{"time": []string{"1"}})
  74. options.Scheme = defaultScheme
  75. })
  76. uri, err := handlerRequestUrl(context.Background(), "go-template", "/get", op)
  77. if err != nil {
  78. t.Fatal(err)
  79. }
  80. return uri
  81. }
  82. func TestHttpRequestAndDecode(t *testing.T) {
  83. initLogger()
  84. response := make(map[string]interface{}, 0)
  85. err := HttpRequestAndDecode(context.Background(), "go-template", "/go-template/info", RequestParams{"id": 11}, &response, func(options *Options) {
  86. options.ServerHostUrl = func(ctx context.Context, serverName string) (s string, e error) {
  87. return "127.0.0.1:8080", nil
  88. }
  89. options.HandlerRequestHeader = func(ctx context.Context, header http.Header) {
  90. header.Add("Content-Type", "application/json")
  91. }
  92. })
  93. if err != nil {
  94. t.Fatal(err)
  95. }
  96. }
  97. /*
  98. go test -run=none -bench=Benchmark_addThirdLog -benchmem ./pkg/net/httpclent -o ./bin/Benchmark_addThirdLog
  99. goos: darwin
  100. goarch: amd64
  101. pkg: git.shuncheng.lu/bigthing/gocommon/pkg/net/httpclent
  102. Benchmark_addThirdLog-12 30000 37131 ns/op 1602 B/op 18 allocs/op
  103. PASS
  104. ok git.shuncheng.lu/bigthing/gocommon/pkg/net/httpclent 1.445s
  105. */
  106. func Benchmark_addThirdLog(b *testing.B) {
  107. boot.Init()
  108. for i := 0; i < b.N; i++ {
  109. func() {
  110. beginTime := time.Now()
  111. if conf.GetString("log", "third_log_switch") == "off" {
  112. return
  113. }
  114. logger.ThirdLog("xxx", 200, time.Now().Sub(beginTime).Milliseconds(), 0, logger.GetTraceId(context.Background()))
  115. }()
  116. }
  117. }