httpstress_test.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
  2. // All rights reserved. Use of this source code is governed by an MIT-style
  3. // license that can be found in the LICENSE file.
  4. // +build integration
  5. package integration_test
  6. import (
  7. "bytes"
  8. "crypto/tls"
  9. "errors"
  10. "io/ioutil"
  11. "log"
  12. "net"
  13. "net/http"
  14. "sync"
  15. "testing"
  16. "time"
  17. )
  18. func TestStressHTTP(t *testing.T) {
  19. log.Println("Cleaning...")
  20. err := removeAll("s2", "h2/index")
  21. if err != nil {
  22. t.Fatal(err)
  23. }
  24. log.Println("Starting up...")
  25. sender := syncthingProcess{ // id1
  26. log: "2.out",
  27. argv: []string{"-home", "h2"},
  28. port: 8082,
  29. apiKey: apiKey,
  30. }
  31. ver, err := sender.start()
  32. if err != nil {
  33. t.Fatal(err)
  34. }
  35. log.Println(ver)
  36. tc := &tls.Config{InsecureSkipVerify: true}
  37. tr := &http.Transport{
  38. TLSClientConfig: tc,
  39. }
  40. client := &http.Client{
  41. Transport: tr,
  42. Timeout: 2 * time.Second,
  43. }
  44. var wg sync.WaitGroup
  45. t0 := time.Now()
  46. var counter int
  47. var lock sync.Mutex
  48. errChan := make(chan error, 2)
  49. // One thread with immediately closed connections
  50. wg.Add(1)
  51. go func() {
  52. for time.Since(t0).Seconds() < 30 {
  53. conn, err := net.Dial("tcp", "localhost:8082")
  54. if err != nil {
  55. log.Println(err)
  56. errChan <- err
  57. return
  58. }
  59. conn.Close()
  60. }
  61. wg.Done()
  62. }()
  63. // 50 threads doing HTTP and HTTPS requests
  64. for i := 0; i < 50; i++ {
  65. i := i
  66. wg.Add(1)
  67. go func() {
  68. for time.Since(t0).Seconds() < 30 {
  69. proto := "http"
  70. if i%2 == 0 {
  71. proto = "https"
  72. }
  73. resp, err := client.Get(proto + "://localhost:8082/")
  74. if err != nil {
  75. errChan <- err
  76. return
  77. }
  78. bs, _ := ioutil.ReadAll(resp.Body)
  79. resp.Body.Close()
  80. if !bytes.Contains(bs, []byte("</html>")) {
  81. log.Printf("%s", bs)
  82. errChan <- errors.New("Incorrect response")
  83. return
  84. }
  85. lock.Lock()
  86. counter++
  87. lock.Unlock()
  88. }
  89. wg.Done()
  90. }()
  91. }
  92. go func() {
  93. wg.Wait()
  94. errChan <- nil
  95. }()
  96. err = <-errChan
  97. t.Logf("%.01f reqs/sec", float64(counter)/time.Since(t0).Seconds())
  98. sender.stop()
  99. if err != nil {
  100. t.Error(err)
  101. }
  102. }