2014-11-16 20:13:20 +00:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
2014-09-29 19:43:32 +00:00
|
|
|
//
|
2015-03-07 20:36:35 +00:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
2017-02-09 06:52:18 +00:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2014-09-17 15:11:53 +00:00
|
|
|
|
|
|
|
// +build integration
|
|
|
|
|
2014-12-17 11:31:03 +00:00
|
|
|
package integration
|
2014-09-17 15:11:53 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/tls"
|
|
|
|
"errors"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
2014-09-18 09:45:48 +00:00
|
|
|
"net"
|
2014-09-17 15:11:53 +00:00
|
|
|
"net/http"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestStressHTTP(t *testing.T) {
|
|
|
|
log.Println("Cleaning...")
|
2015-03-29 10:55:27 +00:00
|
|
|
err := removeAll("s2", "h2/index*")
|
2014-09-17 15:11:53 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("Starting up...")
|
2015-06-18 13:22:45 +00:00
|
|
|
|
|
|
|
p := startInstance(t, 2)
|
|
|
|
defer checkedStop(t, p)
|
2014-09-17 15:11:53 +00:00
|
|
|
|
2014-11-30 09:35:04 +00:00
|
|
|
// Create a client with reasonable timeouts on all stages of the request.
|
|
|
|
|
2014-09-17 15:11:53 +00:00
|
|
|
tc := &tls.Config{InsecureSkipVerify: true}
|
|
|
|
tr := &http.Transport{
|
2014-10-24 08:01:44 +00:00
|
|
|
TLSClientConfig: tc,
|
|
|
|
DisableKeepAlives: true,
|
2014-11-24 09:16:47 +00:00
|
|
|
ResponseHeaderTimeout: 10 * time.Second,
|
|
|
|
TLSHandshakeTimeout: 10 * time.Second,
|
2014-09-17 15:11:53 +00:00
|
|
|
}
|
|
|
|
client := &http.Client{
|
|
|
|
Transport: tr,
|
2014-11-24 09:16:47 +00:00
|
|
|
Timeout: 10 * time.Second,
|
2014-09-17 15:11:53 +00:00
|
|
|
}
|
|
|
|
|
2014-11-30 09:35:04 +00:00
|
|
|
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...")
|
2014-09-17 15:11:53 +00:00
|
|
|
|
2014-11-30 09:35:04 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
t0 := time.Now()
|
2014-09-18 09:45:48 +00:00
|
|
|
|
|
|
|
// One thread with immediately closed connections
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
2014-11-30 09:35:04 +00:00
|
|
|
defer wg.Done()
|
2014-09-18 09:45:48 +00:00
|
|
|
for time.Since(t0).Seconds() < 30 {
|
|
|
|
conn, err := net.Dial("tcp", "localhost:8082")
|
|
|
|
if err != nil {
|
2014-11-30 09:35:04 +00:00
|
|
|
gotError("Dial", err)
|
|
|
|
} else {
|
|
|
|
requestOK("Dial")
|
|
|
|
conn.Close()
|
2014-09-18 09:45:48 +00:00
|
|
|
}
|
2014-11-30 09:35:04 +00:00
|
|
|
|
|
|
|
// At most 100 connects/sec
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
2014-09-18 09:45:48 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2014-11-30 09:35:04 +00:00
|
|
|
// 50 threads doing mixed HTTP and HTTPS requests
|
2014-09-17 15:11:53 +00:00
|
|
|
for i := 0; i < 50; i++ {
|
|
|
|
i := i
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
2014-11-30 09:35:04 +00:00
|
|
|
defer wg.Done()
|
2014-09-17 15:11:53 +00:00
|
|
|
for time.Since(t0).Seconds() < 30 {
|
|
|
|
proto := "http"
|
|
|
|
if i%2 == 0 {
|
|
|
|
proto = "https"
|
|
|
|
}
|
2014-11-30 09:35:04 +00:00
|
|
|
url := proto + "://localhost:8082/"
|
|
|
|
resp, err := client.Get(url)
|
|
|
|
|
2014-09-17 15:11:53 +00:00
|
|
|
if err != nil {
|
2014-11-30 09:35:04 +00:00
|
|
|
gotError("Get "+proto, err)
|
|
|
|
continue
|
2014-09-17 15:11:53 +00:00
|
|
|
}
|
2014-11-30 09:35:04 +00:00
|
|
|
|
|
|
|
bs, err := ioutil.ReadAll(resp.Body)
|
2014-09-17 15:11:53 +00:00
|
|
|
resp.Body.Close()
|
2014-11-30 09:35:04 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
gotError("Read "+proto, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-09-17 15:11:53 +00:00
|
|
|
if !bytes.Contains(bs, []byte("</html>")) {
|
2014-11-30 09:35:04 +00:00
|
|
|
err := errors.New("Incorrect response")
|
|
|
|
gotError("Get "+proto, err)
|
|
|
|
continue
|
2014-09-17 15:11:53 +00:00
|
|
|
}
|
|
|
|
|
2014-11-30 09:35:04 +00:00
|
|
|
requestOK(url)
|
|
|
|
|
|
|
|
// At most 100 requests/sec
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
2014-09-17 15:11:53 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2014-11-30 09:35:04 +00:00
|
|
|
wg.Wait()
|
|
|
|
t.Logf("OK: %v reqs", requestsOK)
|
|
|
|
t.Logf("Err: %v reqs", requestsError)
|
2014-09-17 15:11:53 +00:00
|
|
|
|
2014-11-30 09:35:04 +00:00
|
|
|
if firstError != nil {
|
|
|
|
t.Error(firstError)
|
2014-11-29 21:17:54 +00:00
|
|
|
}
|
2014-09-17 15:11:53 +00:00
|
|
|
}
|