httpstress_test.go 2.5 KB

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