httpstress_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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_test
  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. log: "2.out",
  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. tc := &tls.Config{InsecureSkipVerify: true}
  47. tr := &http.Transport{
  48. TLSClientConfig: tc,
  49. DisableKeepAlives: true,
  50. ResponseHeaderTimeout: 10 * time.Second,
  51. TLSHandshakeTimeout: 10 * time.Second,
  52. }
  53. client := &http.Client{
  54. Transport: tr,
  55. Timeout: 10 * time.Second,
  56. }
  57. var wg sync.WaitGroup
  58. t0 := time.Now()
  59. var counter int
  60. var lock sync.Mutex
  61. errChan := make(chan error, 2)
  62. // One thread with immediately closed connections
  63. wg.Add(1)
  64. go func() {
  65. for time.Since(t0).Seconds() < 30 {
  66. conn, err := net.Dial("tcp", "localhost:8082")
  67. if err != nil {
  68. log.Println(err)
  69. errChan <- err
  70. return
  71. }
  72. conn.Close()
  73. }
  74. wg.Done()
  75. }()
  76. // 50 threads doing HTTP and HTTPS requests
  77. for i := 0; i < 50; i++ {
  78. i := i
  79. wg.Add(1)
  80. go func() {
  81. for time.Since(t0).Seconds() < 30 {
  82. proto := "http"
  83. if i%2 == 0 {
  84. proto = "https"
  85. }
  86. resp, err := client.Get(proto + "://localhost:8082/")
  87. if err != nil {
  88. errChan <- err
  89. return
  90. }
  91. bs, _ := ioutil.ReadAll(resp.Body)
  92. resp.Body.Close()
  93. if !bytes.Contains(bs, []byte("</html>")) {
  94. log.Printf("%s", bs)
  95. errChan <- errors.New("Incorrect response")
  96. return
  97. }
  98. lock.Lock()
  99. counter++
  100. lock.Unlock()
  101. }
  102. wg.Done()
  103. }()
  104. }
  105. go func() {
  106. wg.Wait()
  107. errChan <- nil
  108. }()
  109. err = <-errChan
  110. t.Logf("%.01f reqs/sec", float64(counter)/time.Since(t0).Seconds())
  111. sender.stop()
  112. if err != nil {
  113. t.Error(err)
  114. }
  115. }