httpstress_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at http://mozilla.org/MPL/2.0/.
  6. // +build integration
  7. package integration
  8. import (
  9. "bytes"
  10. "crypto/tls"
  11. "errors"
  12. "io/ioutil"
  13. "log"
  14. "net"
  15. "net/http"
  16. "sync"
  17. "testing"
  18. "time"
  19. )
  20. func TestStressHTTP(t *testing.T) {
  21. log.Println("Cleaning...")
  22. err := removeAll("s2", "h2/index*")
  23. if err != nil {
  24. t.Fatal(err)
  25. }
  26. log.Println("Starting up...")
  27. p := startInstance(t, 2)
  28. defer checkedStop(t, p)
  29. // Create a client with reasonable timeouts on all stages of the request.
  30. tc := &tls.Config{InsecureSkipVerify: true}
  31. tr := &http.Transport{
  32. TLSClientConfig: tc,
  33. DisableKeepAlives: true,
  34. ResponseHeaderTimeout: 10 * time.Second,
  35. TLSHandshakeTimeout: 10 * time.Second,
  36. }
  37. client := &http.Client{
  38. Transport: tr,
  39. Timeout: 10 * time.Second,
  40. }
  41. var (
  42. requestsOK = map[string]int{}
  43. requestsError = map[string]int{}
  44. firstError error
  45. lock sync.Mutex
  46. )
  47. gotError := func(ctx string, err error) {
  48. lock.Lock()
  49. requestsError[ctx]++
  50. if firstError == nil {
  51. firstError = err
  52. }
  53. lock.Unlock()
  54. }
  55. requestOK := func(ctx string) {
  56. lock.Lock()
  57. requestsOK[ctx]++
  58. lock.Unlock()
  59. }
  60. log.Println("Testing...")
  61. var wg sync.WaitGroup
  62. t0 := time.Now()
  63. // One thread with immediately closed connections
  64. wg.Add(1)
  65. go func() {
  66. defer wg.Done()
  67. for time.Since(t0).Seconds() < 30 {
  68. conn, err := net.Dial("tcp", "localhost:8082")
  69. if err != nil {
  70. gotError("Dial", err)
  71. } else {
  72. requestOK("Dial")
  73. conn.Close()
  74. }
  75. // At most 100 connects/sec
  76. time.Sleep(10 * time.Millisecond)
  77. }
  78. }()
  79. // 50 threads doing mixed HTTP and HTTPS requests
  80. for i := 0; i < 50; i++ {
  81. i := i
  82. wg.Add(1)
  83. go func() {
  84. defer wg.Done()
  85. for time.Since(t0).Seconds() < 30 {
  86. proto := "http"
  87. if i%2 == 0 {
  88. proto = "https"
  89. }
  90. url := proto + "://localhost:8082/"
  91. resp, err := client.Get(url)
  92. if err != nil {
  93. gotError("Get "+proto, err)
  94. continue
  95. }
  96. bs, err := ioutil.ReadAll(resp.Body)
  97. resp.Body.Close()
  98. if err != nil {
  99. gotError("Read "+proto, err)
  100. continue
  101. }
  102. if !bytes.Contains(bs, []byte("</html>")) {
  103. err := errors.New("Incorrect response")
  104. gotError("Get "+proto, err)
  105. continue
  106. }
  107. requestOK(url)
  108. // At most 100 requests/sec
  109. time.Sleep(10 * time.Millisecond)
  110. }
  111. }()
  112. }
  113. wg.Wait()
  114. t.Logf("OK: %v reqs", requestsOK)
  115. t.Logf("Err: %v reqs", requestsError)
  116. if firstError != nil {
  117. t.Error(firstError)
  118. }
  119. }