httpstress_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. sender := syncthingProcess{ // id1
  28. instance: "2",
  29. argv: []string{"-home", "h2"},
  30. port: 8082,
  31. apiKey: apiKey,
  32. }
  33. err = sender.start()
  34. if err != nil {
  35. t.Fatal(err)
  36. }
  37. // Create a client with reasonable timeouts on all stages of the request.
  38. tc := &tls.Config{InsecureSkipVerify: true}
  39. tr := &http.Transport{
  40. TLSClientConfig: tc,
  41. DisableKeepAlives: true,
  42. ResponseHeaderTimeout: 10 * time.Second,
  43. TLSHandshakeTimeout: 10 * time.Second,
  44. }
  45. client := &http.Client{
  46. Transport: tr,
  47. Timeout: 10 * time.Second,
  48. }
  49. var (
  50. requestsOK = map[string]int{}
  51. requestsError = map[string]int{}
  52. firstError error
  53. lock sync.Mutex
  54. )
  55. gotError := func(ctx string, err error) {
  56. lock.Lock()
  57. requestsError[ctx]++
  58. if firstError == nil {
  59. firstError = err
  60. }
  61. lock.Unlock()
  62. }
  63. requestOK := func(ctx string) {
  64. lock.Lock()
  65. requestsOK[ctx]++
  66. lock.Unlock()
  67. }
  68. log.Println("Testing...")
  69. var wg sync.WaitGroup
  70. t0 := time.Now()
  71. // One thread with immediately closed connections
  72. wg.Add(1)
  73. go func() {
  74. defer wg.Done()
  75. for time.Since(t0).Seconds() < 30 {
  76. conn, err := net.Dial("tcp", "localhost:8082")
  77. if err != nil {
  78. gotError("Dial", err)
  79. } else {
  80. requestOK("Dial")
  81. conn.Close()
  82. }
  83. // At most 100 connects/sec
  84. time.Sleep(10 * time.Millisecond)
  85. }
  86. }()
  87. // 50 threads doing mixed HTTP and HTTPS requests
  88. for i := 0; i < 50; i++ {
  89. i := i
  90. wg.Add(1)
  91. go func() {
  92. defer wg.Done()
  93. for time.Since(t0).Seconds() < 30 {
  94. proto := "http"
  95. if i%2 == 0 {
  96. proto = "https"
  97. }
  98. url := proto + "://localhost:8082/"
  99. resp, err := client.Get(url)
  100. if err != nil {
  101. gotError("Get "+proto, err)
  102. continue
  103. }
  104. bs, err := ioutil.ReadAll(resp.Body)
  105. resp.Body.Close()
  106. if err != nil {
  107. gotError("Read "+proto, err)
  108. continue
  109. }
  110. if !bytes.Contains(bs, []byte("</html>")) {
  111. err := errors.New("Incorrect response")
  112. gotError("Get "+proto, err)
  113. continue
  114. }
  115. requestOK(url)
  116. // At most 100 requests/sec
  117. time.Sleep(10 * time.Millisecond)
  118. }
  119. }()
  120. }
  121. wg.Wait()
  122. t.Logf("OK: %v reqs", requestsOK)
  123. t.Logf("Err: %v reqs", requestsError)
  124. if firstError != nil {
  125. t.Error(firstError)
  126. }
  127. _, err = sender.stop()
  128. if err != nil {
  129. t.Error(err)
  130. }
  131. }