mirror of
https://github.com/octoleo/syncthing.git
synced 2025-01-03 15:17:25 +00:00
Increase read timeout on HTTP server, try to not run out of sockets in stress test
This commit is contained in:
parent
9d36d88a65
commit
e0265aed05
@ -158,7 +158,7 @@ func startGUI(cfg config.GUIConfiguration, assetDir string, m *model.Model) erro
|
|||||||
|
|
||||||
srv := http.Server{
|
srv := http.Server{
|
||||||
Handler: handler,
|
Handler: handler,
|
||||||
ReadTimeout: 2 * time.Second,
|
ReadTimeout: 10 * time.Second,
|
||||||
}
|
}
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
|
@ -49,6 +49,8 @@ func TestStressHTTP(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create a client with reasonable timeouts on all stages of the request.
|
||||||
|
|
||||||
tc := &tls.Config{InsecureSkipVerify: true}
|
tc := &tls.Config{InsecureSkipVerify: true}
|
||||||
tr := &http.Transport{
|
tr := &http.Transport{
|
||||||
TLSClientConfig: tc,
|
TLSClientConfig: tc,
|
||||||
@ -60,71 +62,100 @@ func TestStressHTTP(t *testing.T) {
|
|||||||
Transport: tr,
|
Transport: tr,
|
||||||
Timeout: 10 * time.Second,
|
Timeout: 10 * time.Second,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
requestsOK = map[string]int{}
|
||||||
|
requestsError = map[string]int{}
|
||||||
|
firstError error
|
||||||
|
lock sync.Mutex
|
||||||
|
)
|
||||||
|
|
||||||
|
gotError := func(ctx string, err error) {
|
||||||
|
lock.Lock()
|
||||||
|
requestsError[ctx]++
|
||||||
|
if firstError == nil {
|
||||||
|
firstError = err
|
||||||
|
}
|
||||||
|
lock.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
requestOK := func(ctx string) {
|
||||||
|
lock.Lock()
|
||||||
|
requestsOK[ctx]++
|
||||||
|
lock.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Println("Testing...")
|
||||||
|
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
t0 := time.Now()
|
t0 := time.Now()
|
||||||
|
|
||||||
var counter int
|
|
||||||
var lock sync.Mutex
|
|
||||||
|
|
||||||
errChan := make(chan error, 2)
|
|
||||||
|
|
||||||
// One thread with immediately closed connections
|
// One thread with immediately closed connections
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
|
defer wg.Done()
|
||||||
for time.Since(t0).Seconds() < 30 {
|
for time.Since(t0).Seconds() < 30 {
|
||||||
conn, err := net.Dial("tcp", "localhost:8082")
|
conn, err := net.Dial("tcp", "localhost:8082")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
gotError("Dial", err)
|
||||||
errChan <- err
|
} else {
|
||||||
return
|
requestOK("Dial")
|
||||||
}
|
|
||||||
conn.Close()
|
conn.Close()
|
||||||
}
|
}
|
||||||
wg.Done()
|
|
||||||
|
// At most 100 connects/sec
|
||||||
|
time.Sleep(10 * time.Millisecond)
|
||||||
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// 50 threads doing HTTP and HTTPS requests
|
// 50 threads doing mixed HTTP and HTTPS requests
|
||||||
for i := 0; i < 50; i++ {
|
for i := 0; i < 50; i++ {
|
||||||
i := i
|
i := i
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
|
defer wg.Done()
|
||||||
for time.Since(t0).Seconds() < 30 {
|
for time.Since(t0).Seconds() < 30 {
|
||||||
proto := "http"
|
proto := "http"
|
||||||
if i%2 == 0 {
|
if i%2 == 0 {
|
||||||
proto = "https"
|
proto = "https"
|
||||||
}
|
}
|
||||||
resp, err := client.Get(proto + "://localhost:8082/")
|
url := proto + "://localhost:8082/"
|
||||||
|
resp, err := client.Get(url)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errChan <- err
|
gotError("Get "+proto, err)
|
||||||
return
|
continue
|
||||||
}
|
}
|
||||||
bs, _ := ioutil.ReadAll(resp.Body)
|
|
||||||
|
bs, err := ioutil.ReadAll(resp.Body)
|
||||||
resp.Body.Close()
|
resp.Body.Close()
|
||||||
if !bytes.Contains(bs, []byte("</html>")) {
|
|
||||||
log.Printf("%s", bs)
|
|
||||||
errChan <- errors.New("Incorrect response")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
lock.Lock()
|
|
||||||
counter++
|
|
||||||
lock.Unlock()
|
|
||||||
}
|
|
||||||
wg.Done()
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
wg.Wait()
|
|
||||||
errChan <- nil
|
|
||||||
}()
|
|
||||||
|
|
||||||
err = <-errChan
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
gotError("Read "+proto, err)
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Logf("%.01f reqs/sec", float64(counter)/time.Since(t0).Seconds())
|
if !bytes.Contains(bs, []byte("</html>")) {
|
||||||
|
err := errors.New("Incorrect response")
|
||||||
|
gotError("Get "+proto, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
requestOK(url)
|
||||||
|
|
||||||
|
// At most 100 requests/sec
|
||||||
|
time.Sleep(10 * time.Millisecond)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
wg.Wait()
|
||||||
|
t.Logf("OK: %v reqs", requestsOK)
|
||||||
|
t.Logf("Err: %v reqs", requestsError)
|
||||||
|
|
||||||
|
if firstError != nil {
|
||||||
|
t.Error(firstError)
|
||||||
|
}
|
||||||
|
|
||||||
err = sender.stop()
|
err = sender.stop()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user