Skip to content

Commit

Permalink
TCP/SSL/HTTP performance testing tools
Browse files Browse the repository at this point in the history
  • Loading branch information
drealecs committed Nov 9, 2021
0 parents commit ae8b318
Show file tree
Hide file tree
Showing 5 changed files with 228 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build/
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
These are some tools I've been using to test performance issues related to long running HTTP requests or TCP/SSL handshake

Usage:
```sh
concurent-http-requests <requests> <concurrency> <url>
```
```sh
concurrent-ssl-only <requests> <concurrency> <host:port>
```

Depending where the tools will run, you should usually make sure that open file limits is high enough
19 changes: 19 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env sh

script_dir="$(realpath "$(dirname "$0")")"

cd "$script_dir"

mkdir -p "$script_dir/build"

GOOS=windows GOARCH=amd64 go build
GOOS=linux GOARCH=amd64 go build

mv concurrent-http* build

cd "$script_dir/concurrent-ssl-only"

GOOS=windows GOARCH=amd64 go build
GOOS=linux GOARCH=amd64 go build

mv concurrent-ssl* ../build
81 changes: 81 additions & 0 deletions concurrent-ssl-only/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package main

import (
"crypto/tls"
"fmt"
"net"
"os"
"strconv"
"time"
)

func main() {

requestsCount, _ := strconv.Atoi(os.Args[1])
maxConcurrency, _ := strconv.Atoi(os.Args[2])
host := os.Args[3]

dialer := &net.Dialer{
Timeout: 60 * time.Second,
}

config := &tls.Config{
InsecureSkipVerify: true,
}

concurrencyMonitor := make(chan string, maxConcurrency)

concurrent := 0

requestsCountOk := 0
requestsCountFailed := 0
requestsLastError := ""

lastMonitoringMessageTime := time.Now().Add(-3 * time.Second)

for {
if time.Now().Sub(lastMonitoringMessageTime) > 2*time.Second {
fmt.Printf("remaining requests: %v, concurrent requests: %v, ok: %v, failed: %v, lastError: %v\n", requestsCount, concurrent, requestsCountOk, requestsCountFailed, requestsLastError)
lastMonitoringMessageTime = time.Now()
requestsCountOk = 0
requestsCountFailed = 0
requestsLastError = ""
}

if concurrent < maxConcurrency && requestsCount > 0 {
concurrent++
requestsCount--
go DoRequest(dialer, config, host, concurrencyMonitor)
} else {
time.Sleep(10 * time.Microsecond)
}

select {
case status := <-concurrencyMonitor:
concurrent--
if status == "OK" {
requestsCountOk++
} else {
requestsCountFailed++
requestsLastError = status
}
default:
}

if requestsCount <= 0 && concurrent <= 0 {
break
}

}
}

func DoRequest(dialer *net.Dialer, config *tls.Config, host string, monitor chan string) {
conn, err := tls.DialWithDialer(dialer, "tcp", host, config)
if err != nil {
monitor <- err.Error()
return
}

_ = conn.Close()
monitor <- "OK"
}
116 changes: 116 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package main

import (
"crypto/tls"
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
"strconv"
"time"
)

func main() {

requestsCount, _ := strconv.Atoi(os.Args[1])
maxConcurrency, _ := strconv.Atoi(os.Args[2])
url := os.Args[3]

client := CreateClient()
request, _ := CreateRequest(url)

concurrencyMonitor := make(chan string, maxConcurrency)

concurrent := 0

requestsCountOk := 0
requestsCountFailed := 0
requestsLastError := ""

lastMonitoringMessageTime := time.Now().Add(-3 * time.Second)

for {
if time.Now().Sub(lastMonitoringMessageTime) > 2*time.Second {
fmt.Printf("remaining requests: %v, concurrent requests: %v, ok: %v, failed: %v, lastError: %v\n", requestsCount, concurrent, requestsCountOk, requestsCountFailed, requestsLastError)
lastMonitoringMessageTime = time.Now()
requestsCountOk = 0
requestsCountFailed = 0
requestsLastError = ""
}

if concurrent < maxConcurrency && requestsCount > 0 {
concurrent++
requestsCount--
go DoRequest(client, request, concurrencyMonitor)
} else {
time.Sleep(10 * time.Microsecond)
}

select {
case status := <-concurrencyMonitor:
concurrent--
if status == "OK" {
requestsCountOk++
} else {
requestsCountFailed++
requestsLastError = status
}
default:
}

if requestsCount <= 0 && concurrent <= 0 {
break
}

}
}

func CreateClient() *http.Client {
transport := &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
DialContext: (&net.Dialer{
Timeout: 60 * time.Second,
}).DialContext,
TLSHandshakeTimeout: 120 * time.Second,
ResponseHeaderTimeout: 180 * time.Second,
MaxIdleConns: 0,
MaxIdleConnsPerHost: 0,
DisableKeepAlives: true,
}
client := &http.Client{
Transport: transport,
Timeout: 200 * time.Second,
}

return client
}

func CreateRequest(url string) (*http.Request, error) {
request, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, fmt.Errorf("while creating a new request: %w", err)
}
request.Header.Add("Accept", "application/json")

return request, nil
}

func DoRequest(client *http.Client, request *http.Request, monitor chan string) {
response, err := client.Do(request)
if err != nil {
monitor <- err.Error()
return
}

_, err = ioutil.ReadAll(response.Body)
if err != nil {
monitor <- err.Error()
return
}

_ = response.Body.Close()
monitor <- "OK"
}

0 comments on commit ae8b318

Please sign in to comment.