httpstress_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This program is free software: you can redistribute it and/or modify it
  4. // under the terms of the GNU General Public License as published by the Free
  5. // Software Foundation, either version 3 of the License, or (at your option)
  6. // any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful, but WITHOUT
  9. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. // more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program. If not, see <http://www.gnu.org/licenses/>.
  15. // +build integration
  16. package integration
  17. import (
  18. "bytes"
  19. "crypto/tls"
  20. "errors"
  21. "io/ioutil"
  22. "log"
  23. "net"
  24. "net/http"
  25. "sync"
  26. "testing"
  27. "time"
  28. )
  29. func TestStressHTTP(t *testing.T) {
  30. log.Println("Cleaning...")
  31. err := removeAll("s2", "h2/index")
  32. if err != nil {
  33. t.Fatal(err)
  34. }
  35. log.Println("Starting up...")
  36. sender := syncthingProcess{ // id1
  37. instance: "2",
  38. argv: []string{"-home", "h2"},
  39. port: 8082,
  40. apiKey: apiKey,
  41. }
  42. err = sender.start()
  43. if err != nil {
  44. t.Fatal(err)
  45. }
  46. // Create a client with reasonable timeouts on all stages of the request.
  47. tc := &tls.Config{InsecureSkipVerify: true}
  48. tr := &http.Transport{
  49. TLSClientConfig: tc,
  50. DisableKeepAlives: true,
  51. ResponseHeaderTimeout: 10 * time.Second,
  52. TLSHandshakeTimeout: 10 * time.Second,
  53. }
  54. client := &http.Client{
  55. Transport: tr,
  56. Timeout: 10 * time.Second,
  57. }
  58. var (
  59. requestsOK = map[string]int{}
  60. requestsError = map[string]int{}
  61. firstError error
  62. lock sync.Mutex
  63. )
  64. gotError := func(ctx string, err error) {
  65. lock.Lock()
  66. requestsError[ctx]++
  67. if firstError == nil {
  68. firstError = err
  69. }
  70. lock.Unlock()
  71. }
  72. requestOK := func(ctx string) {
  73. lock.Lock()
  74. requestsOK[ctx]++
  75. lock.Unlock()
  76. }
  77. log.Println("Testing...")
  78. var wg sync.WaitGroup
  79. t0 := time.Now()
  80. // One thread with immediately closed connections
  81. wg.Add(1)
  82. go func() {
  83. defer wg.Done()
  84. for time.Since(t0).Seconds() < 30 {
  85. conn, err := net.Dial("tcp", "localhost:8082")
  86. if err != nil {
  87. gotError("Dial", err)
  88. } else {
  89. requestOK("Dial")
  90. conn.Close()
  91. }
  92. // At most 100 connects/sec
  93. time.Sleep(10 * time.Millisecond)
  94. }
  95. }()
  96. // 50 threads doing mixed HTTP and HTTPS requests
  97. for i := 0; i < 50; i++ {
  98. i := i
  99. wg.Add(1)
  100. go func() {
  101. defer wg.Done()
  102. for time.Since(t0).Seconds() < 30 {
  103. proto := "http"
  104. if i%2 == 0 {
  105. proto = "https"
  106. }
  107. url := proto + "://localhost:8082/"
  108. resp, err := client.Get(url)
  109. if err != nil {
  110. gotError("Get "+proto, err)
  111. continue
  112. }
  113. bs, err := ioutil.ReadAll(resp.Body)
  114. resp.Body.Close()
  115. if err != nil {
  116. gotError("Read "+proto, err)
  117. continue
  118. }
  119. if !bytes.Contains(bs, []byte("</html>")) {
  120. err := errors.New("Incorrect response")
  121. gotError("Get "+proto, err)
  122. continue
  123. }
  124. requestOK(url)
  125. // At most 100 requests/sec
  126. time.Sleep(10 * time.Millisecond)
  127. }
  128. }()
  129. }
  130. wg.Wait()
  131. t.Logf("OK: %v reqs", requestsOK)
  132. t.Logf("Err: %v reqs", requestsError)
  133. if firstError != nil {
  134. t.Error(firstError)
  135. }
  136. err = sender.stop()
  137. if err != nil {
  138. t.Error(err)
  139. }
  140. }